2023-06-16 07:42:50 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-04-01 09:09:54 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2023-06-16 07:42:50 +00:00
|
|
|
"github.com/joho/godotenv"
|
2023-06-20 14:10:46 +00:00
|
|
|
_ "github.com/lib/pq"
|
2024-02-10 16:47:14 +00:00
|
|
|
"holiday-api/api"
|
|
|
|
"holiday-api/db"
|
2023-06-20 14:10:46 +00:00
|
|
|
"holiday-api/migration"
|
2024-02-10 16:47:14 +00:00
|
|
|
"io/fs"
|
2024-04-01 09:09:54 +00:00
|
|
|
"log/slog"
|
2023-06-16 07:42:50 +00:00
|
|
|
"net/http"
|
2023-06-20 14:10:46 +00:00
|
|
|
"os"
|
2024-04-01 09:09:54 +00:00
|
|
|
"runtime/debug"
|
2023-06-20 14:10:46 +00:00
|
|
|
"strings"
|
2023-06-16 07:42:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
godotenv.Load()
|
2024-04-01 09:09:54 +00:00
|
|
|
if !hasProfile("dev") {
|
|
|
|
gin.SetMode(gin.ReleaseMode)
|
|
|
|
}
|
|
|
|
if value := os.Getenv("LOG_FORMAT"); value == "json" {
|
|
|
|
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{AddSource: true})))
|
|
|
|
}
|
2023-06-16 07:42:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2024-04-01 09:09:54 +00:00
|
|
|
commit, time := buildInfo()
|
|
|
|
slog.Info("build info", slog.String("commit", commit), slog.String("time", time))
|
|
|
|
|
|
|
|
client, err := connectToDb()
|
2023-06-16 07:42:50 +00:00
|
|
|
if err != nil {
|
2024-04-01 09:09:54 +00:00
|
|
|
slog.Error("couldn't connect to client", slog.String("err", err.Error()))
|
|
|
|
os.Exit(1)
|
2023-06-16 07:42:50 +00:00
|
|
|
}
|
2024-04-01 09:09:54 +00:00
|
|
|
if err := migration.InitializeMigrations(client, migrationFolder()); err != nil {
|
|
|
|
slog.Error("couldn't finish migration", slog.String("err", err.Error()))
|
|
|
|
os.Exit(1)
|
2023-06-16 07:42:50 +00:00
|
|
|
}
|
|
|
|
|
2024-04-01 09:09:54 +00:00
|
|
|
server := api.SetupServer(client)
|
2024-02-08 20:30:20 +00:00
|
|
|
|
2024-04-01 09:09:54 +00:00
|
|
|
port := ":" + getOrDefault("SERVER_PORT", "5281")
|
|
|
|
slog.Info("app is ready", slog.String("port", port))
|
|
|
|
if err := http.ListenAndServe(port, server); err != nil {
|
|
|
|
slog.Error("Couldn't start server!\n", slog.Any("err", err.Error()))
|
|
|
|
}
|
2023-06-23 17:31:02 +00:00
|
|
|
}
|
2023-06-20 14:10:46 +00:00
|
|
|
|
2024-02-10 16:47:14 +00:00
|
|
|
func hasProfile(value string) bool {
|
|
|
|
profileOptions := strings.Split(os.Getenv("PROFILE"), ",")
|
|
|
|
for _, option := range profileOptions {
|
|
|
|
if option == value {
|
|
|
|
return true
|
2023-06-20 14:10:46 +00:00
|
|
|
}
|
2024-02-10 16:47:14 +00:00
|
|
|
}
|
|
|
|
return false
|
2023-06-20 14:10:46 +00:00
|
|
|
}
|
2023-06-16 07:42:50 +00:00
|
|
|
|
2024-02-10 16:47:14 +00:00
|
|
|
func migrationFolder() fs.FS {
|
|
|
|
if hasProfile("dev") {
|
|
|
|
return db.DevMigrations
|
|
|
|
}
|
|
|
|
return db.ProdMigrations
|
2023-06-16 07:42:50 +00:00
|
|
|
}
|
2024-04-01 09:09:54 +00:00
|
|
|
|
|
|
|
func buildInfo() (string, string) {
|
|
|
|
revision := ""
|
|
|
|
time := ""
|
|
|
|
|
|
|
|
if info, ok := debug.ReadBuildInfo(); ok {
|
|
|
|
for _, setting := range info.Settings {
|
|
|
|
if setting.Key == "vcs.revision" {
|
|
|
|
revision = setting.Value
|
|
|
|
} else if setting.Key == "vcs.time" {
|
|
|
|
time = setting.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return revision, time
|
|
|
|
}
|
|
|
|
|
|
|
|
func getOrDefault(env string, defaultValue string) string {
|
|
|
|
if value, present := os.LookupEnv(env); present {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
return defaultValue
|
|
|
|
}
|