learndrone/main.go
Pedro Pérez cf4ee051bb
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
add test
2025-05-30 01:05:36 +02:00

37 lines
865 B
Go

package main
import (
"encoding/json"
"net/http"
"strconv"
)
const (
version = "1.0.1"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, World page"))
})
mux.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(map[string]string{"message": "hello world json!"})
})
mux.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(map[string]string{"version": version})
})
mux.HandleFunc("/sum", func(w http.ResponseWriter, r *http.Request) {
a, _ := strconv.Atoi(r.URL.Query().Get("a"))
b, _ := strconv.Atoi(r.URL.Query().Get("b"))
json.NewEncoder(w).Encode(map[string]int{"result": sum(a, b)})
})
http.ListenAndServe(":8080", mux)
}
func sum(a, b int) int {
return a + b
}