2022-07-13 17:01:09 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.netflux.io/rob/solar-toolkit/gateway/handler"
|
|
|
|
"git.netflux.io/rob/solar-toolkit/gateway/store"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
)
|
|
|
|
|
|
|
|
const defaultBindAddr = ":8888"
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
databaseURL := os.Getenv("DATABASE_URL")
|
|
|
|
if databaseURL == "" {
|
|
|
|
log.Fatal("missing configuration DATABASE_URL")
|
|
|
|
}
|
|
|
|
|
|
|
|
bindAddr := os.Getenv("BIND_ADDR")
|
|
|
|
if bindAddr == "" {
|
|
|
|
bindAddr = defaultBindAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
db, err := sqlx.Connect("postgres", databaseURL)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not connect to database: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
store := store.NewSQL(db)
|
2022-07-13 17:01:09 +00:00
|
|
|
handler := handler.New(store)
|
2022-07-13 17:01:09 +00:00
|
|
|
srv := http.Server{
|
|
|
|
ReadTimeout: time.Second * 3,
|
|
|
|
WriteTimeout: time.Second * 3,
|
|
|
|
Handler: handler,
|
|
|
|
Addr: bindAddr,
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Listening on %s...", bindAddr)
|
|
|
|
if err := srv.ListenAndServe(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|