package main import ( "encoding/json" "log" "mime" "net/http" "strconv" "git.phc.dm.unipi.it/phc/drone-example/foo" ) func main() { r := http.NewServeMux() r.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) return } aStr := r.URL.Query().Get("a") bStr := r.URL.Query().Get("b") a, err := strconv.Atoi(aStr) if err != nil { http.Error(w, `the query param "a" must be an integer`, http.StatusUnprocessableEntity) return } b, err := strconv.Atoi(bStr) if err != nil { http.Error(w, `the query param "b" must be an integer`, http.StatusUnprocessableEntity) return } result := foo.Sum(a, b) w.Header().Set("Content-Type", mime.TypeByExtension(".json")) if err := json.NewEncoder(w).Encode(result); err != nil { log.Fatal(err) return } }) log.Fatal(http.ListenAndServe(":5000", r)) }