owl-blogs/web/app.go

116 lines
3.8 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-17 18:44:59 +00:00
"owl-blogs/app/repository"
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-17 18:44:59 +00:00
FiberApp *fiber.App
EntryService *app.EntryService
BinaryService *app.BinaryService
Registry *app.EntryTypeRegistry
AuthorService *app.AuthorService
SiteConfigRepo repository.ConfigRepository
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,
2023-07-19 18:45:42 +00:00
configRepo repository.ConfigRepository,
2023-07-08 11:28:06 +00:00
) *WebApp {
2023-06-25 19:32:36 +00:00
app := fiber.New()
2023-07-19 18:45:42 +00:00
indexHandler := NewIndexHandler(entryService, configRepo)
listHandler := NewListHandler(entryService, configRepo)
entryHandler := NewEntryHandler(entryService, typeRegistry, authorService, configRepo)
2023-07-08 12:32:20 +00:00
mediaHandler := NewMediaHandler(binService)
2023-06-25 19:32:36 +00:00
rssHandler := NewRSSHandler(entryService)
2023-07-19 18:45:42 +00:00
loginHandler := NewLoginHandler(authorService, configRepo)
editorListHandler := NewEditorListHandler(typeRegistry, configRepo)
editorHandler := NewEditorHandler(entryService, typeRegistry, binService, configRepo)
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-17 18:44:59 +00:00
// SiteConfig
siteConfig := app.Group("/site-config")
siteConfig.Use(middleware.NewAuthMiddleware(authorService).Handle)
2023-07-17 19:43:31 +00:00
2023-07-19 18:45:42 +00:00
siteConfigHandler := NewSiteConfigHandler(configRepo)
2023-07-17 19:43:31 +00:00
siteConfig.Get("/", siteConfigHandler.HandleGet)
siteConfig.Post("/", siteConfigHandler.HandlePost)
2023-07-19 18:45:42 +00:00
siteConfigMeHandler := NewSiteConfigMeHandler(configRepo)
2023-07-17 19:43:31 +00:00
siteConfig.Get("/me", siteConfigMeHandler.HandleGet)
siteConfig.Post("/me/create/", siteConfigMeHandler.HandleCreate)
siteConfig.Post("/me/delete/", siteConfigMeHandler.HandleDelete)
2023-07-19 18:45:42 +00:00
siteConfigListHandler := NewSiteConfigListHandler(configRepo, typeRegistry)
2023-07-17 19:43:31 +00:00
siteConfig.Get("/lists", siteConfigListHandler.HandleGet)
siteConfig.Post("/lists/create/", siteConfigListHandler.HandleCreate)
siteConfig.Post("/lists/delete/", siteConfigListHandler.HandleDelete)
2023-07-17 18:44:59 +00:00
2023-07-19 18:45:42 +00:00
siteConfigMenusHandler := NewSiteConfigMenusHandler(configRepo)
2023-07-18 18:09:45 +00:00
siteConfig.Get("/menus", siteConfigMenusHandler.HandleGet)
siteConfig.Post("/menus/create/", siteConfigMenusHandler.HandleCreate)
siteConfig.Post("/menus/delete/", siteConfigMenusHandler.HandleDelete)
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{
2023-07-17 18:44:59 +00:00
FiberApp: app,
EntryService: entryService,
Registry: typeRegistry,
BinaryService: binService,
AuthorService: authorService,
2023-07-19 18:45:42 +00:00
SiteConfigRepo: configRepo,
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
}