humiditycalc/server/server.go

101 lines
2.6 KiB
Go
Raw Permalink Normal View History

2024-10-26 14:31:51 +00:00
package server
import (
"fmt"
2024-10-26 17:53:38 +00:00
"html/template"
2024-10-26 14:31:51 +00:00
"net/http"
"strconv"
"strings"
2024-10-26 14:31:51 +00:00
"github.com/LeRoid-hub/humiditycalc/internal"
"github.com/LeRoid-hub/humiditycalc/models"
2024-10-26 14:31:51 +00:00
)
2024-10-26 17:53:38 +00:00
type result struct {
AbsoluteHumidity string
WeatherData WeatherData
}
type WeatherData struct {
Temperature string
RelativeHumidity string
AbsoluteHumidity string
2024-10-26 17:53:38 +00:00
}
2024-11-02 12:24:36 +00:00
// Run starts the HTTP server.
func Run(env map[string]string) {
// Cache weather data
var cacheWeather = models.NewWeatherCache()
// Load HTML template
2024-10-26 17:53:38 +00:00
tmpl := template.Must(template.ParseFiles("./web/templates/index.html"))
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 == "" {
go func(cacheW **models.WeatherCache) {
humidity, temperature := internal.Weather(env)
(*cacheW).SetData(humidity, temperature)
}(&cacheWeather)
2024-10-26 17:53:38 +00:00
tmpl.Execute(w, nil)
return
}
2024-12-05 21:12:57 +00:00
tempCelsius = strings.Replace(tempCelsius, ",", ".", 1)
relativeHumidity = strings.Replace(relativeHumidity, ",", ".", 1)
2024-10-26 17:53:38 +00:00
// 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)
var absoluteWeatherHumidity float64
var temperature, humidity float64
// Check if cache is expired or empty
if cacheWeather.IsExpired() || cacheWeather.Humidity == 0 || cacheWeather.Temperature == 0 {
// Get Weather data
humidity, temperature := internal.Weather(env)
// Update cache
cacheWeather.SetData(humidity, temperature)
}
2024-11-02 18:11:56 +00:00
// Use cached data
humidity, temperature = cacheWeather.GetData()
absoluteWeatherHumidity = internal.AbsoluteHumidity(temperature, humidity)
// Create response
WData := WeatherData{Temperature: FormatFloat(temperature, 2), RelativeHumidity: FormatFloat(humidity, 4), AbsoluteHumidity: FormatFloat(absoluteWeatherHumidity, 4)}
re := result{AbsoluteHumidity: FormatFloat(absoluteHumidity, 4), WeatherData: WData}
2024-10-26 17:53:38 +00:00
// Write response
tmpl.Execute(w, re)
2024-10-26 17:53:38 +00:00
})
2024-10-26 14:31:51 +00:00
http.ListenAndServe(":8080", nil)
}
func FormatFloat(num float64, prc int) string {
var (
zero, dot = "0", "."
str = fmt.Sprintf("%."+strconv.Itoa(prc)+"f", num)
)
return strings.TrimRight(strings.TrimRight(str, zero), dot)
}