2024-07-26 13:37:21 +02:00
|
|
|
package main
|
|
|
|
|
2024-08-21 16:35:44 +02:00
|
|
|
import (
|
2024-09-07 12:10:48 +02:00
|
|
|
"fmt"
|
2024-08-28 07:21:02 +02:00
|
|
|
"log/slog"
|
2024-08-21 16:35:44 +02:00
|
|
|
"net/http"
|
2024-08-28 07:21:02 +02:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2024-09-07 12:10:48 +02:00
|
|
|
"strconv"
|
2024-08-28 07:21:02 +02:00
|
|
|
"syscall"
|
2024-08-21 16:35:44 +02:00
|
|
|
)
|
|
|
|
|
2024-07-26 13:37:21 +02:00
|
|
|
func main() {
|
2024-09-09 16:36:03 +02:00
|
|
|
dbPath := os.Getenv("PLANNER_DB_PATH")
|
|
|
|
if dbPath == "" {
|
|
|
|
fmt.Println("PLANNER_DB_PATH is empty")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-09-07 12:10:48 +02:00
|
|
|
port, err := strconv.Atoi(os.Getenv("PLANNER_PORT"))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("PLANNER_PORT env is not an integer")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
apiKey := os.Getenv("PLANNER_API_KEY")
|
|
|
|
if apiKey == "" {
|
|
|
|
fmt.Println("PLANNER_API_KEY is empty")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-09-10 14:01:13 +02:00
|
|
|
crtPath := os.Getenv("PLANNER_CRT_PATH")
|
|
|
|
keyPath := os.Getenv("PLANNER_KEY_PATH")
|
2024-09-07 12:10:48 +02:00
|
|
|
|
2024-09-09 16:36:03 +02:00
|
|
|
repo, err := NewSqlite(dbPath)
|
2024-09-08 11:17:49 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("could not open sqlite db: %s", err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2024-08-28 07:21:02 +02:00
|
|
|
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
2024-08-21 16:35:44 +02:00
|
|
|
|
2024-09-10 14:01:13 +02:00
|
|
|
address := fmt.Sprintf(":%d", port)
|
|
|
|
srv := NewServer(repo, apiKey, logger)
|
|
|
|
if crtPath != "" || keyPath != "" {
|
|
|
|
go http.ListenAndServeTLS(address, crtPath, keyPath, srv)
|
|
|
|
} else {
|
|
|
|
go http.ListenAndServe(address, srv)
|
|
|
|
}
|
2024-08-21 16:35:44 +02:00
|
|
|
|
2024-08-28 07:21:02 +02:00
|
|
|
logger.Info("service started")
|
|
|
|
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
<-c
|
|
|
|
|
|
|
|
logger.Info("service stopped")
|
2024-07-26 13:37:21 +02:00
|
|
|
}
|