mirror of
https://github.com/LeRoid-hub/humiditycalc.git
synced 2025-01-31 03:34:56 +00:00
fixes + Dockerfile
This commit is contained in:
parent
c0049aa393
commit
1f2e2bd221
12
Dockerfile
Normal file
12
Dockerfile
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
FROM golang:1.22.2
|
||||||
|
|
||||||
|
WORKDIR /usr/src/app
|
||||||
|
|
||||||
|
# pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change
|
||||||
|
COPY go.mod ./
|
||||||
|
RUN go mod download && go mod verify
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
RUN go build -v -o /usr/local/bin/humiditycalc ./...
|
||||||
|
|
||||||
|
CMD ["humiditycalc"]
|
58
configs/config.go
Normal file
58
configs/config.go
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
package configs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/joho/godotenv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Load() map[string]string {
|
||||||
|
var env map[string]string = make(map[string]string)
|
||||||
|
|
||||||
|
envpath := "./.env"
|
||||||
|
|
||||||
|
if _, err := os.Stat(envpath); err == nil {
|
||||||
|
|
||||||
|
dotenv, err := godotenv.Read(envpath)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error loading .env file: ", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
env = dotenv
|
||||||
|
} else {
|
||||||
|
fmt.Println("No .env file found", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
mode := os.Getenv("MODE")
|
||||||
|
if mode != "" {
|
||||||
|
env["MODE"] = os.Getenv("MODE")
|
||||||
|
}
|
||||||
|
|
||||||
|
openweathermapAPIKey := os.Getenv("OPENWEATHERMAP_API_KEY")
|
||||||
|
if openweathermapAPIKey != "" {
|
||||||
|
env["OPENWEATHERMAP_API_KEY"] = openweathermapAPIKey
|
||||||
|
}
|
||||||
|
|
||||||
|
latitude := os.Getenv("LATITUDE")
|
||||||
|
if latitude != "" {
|
||||||
|
env["LATITUDE"] = latitude
|
||||||
|
}
|
||||||
|
|
||||||
|
longitude := os.Getenv("LONGITUDE")
|
||||||
|
if longitude != "" {
|
||||||
|
env["LONGITUDE"] = longitude
|
||||||
|
}
|
||||||
|
|
||||||
|
port := os.Getenv("PORT")
|
||||||
|
if port != "" {
|
||||||
|
env["PORT"] = port
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(env) == 0 {
|
||||||
|
fmt.Println("no environment variables are set")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
return env
|
||||||
|
}
|
18
main.go
18
main.go
@ -5,12 +5,12 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/LeRoid-hub/humiditycalc/configs"
|
||||||
"github.com/LeRoid-hub/humiditycalc/server"
|
"github.com/LeRoid-hub/humiditycalc/server"
|
||||||
"github.com/joho/godotenv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
env := loadEnv()
|
env := configs.Load()
|
||||||
if val, ok := env["MODE"]; ok {
|
if val, ok := env["MODE"]; ok {
|
||||||
if strings.ToLower(val) == "both" {
|
if strings.ToLower(val) == "both" {
|
||||||
checkEnv(env)
|
checkEnv(env)
|
||||||
@ -28,20 +28,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadEnv() map[string]string {
|
|
||||||
var env map[string]string
|
|
||||||
env, err := godotenv.Read()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error loading .env file: ", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
if len(env) == 0 {
|
|
||||||
fmt.Println(".env file is empty")
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
return env
|
|
||||||
}
|
|
||||||
|
|
||||||
func checkEnv(env map[string]string) {
|
func checkEnv(env map[string]string) {
|
||||||
// Is there an API key for openweathermap?
|
// Is there an API key for openweathermap?
|
||||||
if val, ok := env["OPENWEATHERMAP_API_KEY"]; ok {
|
if val, ok := env["OPENWEATHERMAP_API_KEY"]; ok {
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -13,7 +12,6 @@ type WeatherCache struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *WeatherCache) IsExpired() bool {
|
func (w *WeatherCache) IsExpired() bool {
|
||||||
fmt.Println(time.Since(w.timestamp), time.Duration(w.duration)*time.Second)
|
|
||||||
return time.Since(w.timestamp) > time.Duration(w.duration)*time.Second
|
return time.Since(w.timestamp) > time.Duration(w.duration)*time.Second
|
||||||
}
|
}
|
||||||
|
|
||||||
|
4
models/env.go
Normal file
4
models/env.go
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
}
|
@ -61,17 +61,14 @@ func Run(env map[string]string) {
|
|||||||
// Get Weather data
|
// Get Weather data
|
||||||
humidity, temperature := internal.Weather(env)
|
humidity, temperature := internal.Weather(env)
|
||||||
|
|
||||||
// Calculate absolute weather humidity
|
|
||||||
absoluteWeatherHumidity = internal.AbsoluteHumidity(temperature, humidity)
|
|
||||||
|
|
||||||
// Update cache
|
// Update cache
|
||||||
cacheWeather.SetData(humidity, temperature)
|
cacheWeather.SetData(humidity, temperature)
|
||||||
} else {
|
|
||||||
// Use cached data
|
|
||||||
humidity, temperature := cacheWeather.GetData()
|
|
||||||
absoluteWeatherHumidity = internal.AbsoluteHumidity(temperature, humidity)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Use cached data
|
||||||
|
humidity, temperature = cacheWeather.GetData()
|
||||||
|
absoluteWeatherHumidity = internal.AbsoluteHumidity(temperature, humidity)
|
||||||
|
|
||||||
// Create response
|
// Create response
|
||||||
WData := WeatherData{Temperature: FormatFloat(temperature, 2), RelativeHumidity: FormatFloat(humidity, 4), AbsoluteHumidity: FormatFloat(absoluteWeatherHumidity, 4)}
|
WData := WeatherData{Temperature: FormatFloat(temperature, 2), RelativeHumidity: FormatFloat(humidity, 4), AbsoluteHumidity: FormatFloat(absoluteWeatherHumidity, 4)}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user