owl-blogs/web/app.go

150 lines
5.2 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"
2023-07-25 17:05:55 +00:00
"fmt"
2023-07-13 19:20:00 +00:00
"net/http"
2023-07-25 17:05:55 +00:00
"net/url"
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-25 17:05:55 +00:00
"owl-blogs/config"
"owl-blogs/domain/model"
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-22 19:12:53 +00:00
configRegister *app.ConfigRegister,
2023-07-08 11:28:06 +00:00
) *WebApp {
2023-06-25 19:32:36 +00:00
app := fiber.New()
2023-07-25 19:31:03 +00:00
app.Use(middleware.NewUserMiddleware(authorService).Handle)
2023-06-25 19:32:36 +00:00
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-07-20 17:49:52 +00:00
rssHandler := NewRSSHandler(entryService, configRepo)
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)
2023-07-22 19:12:53 +00:00
// admin
adminHandler := NewAdminHandler(configRepo, configRegister)
admin := app.Group("/admin")
admin.Use(middleware.NewAuthMiddleware(authorService).Handle)
admin.Get("/", adminHandler.Handle)
admin.Get("/config/:config/", adminHandler.HandleConfigGet)
admin.Post("/config/:config/", adminHandler.HandleConfigPost)
2023-07-08 11:28:06 +00:00
// Editor
editor := app.Group("/editor")
editor.Use(middleware.NewAuthMiddleware(authorService).Handle)
editor.Get("/", editorListHandler.Handle)
2023-07-25 19:31:03 +00:00
editor.Get("/new/:editor/", editorHandler.HandleGetNew)
editor.Post("/new/:editor/", editorHandler.HandlePostNew)
editor.Get("/edit/:id/", editorHandler.HandleGetEdit)
editor.Post("/edit/:id/", editorHandler.HandlePostEdit)
2023-07-25 19:49:09 +00:00
editor.Post("/delete/:id/", editorHandler.HandlePostDelete)
2023-07-08 11:28:06 +00:00
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)
2023-07-25 16:59:25 +00:00
// robots.txt
app.Get("/robots.txt", func(c *fiber.Ctx) error {
2023-07-25 17:05:55 +00:00
siteConfig := model.SiteConfig{}
configRepo.Get(config.SITE_CONFIG, &siteConfig)
sitemapUrl, _ := url.JoinPath(siteConfig.FullUrl, "/sitemap.xml")
2023-07-25 16:59:25 +00:00
c.Set("Content-Type", "text/plain")
2023-07-25 17:05:55 +00:00
return c.SendString(fmt.Sprintf("User-agent: *\nAllow: /\n\nSitemap: %s\n", sitemapUrl))
2023-07-25 16:59:25 +00:00
})
2023-07-25 17:05:55 +00:00
// sitemap.xml
app.Get("/sitemap.xml", NewSiteMapHandler(entryService, configRepo).Handle)
2023-07-22 18:34:17 +00:00
// ActivityPub
2023-07-26 19:28:57 +00:00
activityPubServer := NewActivityPubServer(configRepo, entryService)
2023-07-22 19:12:53 +00:00
configRegister.Register(ACT_PUB_CONF_NAME, &ActivityPubConfig{})
2023-07-26 19:28:57 +00:00
app.Get("/.well-known/webfinger", activityPubServer.HandleWebfinger)
app.Route("/activitypub", activityPubServer.Router)
2023-07-22 18:34:17 +00:00
2023-06-25 19:32:36 +00:00
// 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
}