From 974e36070443de6b1bcfd183d1c6d608bb217dc4 Mon Sep 17 00:00:00 2001 From: iacore Date: Sat, 10 Feb 2024 13:54:09 +0000 Subject: [PATCH] add dev mode (PIXIVFE_DEV=1) --- core/config/config.go | 8 ++++++-- main.go | 38 ++++++++++++++++++++++---------------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/core/config/config.go b/core/config/config.go index a9f48a8..8d8f391 100644 --- a/core/config/config.go +++ b/core/config/config.go @@ -27,11 +27,15 @@ type ServerConfig struct { AcceptLanguage string RequestLimit int - StartingTime string - Version string + StartingTime string + Version string + InDevelopment bool } func (s *ServerConfig) InitializeConfig() error { + _, hasDev := os.LookupEnv("PIXIVFE_DEV") + s.InDevelopment = hasDev + token, hasToken := os.LookupEnv("PIXIVFE_TOKEN") if !hasToken { log.Fatalln("PIXIVFE_TOKEN is required, but was not set.") diff --git a/main.go b/main.go index e86046b..c7974e3 100644 --- a/main.go +++ b/main.go @@ -36,6 +36,9 @@ func main() { config.GlobalServerConfig.InitializeConfig() engine := jet.New("./views", ".jet.html") + if config.GlobalServerConfig.InDevelopment { + engine.Reload(true) + } engine.AddFuncMap(serve.GetTemplateFunctions()) @@ -79,25 +82,28 @@ func main() { }, )) - server.Use(cache.New( - cache.Config{ - Next: func(c *fiber.Ctx) bool { - resp_code := c.Response().StatusCode() - if resp_code < 200 || resp_code >= 300 { - return true - } + if !config.GlobalServerConfig.InDevelopment { + server.Use(cache.New( + cache.Config{ + Next: func(c *fiber.Ctx) bool { + resp_code := c.Response().StatusCode() + if resp_code < 200 || resp_code >= 300 { + return true + } - // Disable cache for settings page - return strings.Contains(c.Path(), "/settings") || c.Path() == "/" - }, - Expiration: 5 * time.Minute, - CacheControl: true, + // Disable cache for settings page + return strings.Contains(c.Path(), "/settings") || c.Path() == "/" + }, + Expiration: 5 * time.Minute, + CacheControl: true, - KeyGenerator: func(c *fiber.Ctx) string { - return utils.CopyString(c.OriginalURL()) + KeyGenerator: func(c *fiber.Ctx) string { + return utils.CopyString(c.OriginalURL()) + }, }, - }, - )) + )) + } + server.Use(recover.New()) server.Use(compress.New(compress.Config{