jamtrack/main.go

79 lines
2.2 KiB
Go

// Package main is the entry point for the jamtrack application.
package main
import (
"log"
"os"
"strings"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/plugins/migratecmd"
"github.com/pocketbase/pocketbase/tools/router"
"code.osinet.fr/fgm/jamtrack/internal/web"
_ "code.osinet.fr/fgm/jamtrack/migrations"
)
func main() {
app := pocketbase.New()
// Automigrate only when running via `go run` (development).
isGoRun := strings.HasPrefix(os.Args[0], os.TempDir())
migratecmd.MustRegister(app, app.RootCmd, migratecmd.Config{
Automigrate: isGoRun,
})
// Block deletion of a location that still has jams referencing it.
app.OnRecordDelete("locations").BindFunc(func(e *core.RecordEvent) error {
jams, err := app.FindRecordsByFilter(
"jams", "location={:loc}", "", 1, 0,
map[string]any{"loc": e.Record.Id},
)
if err != nil {
return err
}
if len(jams) > 0 {
return router.NewBadRequestError("cannot delete a location that still has jams", nil)
}
return e.Next()
})
app.OnServe().BindFunc(func(se *core.ServeEvent) error {
web.RegisterStatic(se)
// Open routes (no auth required)
se.Router.GET("/login", web.LoginGet)
se.Router.POST("/login", web.LoginPost)
se.Router.GET("/logout", web.Logout)
// Protected routes — all behind requireAuth middleware
protected := se.Router.Group("")
protected.BindFunc(web.RequireAuth)
protected.GET("/", web.Dashboard)
protected.GET("/jams", web.JamList)
protected.GET("/jams/new", web.JamNew)
protected.POST("/jams", web.JamCreate)
protected.GET("/jams/{id}", web.JamDetail)
protected.GET("/locations", web.LocationList)
protected.POST("/locations", web.LocationCreate)
protected.GET("/locations/{id}", web.LocationDetail)
protected.GET("/songs/search", web.SongSearch)
protected.POST("/jams/{id}/songs", web.SetlistAdd)
protected.GET("/setlist/{id}/view", web.SetlistView)
protected.GET("/setlist/{id}/edit", web.SetlistEditForm)
protected.PATCH("/setlist/{id}", web.SetlistUpdate)
protected.PATCH("/setlist/{id}/played", web.SetlistToggle)
protected.DELETE("/setlist/{id}", web.SetlistDelete)
return se.Next()
})
if err := app.Start(); err != nil {
log.Fatal(err)
}
}