46 lines
815 B
Go
46 lines
815 B
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
|
|
"powerwatts/internal/config"
|
|
"powerwatts/internal/server"
|
|
"powerwatts/internal/store"
|
|
)
|
|
|
|
func main() {
|
|
secret := os.Getenv("WRITE_SECRET")
|
|
addr := os.Getenv("ADDR")
|
|
chartMax := 0
|
|
|
|
if cfg, err := config.Load("config.json"); err != nil {
|
|
if !errors.Is(err, os.ErrNotExist) {
|
|
log.Fatalf("load config: %v", err)
|
|
}
|
|
} else {
|
|
if secret == "" {
|
|
secret = cfg.WriteSecret
|
|
}
|
|
if addr == "" {
|
|
addr = cfg.Addr
|
|
}
|
|
if cfg.ChartMaxWatts > 0 {
|
|
chartMax = cfg.ChartMaxWatts
|
|
}
|
|
}
|
|
|
|
if secret == "" {
|
|
log.Fatal("WRITE_SECRET is required")
|
|
}
|
|
if addr == "" {
|
|
addr = ":8080"
|
|
}
|
|
|
|
app := server.New(secret, chartMax, &store.Store{}, embeddedStatic)
|
|
log.Printf("listening on %s", addr)
|
|
log.Fatal(http.ListenAndServe(addr, app.Routes()))
|
|
}
|