diff --git a/server/server.go b/server/server.go index bebfcb3..3131d30 100644 --- a/server/server.go +++ b/server/server.go @@ -1,69 +1,48 @@ package server import ( - "fmt" + "html/template" "net/http" "strconv" "github.com/LeRoid-hub/humiditycalc/internal" ) +type result struct { + AbsoluteHumidity float64 +} + // StartServer starts the HTTP server. func Run() { - http.HandleFunc("/", HandleIndex) + tmpl := template.Must(template.ParseFiles("./web/templates/index.html")) - http.HandleFunc("/absolute-humidity", HandleAbsoluteHumidity) + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + // Parse query parameters + tempCelsius, relativeHumidity := r.URL.Query().Get("temp"), r.URL.Query().Get("rh") + if tempCelsius == "" || relativeHumidity == "" { + tmpl.Execute(w, nil) + return + } + + // Convert query parameters to float64 + temp, err := strconv.ParseFloat(tempCelsius, 32) + if err != nil { + http.Error(w, "invalid temperature value", http.StatusBadRequest) + return + } + rh, err := strconv.ParseFloat(relativeHumidity, 32) + if err != nil { + http.Error(w, "invalid relative humidity value", http.StatusBadRequest) + return + } + + // Calculate absolute humidity + absoluteHumidity := internal.AbsoluteHumidity(temp, rh) + + // Write response + tmpl.Execute(w, result{AbsoluteHumidity: absoluteHumidity}) + }) http.ListenAndServe(":8080", nil) -} - -func HandleIndex(w http.ResponseWriter, r *http.Request) { - html := ` - - Humidity Calculator - - -

Humidity Calculator

-

Calculate absolute humidity in g/m³ given temperature in Celsius and relative humidity percentage.

-
- - -
- - -
- -
- -` - w.Write([]byte(html)) -} - -func HandleAbsoluteHumidity(w http.ResponseWriter, r *http.Request) { - // Parse query parameters - tempCelsius, relativeHumidity := r.URL.Query().Get("temp"), r.URL.Query().Get("rh") - if tempCelsius == "" || relativeHumidity == "" { - http.Error(w, "missing query parameters", http.StatusBadRequest) - return - } - - // Convert query parameters to float64 - - temp, err := strconv.ParseFloat(tempCelsius, 32) - if err != nil { - http.Error(w, "invalid temperature value", http.StatusBadRequest) - return - } - - rh, err := strconv.ParseFloat(relativeHumidity, 32) - if err != nil { - http.Error(w, "invalid relative humidity value", http.StatusBadRequest) - return - } - - // Calculate absolute humidity - absoluteHumidity := internal.AbsoluteHumidity(temp, rh) - - // Write response - w.Write([]byte(fmt.Sprintf("Absolute humidity: %.2f g/m³\n", absoluteHumidity))) + } diff --git a/web/static/a b/web/static/a new file mode 100644 index 0000000..e69de29 diff --git a/web/templates/index.html b/web/templates/index.html new file mode 100644 index 0000000..337664c --- /dev/null +++ b/web/templates/index.html @@ -0,0 +1,21 @@ + + + Humidity Calculator + + +

Humidity Calculator

+

Calculate absolute humidity in g/m³ given temperature in Celsius and relative humidity percentage.

+
+ + +
+ + +
+ +
+ {{ if .AbsoluteHumidity }} +

Absolute Humidity: {{ .AbsoluteHumidity }} g/m³

+ {{ end }} + + \ No newline at end of file