mercury/web/routes.go

47 lines
1.4 KiB
Go

package web
import (
"git.sleepycat.moe/sam/mercury/internal/database"
"git.sleepycat.moe/sam/mercury/web/api"
"git.sleepycat.moe/sam/mercury/web/api/accounts"
"git.sleepycat.moe/sam/mercury/web/app"
"git.sleepycat.moe/sam/mercury/web/auth"
"git.sleepycat.moe/sam/mercury/web/frontend"
"github.com/go-chi/chi/v5"
)
func Routes(app *app.App) {
// auth
app.Router.Route("/auth", func(r chi.Router) {
auth := auth.New(app)
r.Get("/login", auth.GetLogin)
r.Post("/login", auth.PostLogin)
r.Get("/sign_up", auth.GetSignup)
r.Post("/sign_up", auth.PostSignup)
})
// web app handlers
// also assets
app.Router.Group(func(r chi.Router) {
frontend := frontend.New(app)
r.Use(app.FrontendAuth)
r.HandleFunc(frontend.AssetsPath(), frontend.ServeAssets)
r.HandleFunc("/static/*", frontend.ServeStaticAssets)
r.HandleFunc("/web", frontend.ServeFrontend)
r.HandleFunc("/web/*", frontend.ServeFrontend)
r.HandleFunc("/web/@{username}", frontend.ServeUser)
r.HandleFunc("/web/@{username}/posts/{postID}", frontend.ServeStatus)
})
// APIv1 handlers
app.Router.Route("/api/v1", func(r chi.Router) {
// account handlers
accounts := accounts.New(app)
r.With(app.APIAuth(database.TokenScopeAccountsRead, true)).
Get("/accounts/{accountID}", api.WrapHandlerT(accounts.GetID))
r.With(app.APIAuth(database.TokenScopeAccountsMe, false)).
Get("/accounts/@me", api.WrapHandlerT(accounts.GetMe))
})
}