2023-12-16 12:11:16 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-12-17 11:58:33 +01:00
|
|
|
"flag"
|
2023-12-16 12:11:16 +01:00
|
|
|
"fmt"
|
|
|
|
"log/slog"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
2023-12-22 15:50:42 +01:00
|
|
|
"ewintr.nl/emdb/cmd/api-service/server"
|
2023-12-16 12:11:16 +01:00
|
|
|
)
|
|
|
|
|
2023-12-17 11:58:33 +01:00
|
|
|
var (
|
|
|
|
port = flag.Int("port", 8080, "port to listen on")
|
|
|
|
dbPath = flag.String("dbpath", "test.db", "path to sqlite db")
|
|
|
|
apiKey = flag.String("apikey", "hoi", "api key to use")
|
|
|
|
)
|
|
|
|
|
2023-12-16 12:11:16 +01:00
|
|
|
func main() {
|
2023-12-17 11:58:33 +01:00
|
|
|
flag.Parse()
|
2023-12-16 12:11:16 +01:00
|
|
|
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
2023-12-17 12:21:32 +01:00
|
|
|
logger.Info("starting server", "port", *port, "dbPath", *dbPath)
|
|
|
|
|
2023-12-21 15:15:51 +01:00
|
|
|
repo, err := server.NewSQLite(*dbPath)
|
2023-12-16 14:45:38 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("could not create new sqlite repo: %s", err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2023-12-16 12:11:16 +01:00
|
|
|
|
2023-12-21 15:15:51 +01:00
|
|
|
apis := server.APIIndex{
|
|
|
|
"movie": server.NewMovieAPI(repo, logger),
|
2023-12-16 14:45:38 +01:00
|
|
|
}
|
2023-12-16 12:11:16 +01:00
|
|
|
|
2023-12-21 15:15:51 +01:00
|
|
|
go http.ListenAndServe(fmt.Sprintf(":%d", *port), server.NewServer(*apiKey, apis, logger))
|
2023-12-17 12:21:32 +01:00
|
|
|
logger.Info("server started")
|
2023-12-16 12:11:16 +01:00
|
|
|
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
<-c
|
2023-12-17 12:21:32 +01:00
|
|
|
|
|
|
|
logger.Info("server stopped")
|
2023-12-16 12:11:16 +01:00
|
|
|
}
|