Page 1 of 1

Server on Golang

Posted: Wed Nov 08, 2023 2:04 pm
by admin
package main

import (
"fmt"
"log"
"net/http"
// "strconv"
// "sync"
)

var (
count int
)

func fibonacci(n int) int {
if n <= 1 {
return n
}
return fibonacci(n-1) + fibonacci(n-2)
}

func fibonacciHandler(w http.ResponseWriter, r *http.Request) {

fmt.Fprintf(w, "%d\n", fibonacci(count))
count++
}

func metricsHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "rpc_duration_milliseconds_count %d", count)
}

func main() {
count = 0
http.HandleFunc("/", fibonacciHandler)
http.HandleFunc("/metrics", metricsHandler)
log.Println("Server started on http://localhost:8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}