You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
963 B
Go

2 years ago
package main
import (
"encoding/json"
2 years ago
"log"
"mime"
"net/http"
"strconv"
2 years ago
"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))
2 years ago
}