mirror of
https://github.com/LeRoid-hub/humiditycalc.git
synced 2025-01-31 03:34:56 +00:00
html templating
This commit is contained in:
parent
434b9860c3
commit
e72a267cd4
@ -1,69 +1,48 @@
|
|||||||
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) {
|
||||||
|
// 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)
|
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
|
|
||||||
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)))
|
|
||||||
}
|
}
|
||||||
|
0
web/static/a
Normal file
0
web/static/a
Normal file
21
web/templates/index.html
Normal file
21
web/templates/index.html
Normal 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>
|
Loading…
Reference in New Issue
Block a user