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"
|
2024-05-13 17:03:21 +00:00
|
|
|
"owl-blogs/config"
|
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
|
2023-07-19 18:25:42 +00:00
|
|
|
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,
|
2024-02-25 13:01:55 +00:00
|
|
|
siteConfigService *app.SiteConfigService,
|
2023-08-08 18:17:04 +00:00
|
|
|
webmentionService *app.WebmentionService,
|
2023-08-09 18:36:44 +00:00
|
|
|
interactionRepo repository.InteractionRepository,
|
2024-05-13 17:03:21 +00:00
|
|
|
apService *app.ActivityPubService,
|
2023-07-08 11:28:06 +00:00
|
|
|
) *WebApp {
|
2024-05-13 17:03:21 +00:00
|
|
|
fiberApp := fiber.New(fiber.Config{
|
2024-05-18 19:59:32 +00:00
|
|
|
BodyLimit: 50 * 1024 * 1024, // 50MB in bytes
|
|
|
|
DisableStartupMessage: true,
|
2024-05-09 08:47:10 +00:00
|
|
|
})
|
2024-05-13 17:03:21 +00:00
|
|
|
fiberApp.Use(middleware.NewUserMiddleware(authorService).Handle)
|
2023-06-25 19:32:36 +00:00
|
|
|
|
2024-02-25 13:01:55 +00:00
|
|
|
indexHandler := NewIndexHandler(entryService, siteConfigService)
|
|
|
|
listHandler := NewListHandler(entryService, siteConfigService)
|
2023-08-09 18:36:44 +00:00
|
|
|
entryHandler := NewEntryHandler(entryService, typeRegistry, authorService, configRepo, interactionRepo)
|
2023-07-08 12:32:20 +00:00
|
|
|
mediaHandler := NewMediaHandler(binService)
|
2024-02-25 13:01:55 +00:00
|
|
|
rssHandler := NewRSSHandler(entryService, siteConfigService)
|
2023-07-19 18:45:42 +00:00
|
|
|
loginHandler := NewLoginHandler(authorService, configRepo)
|
|
|
|
editorHandler := NewEditorHandler(entryService, typeRegistry, binService, configRepo)
|
2023-08-08 19:32:24 +00:00
|
|
|
webmentionHandler := NewWebmentionHandler(webmentionService, configRepo)
|
2023-06-25 19:32:36 +00:00
|
|
|
|
2023-07-08 11:28:06 +00:00
|
|
|
// Login
|
2024-05-13 17:03:21 +00:00
|
|
|
fiberApp.Get("/auth/login", loginHandler.HandleGet)
|
|
|
|
fiberApp.Post("/auth/login", loginHandler.HandlePost)
|
2023-07-08 11:28:06 +00:00
|
|
|
|
2023-07-22 19:12:53 +00:00
|
|
|
// admin
|
2023-07-27 19:18:09 +00:00
|
|
|
adminHandler := NewAdminHandler(configRepo, configRegister, typeRegistry)
|
2024-02-25 13:01:55 +00:00
|
|
|
draftHandler := NewDraftHandler(entryService, siteConfigService)
|
2023-07-27 20:02:13 +00:00
|
|
|
binaryManageHandler := NewBinaryManageHandler(configRepo, binService)
|
2023-08-09 18:48:16 +00:00
|
|
|
adminInteractionHandler := NewAdminInteractionHandler(configRepo, interactionRepo)
|
2024-05-13 17:03:21 +00:00
|
|
|
admin := fiberApp.Group("/admin")
|
2023-07-22 19:12:53 +00:00
|
|
|
admin.Use(middleware.NewAuthMiddleware(authorService).Handle)
|
|
|
|
admin.Get("/", adminHandler.Handle)
|
2023-08-05 11:35:56 +00:00
|
|
|
admin.Get("/drafts/", draftHandler.Handle)
|
2023-07-22 19:12:53 +00:00
|
|
|
admin.Get("/config/:config/", adminHandler.HandleConfigGet)
|
|
|
|
admin.Post("/config/:config/", adminHandler.HandleConfigPost)
|
2023-07-27 20:02:13 +00:00
|
|
|
admin.Get("/binaries/", binaryManageHandler.Handle)
|
|
|
|
admin.Post("/binaries/new/", binaryManageHandler.HandleUpload)
|
|
|
|
admin.Post("/binaries/delete", binaryManageHandler.HandleDelete)
|
2023-08-09 18:48:16 +00:00
|
|
|
admin.Post("/interactions/:id/delete/", adminInteractionHandler.HandleDelete)
|
2024-02-24 18:53:49 +00:00
|
|
|
admin.Get("/interactions/", adminInteractionHandler.HandleGet)
|
2023-07-22 19:12:53 +00:00
|
|
|
|
2024-02-21 20:21:58 +00:00
|
|
|
adminApi := admin.Group("/api")
|
|
|
|
adminApi.Post("/binaries", binaryManageHandler.HandleUploadApi)
|
|
|
|
|
2023-07-08 11:28:06 +00:00
|
|
|
// Editor
|
2024-05-13 17:03:21 +00:00
|
|
|
editor := fiberApp.Group("/editor")
|
2023-07-08 11:28:06 +00:00
|
|
|
editor.Use(middleware.NewAuthMiddleware(authorService).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-30 09:29:30 +00:00
|
|
|
editor.Post("/unpublish/:id/", editorHandler.HandlePostUnpublish)
|
2023-07-08 11:28:06 +00:00
|
|
|
|
2023-07-17 18:44:59 +00:00
|
|
|
// SiteConfig
|
2024-05-13 17:03:21 +00:00
|
|
|
siteConfig := fiberApp.Group("/site-config")
|
2023-07-17 18:44:59 +00:00
|
|
|
siteConfig.Use(middleware.NewAuthMiddleware(authorService).Handle)
|
2023-07-17 19:43:31 +00:00
|
|
|
|
2024-02-25 13:01:55 +00:00
|
|
|
siteConfigHandler := NewSiteConfigHandler(siteConfigService)
|
2023-07-17 19:43:31 +00:00
|
|
|
siteConfig.Get("/", siteConfigHandler.HandleGet)
|
|
|
|
siteConfig.Post("/", siteConfigHandler.HandlePost)
|
|
|
|
|
2024-02-25 13:01:55 +00:00
|
|
|
siteConfigMeHandler := NewSiteConfigMeHandler(siteConfigService)
|
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)
|
|
|
|
|
2024-02-25 13:01:55 +00:00
|
|
|
siteConfigListHandler := NewSiteConfigListHandler(siteConfigService, 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
|
|
|
|
2024-02-25 13:01:55 +00:00
|
|
|
siteConfigMenusHandler := NewSiteConfigMenusHandler(siteConfigService)
|
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)
|
|
|
|
|
2024-05-17 18:23:38 +00:00
|
|
|
activityPubServer := NewActivityPubServer(siteConfigService, entryService, apService)
|
|
|
|
configRegister.Register(config.ACT_PUB_CONF_NAME, &app.ActivityPubConfig{})
|
|
|
|
|
2024-05-13 17:03:21 +00:00
|
|
|
fiberApp.Use("/static", filesystem.New(filesystem.Config{
|
2023-07-13 19:20:00 +00:00
|
|
|
Root: http.FS(embedDirStatic),
|
|
|
|
PathPrefix: "static",
|
|
|
|
Browse: false,
|
|
|
|
}))
|
2024-05-17 18:23:38 +00:00
|
|
|
fiberApp.Get("/", activityPubServer.HandleActor, indexHandler.Handle)
|
2024-05-13 17:03:21 +00:00
|
|
|
fiberApp.Get("/lists/:list/", listHandler.Handle)
|
2023-06-25 19:32:36 +00:00
|
|
|
// Media
|
2024-05-13 17:03:21 +00:00
|
|
|
fiberApp.Get("/media/+", mediaHandler.Handle)
|
2023-06-25 19:32:36 +00:00
|
|
|
// RSS
|
2024-05-13 17:03:21 +00:00
|
|
|
fiberApp.Get("/index.xml", rssHandler.Handle)
|
2023-06-25 19:32:36 +00:00
|
|
|
// Posts
|
2024-05-13 17:03:21 +00:00
|
|
|
fiberApp.Get("/posts/:post/", entryHandler.Handle)
|
2023-08-08 19:32:24 +00:00
|
|
|
// Webmention
|
2024-05-13 17:03:21 +00:00
|
|
|
fiberApp.Post("/webmention/", webmentionHandler.Handle)
|
2023-07-25 16:59:25 +00:00
|
|
|
// robots.txt
|
2024-05-13 17:03:21 +00:00
|
|
|
fiberApp.Get("/robots.txt", func(c *fiber.Ctx) error {
|
2024-02-25 13:01:55 +00:00
|
|
|
siteConfig, _ := siteConfigService.GetSiteConfig()
|
2023-07-25 17:05:55 +00:00
|
|
|
sitemapUrl, _ := url.JoinPath(siteConfig.FullUrl, "/sitemap.xml")
|
2023-07-25 16:59:25 +00:00
|
|
|
c.Set("Content-Type", "text/plain")
|
2023-08-09 17:16:17 +00:00
|
|
|
return c.SendString(fmt.Sprintf("User-agent: GPTBot\nDisallow: /\n\nUser-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
|
2024-05-13 17:03:21 +00:00
|
|
|
fiberApp.Get("/sitemap.xml", NewSiteMapHandler(entryService, siteConfigService).Handle)
|
2023-07-22 18:34:17 +00:00
|
|
|
|
|
|
|
// ActivityPub
|
2024-05-13 17:03:21 +00:00
|
|
|
fiberApp.Get("/.well-known/webfinger", activityPubServer.HandleWebfinger)
|
|
|
|
fiberApp.Route("/activitypub", activityPubServer.Router)
|
2023-07-22 18:34:17 +00:00
|
|
|
|
2023-07-08 09:08:55 +00:00
|
|
|
return &WebApp{
|
2024-05-13 17:03:21 +00:00
|
|
|
FiberApp: fiberApp,
|
2023-07-17 18:44:59 +00:00
|
|
|
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
|
|
|
}
|