main.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Package main is the entry point for the jamtrack application.
  2. package main
  3. import (
  4. "log"
  5. "os"
  6. "strings"
  7. "github.com/pocketbase/pocketbase"
  8. "github.com/pocketbase/pocketbase/core"
  9. "github.com/pocketbase/pocketbase/plugins/migratecmd"
  10. "github.com/pocketbase/pocketbase/tools/router"
  11. "code.osinet.fr/fgm/jamtrack/internal/web"
  12. _ "code.osinet.fr/fgm/jamtrack/migrations"
  13. )
  14. func main() {
  15. app := pocketbase.New()
  16. // Automigrate only when running via `go run` (development).
  17. isGoRun := strings.HasPrefix(os.Args[0], os.TempDir())
  18. migratecmd.MustRegister(app, app.RootCmd, migratecmd.Config{
  19. Automigrate: isGoRun,
  20. })
  21. // Block deletion of a location that still has jams referencing it.
  22. app.OnRecordDelete("locations").BindFunc(func(e *core.RecordEvent) error {
  23. jams, err := app.FindRecordsByFilter(
  24. "jams", "location={:loc}", "", 1, 0,
  25. map[string]any{"loc": e.Record.Id},
  26. )
  27. if err != nil {
  28. return err
  29. }
  30. if len(jams) > 0 {
  31. return router.NewBadRequestError("cannot delete a location that still has jams", nil)
  32. }
  33. return e.Next()
  34. })
  35. app.OnServe().BindFunc(func(se *core.ServeEvent) error {
  36. web.RegisterStatic(se)
  37. // Open routes (no auth required)
  38. se.Router.GET("/login", web.LoginGet)
  39. se.Router.POST("/login", web.LoginPost)
  40. se.Router.GET("/logout", web.Logout)
  41. // Protected routes — all behind requireAuth middleware
  42. protected := se.Router.Group("")
  43. protected.BindFunc(web.RequireAuth)
  44. protected.GET("/", web.Dashboard)
  45. protected.GET("/jams", web.JamList)
  46. protected.GET("/jams/new", web.JamNew)
  47. protected.POST("/jams", web.JamCreate)
  48. protected.GET("/jams/{id}", web.JamDetail)
  49. protected.GET("/locations", web.LocationList)
  50. protected.POST("/locations", web.LocationCreate)
  51. protected.GET("/locations/{id}", web.LocationDetail)
  52. protected.GET("/songs/search", web.SongSearch)
  53. protected.POST("/jams/{id}/songs", web.SetlistAdd)
  54. protected.GET("/setlist/{id}/view", web.SetlistView)
  55. protected.GET("/setlist/{id}/edit", web.SetlistEditForm)
  56. protected.PATCH("/setlist/{id}", web.SetlistUpdate)
  57. protected.PATCH("/setlist/{id}/played", web.SetlistToggle)
  58. protected.DELETE("/setlist/{id}", web.SetlistDelete)
  59. return se.Next()
  60. })
  61. if err := app.Start(); err != nil {
  62. log.Fatal(err)
  63. }
  64. }