52 lines
933 B
Go
52 lines
933 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"net/http"
|
||
|
"os"
|
||
|
"os/signal"
|
||
|
|
||
|
"gitlab.com/1f320/pronouns/backend/log"
|
||
|
"gitlab.com/1f320/pronouns/backend/server"
|
||
|
|
||
|
"github.com/joho/godotenv"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
err := godotenv.Load()
|
||
|
if err != nil {
|
||
|
log.Fatalf("Error loading .env file: %v", err)
|
||
|
}
|
||
|
|
||
|
port := ":" + os.Getenv("PORT")
|
||
|
|
||
|
s, err := server.New()
|
||
|
if err != nil {
|
||
|
log.Fatalf("Error creating server: %v", err)
|
||
|
}
|
||
|
|
||
|
// mount api routes
|
||
|
mountRoutes(s)
|
||
|
|
||
|
e := make(chan error)
|
||
|
|
||
|
// run server in another goroutine (for gracefully shutting down, see below)
|
||
|
go func() {
|
||
|
e <- http.ListenAndServe(port, s.Router)
|
||
|
}()
|
||
|
|
||
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
|
||
|
defer stop()
|
||
|
|
||
|
log.Infof("API server running at %v!", port)
|
||
|
|
||
|
select {
|
||
|
case <-ctx.Done():
|
||
|
log.Info("Interrupt signal received, shutting down...")
|
||
|
s.DB.Close()
|
||
|
return
|
||
|
case err := <-e:
|
||
|
log.Fatalf("Error running server: %v", err)
|
||
|
}
|
||
|
}
|