2023-09-03 00:23:48 +02:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
2023-09-06 02:23:06 +02:00
|
|
|
"git.sleepycat.moe/sam/mercury/internal/database"
|
|
|
|
"git.sleepycat.moe/sam/mercury/web/api"
|
|
|
|
"git.sleepycat.moe/sam/mercury/web/api/accounts"
|
2023-09-06 02:40:50 +02:00
|
|
|
"git.sleepycat.moe/sam/mercury/web/api/blogs"
|
2023-09-06 16:32:33 +02:00
|
|
|
"git.sleepycat.moe/sam/mercury/web/api/posts"
|
2023-09-16 04:33:52 +02:00
|
|
|
"git.sleepycat.moe/sam/mercury/web/api/streaming"
|
2023-09-06 16:32:33 +02:00
|
|
|
"git.sleepycat.moe/sam/mercury/web/api/timelines"
|
2023-09-03 00:23:48 +02:00
|
|
|
"git.sleepycat.moe/sam/mercury/web/app"
|
2023-09-03 04:11:56 +02:00
|
|
|
"git.sleepycat.moe/sam/mercury/web/auth"
|
2023-09-03 00:23:48 +02:00
|
|
|
"git.sleepycat.moe/sam/mercury/web/frontend"
|
2023-10-16 15:45:43 +02:00
|
|
|
"git.sleepycat.moe/sam/mercury/web/wellknown"
|
2023-09-03 04:11:56 +02:00
|
|
|
"github.com/go-chi/chi/v5"
|
2023-09-03 00:23:48 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func Routes(app *app.App) {
|
2023-09-03 04:11:56 +02:00
|
|
|
// auth
|
|
|
|
app.Router.Route("/auth", func(r chi.Router) {
|
|
|
|
auth := auth.New(app)
|
|
|
|
r.Get("/login", auth.GetLogin)
|
2023-09-04 03:33:13 +02:00
|
|
|
r.Post("/login", auth.PostLogin)
|
|
|
|
|
|
|
|
r.Get("/sign_up", auth.GetSignup)
|
|
|
|
r.Post("/sign_up", auth.PostSignup)
|
2023-09-03 04:11:56 +02:00
|
|
|
})
|
|
|
|
|
2023-10-16 15:45:43 +02:00
|
|
|
// .well-known handlers
|
|
|
|
app.Router.Route("/.well-known", func(r chi.Router) {
|
|
|
|
wellknown := wellknown.New(app)
|
|
|
|
|
|
|
|
r.Get("/webfinger", api.WrapHandlerT(wellknown.WebFinger))
|
|
|
|
})
|
|
|
|
|
2023-09-06 16:32:33 +02:00
|
|
|
// APIv1 handlers
|
|
|
|
app.Router.Route("/api/v1", func(r chi.Router) {
|
|
|
|
unauthedAccess := !app.AppConfig.Security.RestrictAPI
|
|
|
|
unauthedTimelineAccess := app.AppConfig.Security.PublicTimelines && !app.AppConfig.Security.RestrictAPI
|
|
|
|
_ = unauthedTimelineAccess
|
|
|
|
|
|
|
|
// account handlers
|
|
|
|
accounts := accounts.New(app)
|
|
|
|
r.With(app.APIAuth(database.TokenScopeAccountsRead, unauthedAccess)).
|
|
|
|
Get("/accounts/{accountID}", api.WrapHandlerT(accounts.GetID))
|
|
|
|
r.With(app.APIAuth(database.TokenScopeAccountsMe, false)).
|
|
|
|
Get("/accounts/@me", api.WrapHandlerT(accounts.GetMe))
|
|
|
|
|
|
|
|
blogs := blogs.New(app)
|
|
|
|
r.With(app.APIAuth(database.TokenScopeBlogsRead, unauthedAccess)).
|
|
|
|
Get("/blogs/{blogID}", api.WrapHandlerT(blogs.GetID))
|
|
|
|
r.With(app.APIAuth(database.TokenScopeBlogsRead, unauthedAccess)).
|
|
|
|
Get("/blogs/lookup/{blogName}", api.WrapHandlerT(blogs.LookupName))
|
|
|
|
|
|
|
|
posts := posts.New(app)
|
|
|
|
r.With(app.APIAuth(database.TokenScopePostsRead, unauthedAccess)).
|
|
|
|
Get("/posts/{postID}", api.WrapHandlerT(posts.GetID))
|
|
|
|
r.With(app.APIAuth(database.TokenScopePostsWrite, false)).
|
|
|
|
Post("/blogs/{blogID}/posts", api.WrapHandlerT(posts.Create))
|
|
|
|
|
|
|
|
timelines := timelines.New(app)
|
|
|
|
r.With(app.APIAuth(database.TokenScopeTimeline, false)).
|
|
|
|
Get("/timelines/home", api.WrapHandlerT(timelines.Home))
|
|
|
|
|
2023-09-16 04:33:52 +02:00
|
|
|
streaming := streaming.New(app)
|
|
|
|
r.With(app.APIAuth(database.TokenScopeStreaming, false)).
|
|
|
|
Get("/streaming", api.WrapHandler(streaming.Streaming))
|
2023-09-06 16:32:33 +02:00
|
|
|
})
|
|
|
|
|
2023-09-03 04:11:56 +02:00
|
|
|
// web app handlers
|
|
|
|
// also assets
|
2023-09-04 03:33:13 +02:00
|
|
|
app.Router.Group(func(r chi.Router) {
|
|
|
|
frontend := frontend.New(app)
|
2023-09-04 17:32:45 +02:00
|
|
|
r.Use(app.FrontendAuth)
|
|
|
|
|
2023-09-04 03:33:13 +02:00
|
|
|
r.HandleFunc(frontend.AssetsPath(), frontend.ServeAssets)
|
|
|
|
r.HandleFunc("/static/*", frontend.ServeStaticAssets)
|
|
|
|
r.HandleFunc("/web", frontend.ServeFrontend)
|
|
|
|
r.HandleFunc("/web/*", frontend.ServeFrontend)
|
2023-09-04 17:32:45 +02:00
|
|
|
r.HandleFunc("/web/@{username}", frontend.ServeUser)
|
|
|
|
r.HandleFunc("/web/@{username}/posts/{postID}", frontend.ServeStatus)
|
2023-09-04 03:33:13 +02:00
|
|
|
})
|
2023-09-03 00:23:48 +02:00
|
|
|
}
|