html templating

This commit is contained in:
Jan 2024-10-26 19:53:38 +02:00
parent 434b9860c3
commit e72a267cd4
3 changed files with 54 additions and 54 deletions

View File

@ -1,60 +1,35 @@
package server package server
import ( import (
"fmt" "html/template"
"net/http" "net/http"
"strconv" "strconv"
"github.com/LeRoid-hub/humiditycalc/internal" "github.com/LeRoid-hub/humiditycalc/internal"
) )
type result struct {
AbsoluteHumidity float64
}
// StartServer starts the HTTP server. // StartServer starts the HTTP server.
func Run() { 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) {
http.ListenAndServe(":8080", nil)
}
func HandleIndex(w http.ResponseWriter, r *http.Request) {
html := `<html>
<head>
<title>Humidity Calculator</title>
</head>
<body>
<h1>Humidity Calculator</h1>
<p>Calculate absolute humidity in g/m³ given temperature in Celsius and relative humidity percentage.</p>
<form action="/absolute-humidity">
<label for="temp">Temperature (°C):</label>
<input type="text" id="temp" name="temp" required>
<br>
<label for="rh">Relative Humidity (%):</label>
<input type="text" id="rh" name="rh" required>
<br>
<button type="submit">Calculate</button>
</form>
</body>
</html>`
w.Write([]byte(html))
}
func HandleAbsoluteHumidity(w http.ResponseWriter, r *http.Request) {
// Parse query parameters // Parse query parameters
tempCelsius, relativeHumidity := r.URL.Query().Get("temp"), r.URL.Query().Get("rh") tempCelsius, relativeHumidity := r.URL.Query().Get("temp"), r.URL.Query().Get("rh")
if tempCelsius == "" || relativeHumidity == "" { if tempCelsius == "" || relativeHumidity == "" {
http.Error(w, "missing query parameters", http.StatusBadRequest) tmpl.Execute(w, nil)
return return
} }
// Convert query parameters to float64 // Convert query parameters to float64
temp, err := strconv.ParseFloat(tempCelsius, 32) temp, err := strconv.ParseFloat(tempCelsius, 32)
if err != nil { if err != nil {
http.Error(w, "invalid temperature value", http.StatusBadRequest) http.Error(w, "invalid temperature value", http.StatusBadRequest)
return return
} }
rh, err := strconv.ParseFloat(relativeHumidity, 32) rh, err := strconv.ParseFloat(relativeHumidity, 32)
if err != nil { if err != nil {
http.Error(w, "invalid relative humidity value", http.StatusBadRequest) http.Error(w, "invalid relative humidity value", http.StatusBadRequest)
@ -65,5 +40,9 @@ func HandleAbsoluteHumidity(w http.ResponseWriter, r *http.Request) {
absoluteHumidity := internal.AbsoluteHumidity(temp, rh) absoluteHumidity := internal.AbsoluteHumidity(temp, rh)
// Write response // Write response
w.Write([]byte(fmt.Sprintf("Absolute humidity: %.2f g/m³\n", absoluteHumidity))) tmpl.Execute(w, result{AbsoluteHumidity: absoluteHumidity})
})
http.ListenAndServe(":8080", nil)
} }

0
web/static/a Normal file
View File

21
web/templates/index.html Normal file
View File

@ -0,0 +1,21 @@
<html>
<head>
<title>Humidity Calculator</title>
</head>
<body>
<h1>Humidity Calculator</h1>
<p>Calculate absolute humidity in g/m³ given temperature in Celsius and relative humidity percentage.</p>
<form action="/">
<label for="temp">Temperature (°C):</label>
<input type="text" id="temp" name="temp" required>
<br>
<label for="rh">Relative Humidity (%):</label>
<input type="text" id="rh" name="rh" required>
<br>
<button type="submit">Calculate</button>
</form>
{{ if .AbsoluteHumidity }}
<p>Absolute Humidity: {{ .AbsoluteHumidity }} g/m³</p>
{{ end }}
</body>
</html>