51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
numerus "dev.tandem.ws/tandem/numerus/pkg"
|
|
)
|
|
|
|
func main() {
|
|
db, err := numerus.NewDatabase(context.Background(), os.Getenv("NUMERUS_DATABASE_URL"))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
|
|
var demo bool
|
|
_ = db.QueryRow(context.Background(), "select database_is_numerus_demo()").Scan(&demo)
|
|
|
|
srv := http.Server{
|
|
Addr: ":8080",
|
|
Handler: numerus.NewRouter(db, demo),
|
|
ReadTimeout: 5 * time.Second,
|
|
WriteTimeout: 10 * time.Second,
|
|
IdleTimeout: 2 * time.Minute,
|
|
}
|
|
|
|
go func() {
|
|
log.Printf("INFO - listening on %s\n", srv.Addr)
|
|
if err := srv.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
|
|
log.Fatalf("http server: %v", err)
|
|
}
|
|
}()
|
|
|
|
sigCh := make(chan os.Signal, 1)
|
|
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
|
|
<-sigCh
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
log.Print("INFO - stopping server")
|
|
if err := srv.Shutdown(ctx); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|