Diffstat (limited to 'main.go')
| -rw-r--r-- | main.go | 99 |
1 files changed, 81 insertions, 18 deletions
@@ -11,17 +11,23 @@ import ( "github.com/SayaAndy/saya-today-web/config" "github.com/SayaAndy/saya-today-web/internal/b2" - "github.com/SayaAndy/saya-today-web/internal/lightgallery" + "github.com/SayaAndy/saya-today-web/internal/blogtrigger" + "github.com/SayaAndy/saya-today-web/internal/factgiver" + "github.com/SayaAndy/saya-today-web/internal/glightbox" + "github.com/SayaAndy/saya-today-web/internal/mailer" "github.com/SayaAndy/saya-today-web/internal/router" "github.com/SayaAndy/saya-today-web/internal/tailwind" "github.com/SayaAndy/saya-today-web/locale" + "github.com/dgraph-io/ristretto/v2" "github.com/gofiber/fiber/v2" - "github.com/gofiber/fiber/v2/middleware/redirect" + "github.com/gofiber/fiber/v2/middleware/cors" "github.com/golang-migrate/migrate/v4" "github.com/golang-migrate/migrate/v4/database/sqlite3" "github.com/yuin/goldmark" "github.com/yuin/goldmark/parser" - gmhtml "github.com/yuin/goldmark/renderer/html" + "github.com/yuin/goldmark/renderer" + "github.com/yuin/goldmark/renderer/html" + "github.com/yuin/goldmark/util" _ "github.com/golang-migrate/migrate/v4/source/file" _ "github.com/mattn/go-sqlite3" @@ -30,19 +36,25 @@ import ( var ( md = goldmark.New( goldmark.WithExtensions( - lightgallery.NewLightGalleryExtension(), + glightbox.NewGLightboxExtension(), tailwind.NewTailwindExtension(), ), goldmark.WithParserOptions( parser.WithAutoHeadingID(), + parser.WithAttribute(), ), - goldmark.WithRendererOptions( - gmhtml.WithXHTML(), + goldmark.WithRenderer( + renderer.NewRenderer( + renderer.WithNodeRenderers( + util.Prioritized(tailwind.NewCustomLinkRenderer(html.WithUnsafe(), html.WithXHTML()), 50), + util.Prioritized(html.NewRenderer(html.WithXHTML()), 100), + ), + ), ), ) b2Client *b2.B2Client configPath = flag.String("c", "config.yaml", "Path to the configuration file (in YAML format)") - availableLanguages = make([]string, 0) + availableLanguages = make([]config.AvailableLanguageConfig, 0) localization = make(map[string]*locale.LocaleConfig) ) @@ -98,7 +110,7 @@ func main() { slog.Warn("fail to initialize a locale", slog.String("locale", lang.Name), slog.String("error", err.Error())) continue } - availableLanguages = append(availableLanguages, lang.Name) + availableLanguages = append(availableLanguages, lang) localization[lang.Name] = localeCfg } @@ -106,28 +118,75 @@ func main() { ProxyHeader: "X-Forwarded-For", }) - app.Use(redirect.New(redirect.Config{ - Rules: map[string]string{ - "/ru": "/", - "/en": "/", - }, - StatusCode: 301, + app.Use(cors.New(cors.Config{ + AllowOrigins: "https://f003.backblazeb2.com", + AllowMethods: "GET,POST,OPTIONS", + AllowHeaders: "Origin, Content-Type, Accept", })) router.CCache, err = router.NewClientCache(db, []byte(cfg.Auth.Salt)) if err != nil { - slog.Error("fail to initialize cache", slog.String("error", err.Error())) + slog.Error("fail to initialize client cache", slog.String("error", err.Error())) os.Exit(1) } - app.Get("/", router.Root(cfg.AvailableLanguages)) + router.PCache, err = ristretto.NewCache(&ristretto.Config[string, []byte]{ + NumCounters: 1e6, // 1,000,000 + MaxCost: 1 << 29, // 512 MB + BufferItems: 64, // number of keys per Get buffer. + }) + if err != nil { + slog.Error("fail to initialize page cache", slog.String("error", err.Error())) + os.Exit(1) + } + + router.FactGiver, err = factgiver.NewFactGiver(&cfg.FactGiver, availableLanguages) + if err != nil { + slog.Error("fail to initialize fact giver", slog.String("error", err.Error())) + os.Exit(1) + } + + router.Mailer, err = mailer.NewMailer(db, cfg.Mail.ClientHost, cfg.Mail.MailHost, + cfg.Mail.PublicName, cfg.Mail.MailAddress, cfg.Mail.Username, cfg.Mail.Password, []byte(cfg.Mail.Salt), localization) + if err != nil { + slog.Error("fail to initialize mailer", slog.String("error", err.Error())) + os.Exit(1) + } + + router.BlogTrigger, err = blogtrigger.NewBlogTriggerScheduler(b2Client, cfg.AvailableLanguages, cfg.Mail.Trigger.OnNewPost, func(bp []*b2.BlogPage) error { + for _, post := range bp { + if err := router.Mailer.NewPost(post); err != nil { + return err + } + } + return nil + }) + if err != nil { + slog.Error("fail to initialize blog trigger", slog.String("error", err.Error())) + os.Exit(1) + } + + app.Get("/", router.Api_V1_GeneralPage(localization, availableLanguages)) + app.Get("/:lang<len(2)>", router.Api_V1_GeneralPage(localization, availableLanguages)) app.Get("/:lang/map", router.Lang_Map(localization, availableLanguages, b2Client)) - app.Get("/:lang/blog", router.Lang_Blog(localization, availableLanguages, b2Client)) - app.Get("/:lang/blog/:title", router.Lang_Blog_Title(localization, availableLanguages, b2Client, md)) + app.Get("/:lang/user", router.Api_V1_GeneralPage(localization, availableLanguages)) + app.Get("/:lang/user/unsubscribe", router.Lang_User_Unsubscribe(localization, availableLanguages)) + app.Get("/:lang/blog", router.Api_V1_GeneralPage(localization, availableLanguages)) + app.Get("/:lang/blog/:title", router.Api_V1_GeneralPage(localization, availableLanguages)) + app.Get("/api/v1/tz", router.Api_V1_TZ()) app.Get("/api/v1/blog-search", router.Api_V1_BlogSearch(localization, availableLanguages, b2Client)) app.Get("/api/v1/like", router.Api_V1_Like_Get(localization, b2Client)) app.Put("/api/v1/like", router.Api_V1_Like_Put(localization, b2Client)) + app.Get("/api/v1/general-page/header", router.Api_V1_GeneralPage_Header(localization, availableLanguages, b2Client)) + app.Get("/api/v1/general-page/body", router.Api_V1_GeneralPage_Body(localization, availableLanguages, b2Client, md)) + app.Get("/api/v1/general-page/footer", router.Api_V1_GeneralPage_Footer(localization, availableLanguages)) + app.Get("/api/v1/general-page/top-embeds", router.Api_V1_GeneralPage_TopEmbeds(localization, availableLanguages)) + app.Get("/api/v1/general-page/bottom-embeds", router.Api_V1_GeneralPage_BottomEmbeds(localization, availableLanguages, b2Client)) + app.Post("/api/v1/email/send-verification-code", router.Api_V1_Email_SendVerificationCode(localization)) + app.Post("/api/v1/email/verify", router.Api_V1_Email_Verify(localization)) + app.Get("/api/v1/email/is-in-verification", router.Api_V1_Email_IsInVerification(localization)) + app.Put("/api/v1/subs", router.Api_V1_Subs_Put(localization)) app.Static("/", "./static") @@ -143,6 +202,9 @@ func main() { <-sigChan slog.Info("gracefully shutting down...") + if err = router.BlogTrigger.Close(); err != nil { + slog.Error("fail to shutdown blog trigger scheduler", slog.String("error", err.Error())) + } if err = app.Shutdown(); err != nil { slog.Error("fail to shutdown fiber server", slog.String("error", err.Error())) } @@ -152,5 +214,6 @@ func main() { if err = db.Close(); err != nil { slog.Error("fail to close db connection", slog.String("error", err.Error())) } + router.PCache.Close() db.Close() } |