owl-blogs/web/app.go

89 lines
2.5 KiB
Go
Raw Normal View History

2023-06-25 19:32:36 +00:00
package web
import (
2023-07-13 19:20:00 +00:00
"embed"
"net/http"
2023-06-25 19:32:36 +00:00
"owl-blogs/app"
2023-07-08 11:28:06 +00:00
"owl-blogs/web/middleware"
2023-06-25 19:32:36 +00:00
"github.com/gofiber/fiber/v2"
2023-07-13 19:20:00 +00:00
"github.com/gofiber/fiber/v2/middleware/filesystem"
2023-06-25 19:32:36 +00:00
)
2023-07-13 19:20:00 +00:00
//go:embed static/*
var embedDirStatic embed.FS
2023-06-25 19:32:36 +00:00
type WebApp struct {
2023-07-08 09:08:55 +00:00
FiberApp *fiber.App
EntryService *app.EntryService
BinaryService *app.BinaryService
Registry *app.EntryTypeRegistry
2023-07-08 11:28:06 +00:00
AuthorService *app.AuthorService
2023-06-25 19:32:36 +00:00
}
2023-07-08 11:28:06 +00:00
func NewWebApp(
entryService *app.EntryService,
typeRegistry *app.EntryTypeRegistry,
binService *app.BinaryService,
authorService *app.AuthorService,
) *WebApp {
2023-06-25 19:32:36 +00:00
app := fiber.New()
indexHandler := NewIndexHandler(entryService)
listHandler := NewListHandler(entryService)
2023-07-13 19:20:00 +00:00
entryHandler := NewEntryHandler(entryService, typeRegistry, authorService)
2023-07-08 12:32:20 +00:00
mediaHandler := NewMediaHandler(binService)
2023-06-25 19:32:36 +00:00
rssHandler := NewRSSHandler(entryService)
2023-07-08 12:28:15 +00:00
loginHandler := NewLoginHandler(authorService)
2023-07-06 20:16:52 +00:00
editorListHandler := NewEditorListHandler(typeRegistry)
2023-07-08 10:03:10 +00:00
editorHandler := NewEditorHandler(entryService, typeRegistry, binService)
2023-06-25 19:32:36 +00:00
2023-07-08 11:28:06 +00:00
// Login
app.Get("/auth/login", loginHandler.HandleGet)
app.Post("/auth/login", loginHandler.HandlePost)
// Editor
editor := app.Group("/editor")
editor.Use(middleware.NewAuthMiddleware(authorService).Handle)
editor.Get("/", editorListHandler.Handle)
editor.Get("/:editor/", editorHandler.HandleGet)
editor.Post("/:editor/", editorHandler.HandlePost)
2023-07-13 19:20:00 +00:00
// app.Static("/static/*filepath", http.Dir(repo.StaticDir()))
app.Use("/static", filesystem.New(filesystem.Config{
Root: http.FS(embedDirStatic),
PathPrefix: "static",
Browse: false,
}))
2023-06-25 19:32:36 +00:00
app.Get("/", indexHandler.Handle)
app.Get("/lists/:list/", listHandler.Handle)
// Media
2023-07-09 17:09:54 +00:00
app.Get("/media/+", mediaHandler.Handle)
2023-06-25 19:32:36 +00:00
// RSS
app.Get("/index.xml", rssHandler.Handle)
// Posts
app.Get("/posts/:post/", entryHandler.Handle)
// Webmention
// app.Post("/webmention/", userWebmentionHandler(repo))
// Micropub
// app.Post("/micropub/", userMicropubHandler(repo))
// IndieAuth
// app.Get("/auth/", userAuthHandler(repo))
// app.Post("/auth/", userAuthProfileHandler(repo))
// app.Post("/auth/verify/", userAuthVerifyHandler(repo))
// app.Post("/auth/token/", userAuthTokenHandler(repo))
// app.Get("/.well-known/oauth-authorization-server", userAuthMetadataHandler(repo))
// app.NotFound = http.HandlerFunc(notFoundHandler(repo))
2023-07-08 09:08:55 +00:00
return &WebApp{
FiberApp: app,
EntryService: entryService,
Registry: typeRegistry,
BinaryService: binService,
2023-07-08 11:28:06 +00:00
AuthorService: authorService,
2023-07-08 09:08:55 +00:00
}
2023-06-25 19:32:36 +00:00
}
func (w *WebApp) Run() {
2023-07-06 20:16:52 +00:00
w.FiberApp.Listen(":3000")
2023-06-25 19:32:36 +00:00
}