brawlset/backend/main.go

58 lines
1.3 KiB
Go

package main
import (
"os"
"log"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/plugins/migratecmd"
_ "brawlset_server/migrations"
)
// Copied from https://github.com/s-petr/longhabit/blob/main/backend/main.go
// application holds the core application state and configuration.
type application struct {
pb *pocketbase.PocketBase
config appConfig
}
// appConfig holds the application's configuration settings.
type appConfig struct {
dbDir string
}
// newApplication creates and initializes a new application instance.
func newApplication() *application {
dbDir := getEnvOrDefault("DB_DIR", "pb_data")
return &application{
pb: pocketbase.NewWithConfig(pocketbase.Config{DefaultDataDir: dbDir}),
config: appConfig{
dbDir: dbDir,
},
}
}
func main() {
app := newApplication()
migratecmd.MustRegister(app.pb, app.pb.RootCmd, migratecmd.Config{
Dir: "migrations",
})
app.SetupCache()
app.SetupCrons()
app.SetupRoutes()
log.Fatal(app.pb.Start())
}
// getEnvOrDefault retrieves the value of an environment variable by key.
// If the environment variable is empty or not set, it returns the defaultValue.
func getEnvOrDefault(key, defaultValue string) string {
if value := os.Getenv(key); value != "" {
return value
}
return defaultValue
}