2023-06-16 07:42:50 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
2023-06-16 07:42:50 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2023-06-20 14:10:46 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2023-06-16 07:42:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
godotenv.Load()
|
|
|
|
log.SetPrefix("")
|
|
|
|
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2024-02-10 16:47:14 +00:00
|
|
|
db, err := connectToDb()
|
2023-06-16 07:42:50 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("couldn't connect to db: %v", err)
|
|
|
|
}
|
2024-02-10 16:47:14 +00:00
|
|
|
if err := migration.InitializeMigrations(db, migrationFolder()); err != nil {
|
2023-06-16 07:42:50 +00:00
|
|
|
log.Fatalf("couldn't execute migrations: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-02-10 16:47:14 +00:00
|
|
|
server := api.SetupServer(db)
|
2024-02-08 20:30:20 +00:00
|
|
|
|
2024-02-10 16:47:14 +00:00
|
|
|
log.Fatal(http.ListenAndServe(":5281", server))
|
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
|
|
|
}
|