Compare commits
4 commits
7031596aee
...
4d8001c283
Author | SHA1 | Date | |
---|---|---|---|
Lilian Jónsdóttir | 4d8001c283 | ||
Lilian Jónsdóttir | 7451953d43 | ||
Lilian Jónsdóttir | d0be48a532 | ||
Lilian Jónsdóttir | 867c21722a |
|
@ -8,6 +8,7 @@ Code for the [burning.moe](https://burning.moe) homepage
|
||||||
- [cleanenv](https://github.com/ilyakaznacheev/cleanenv)
|
- [cleanenv](https://github.com/ilyakaznacheev/cleanenv)
|
||||||
- [log](https://github.com/charmbracelet/log)
|
- [log](https://github.com/charmbracelet/log)
|
||||||
- [mage](https://github.com/magefile/mage)
|
- [mage](https://github.com/magefile/mage)
|
||||||
|
- [tasks](https://github.com/madflojo/tasks)
|
||||||
- written using [helix](https://helix-editor.com/)
|
- written using [helix](https://helix-editor.com/)
|
||||||
- tested on [void linux](https://voidlinux.org/)
|
- tested on [void linux](https://voidlinux.org/)
|
||||||
- hosted on [debian linux](https://www.debian.org/)
|
- hosted on [debian linux](https://www.debian.org/)
|
||||||
|
|
|
@ -5,10 +5,13 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
"git.burning.moe/celediel/burning.moe/internal/config"
|
"git.burning.moe/celediel/burning.moe/internal/config"
|
||||||
"git.burning.moe/celediel/burning.moe/internal/handlers"
|
"git.burning.moe/celediel/burning.moe/internal/handlers"
|
||||||
"git.burning.moe/celediel/burning.moe/internal/render"
|
"git.burning.moe/celediel/burning.moe/internal/render"
|
||||||
|
|
||||||
|
"github.com/madflojo/tasks"
|
||||||
)
|
)
|
||||||
|
|
||||||
// App wide config data and such
|
// App wide config data and such
|
||||||
|
@ -22,6 +25,26 @@ func main() {
|
||||||
handlers.Initialise(&app)
|
handlers.Initialise(&app)
|
||||||
render.Initialise(&app)
|
render.Initialise(&app)
|
||||||
|
|
||||||
|
// Setup task to regenerate template cache
|
||||||
|
scheduler := tasks.New()
|
||||||
|
defer scheduler.Stop()
|
||||||
|
id, err := scheduler.Add(&tasks.Task{
|
||||||
|
Interval: time.Duration(app.CacheTimer),
|
||||||
|
TaskFunc: func() error {
|
||||||
|
err := render.RegenerateTemplateCache()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
app.Logger.Infof("Regenerate cache job finished at %s", time.Now())
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
ErrFunc: func(err error) {
|
||||||
|
app.Logger.Error("Error in template regeneration cache job!", "err", err)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
app.Logger.Info("Started cache regeneration task.", "interval", app.CacheTimer.String(), "id", id)
|
||||||
|
|
||||||
// Initialise the webserver
|
// Initialise the webserver
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
Addr: fmt.Sprintf(":%d", app.ListenPort),
|
Addr: fmt.Sprintf(":%d", app.ListenPort),
|
||||||
|
@ -30,7 +53,7 @@ func main() {
|
||||||
|
|
||||||
// and finally, start the server
|
// and finally, start the server
|
||||||
app.Logger.Printf("Starting HTTP Server on port %d", app.ListenPort)
|
app.Logger.Printf("Starting HTTP Server on port %d", app.ListenPort)
|
||||||
err := srv.ListenAndServe()
|
err = srv.ListenAndServe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.Logger.Fatal("Failed to start HTTP Server!", "err", err)
|
app.Logger.Fatal("Failed to start HTTP Server!", "err", err)
|
||||||
}
|
}
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -6,6 +6,7 @@ require (
|
||||||
github.com/charmbracelet/log v0.3.1
|
github.com/charmbracelet/log v0.3.1
|
||||||
github.com/go-chi/chi/v5 v5.0.11
|
github.com/go-chi/chi/v5 v5.0.11
|
||||||
github.com/ilyakaznacheev/cleanenv v1.5.0
|
github.com/ilyakaznacheev/cleanenv v1.5.0
|
||||||
|
github.com/madflojo/tasks v1.1.0
|
||||||
github.com/magefile/mage v1.15.0
|
github.com/magefile/mage v1.15.0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -21,6 +22,7 @@ require (
|
||||||
github.com/muesli/reflow v0.3.0 // indirect
|
github.com/muesli/reflow v0.3.0 // indirect
|
||||||
github.com/muesli/termenv v0.15.2 // indirect
|
github.com/muesli/termenv v0.15.2 // indirect
|
||||||
github.com/rivo/uniseg v0.2.0 // indirect
|
github.com/rivo/uniseg v0.2.0 // indirect
|
||||||
|
github.com/rs/xid v1.5.0 // indirect
|
||||||
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
|
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
|
||||||
golang.org/x/sys v0.13.0 // indirect
|
golang.org/x/sys v0.13.0 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
|
4
go.sum
4
go.sum
|
@ -19,6 +19,8 @@ github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||||
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
||||||
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||||
|
github.com/madflojo/tasks v1.1.0 h1:CHQhzdot1MdAlWLw/VOa69SRZ28VQ+gbSf9lI55aSsM=
|
||||||
|
github.com/madflojo/tasks v1.1.0/go.mod h1:h0cJ1gYqIBzczyg0tyEqGAbIltbxxhUsMMKnaQa9IPc=
|
||||||
github.com/magefile/mage v1.15.0 h1:BvGheCMAsG3bWUDbZ8AyXXpCNwU9u5CB6sM+HNb9HYg=
|
github.com/magefile/mage v1.15.0 h1:BvGheCMAsG3bWUDbZ8AyXXpCNwU9u5CB6sM+HNb9HYg=
|
||||||
github.com/magefile/mage v1.15.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
|
github.com/magefile/mage v1.15.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
|
||||||
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
|
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
|
||||||
|
@ -35,6 +37,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
|
||||||
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||||
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
|
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
|
||||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||||
|
github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc=
|
||||||
|
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
|
||||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||||
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
|
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
|
||||||
|
|
|
@ -12,25 +12,28 @@ import (
|
||||||
|
|
||||||
// AppConfig contains data to be accessed across the app.
|
// AppConfig contains data to be accessed across the app.
|
||||||
type AppConfig struct {
|
type AppConfig struct {
|
||||||
|
CacheTimer time.Duration
|
||||||
ListenPort uint16
|
ListenPort uint16
|
||||||
TemplateCache models.TemplateCache
|
|
||||||
UseCache bool
|
|
||||||
Logger *log.Logger
|
Logger *log.Logger
|
||||||
LogLevel log.Level
|
LogLevel log.Level
|
||||||
|
TemplateCache models.TemplateCache
|
||||||
|
UseCache bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// defaluts contains default settings that are used if no environmental variables are set
|
// defaluts contains default settings that are used if no environmental variables are set
|
||||||
var defaults = &AppConfig{
|
var defaults = &AppConfig{
|
||||||
|
CacheTimer: time.Hour * 12,
|
||||||
ListenPort: 9001,
|
ListenPort: 9001,
|
||||||
UseCache: true,
|
|
||||||
LogLevel: log.InfoLevel,
|
LogLevel: log.InfoLevel,
|
||||||
|
UseCache: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
// ConfigDatabase contains data to be loaded from environmental variables
|
// ConfigDatabase contains data to be loaded from environmental variables
|
||||||
type ConfigDatabase struct {
|
type ConfigDatabase struct {
|
||||||
Port uint16 `env:"PORT" env-default:"9001" env-description:"server port"`
|
CacheTimer time.Duration `env:"CACHETIMER" env-description:"How often to automatically regenerate template cache."`
|
||||||
LogLevel string `env:"LOGLEVEL" env-default:"warn" env-description:"Logging level. Default: warn, Possible values: debug info warn error fatal none"`
|
LogLevel string `env:"LOGLEVEL" env-description:"Logging level. Default: warn, Possible values: debug info warn error fatal none"`
|
||||||
UseCache bool `env:"CACHE" env-default:"true" env-description:"Use template cache"`
|
Port uint16 `env:"PORT" env-description:"server port"`
|
||||||
|
UseCache bool `env:"CACHE" env-description:"Use template cache"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialises the app wide AppConfig, loads values from environment, and set up the Logger
|
// Initialises the app wide AppConfig, loads values from environment, and set up the Logger
|
||||||
|
@ -47,6 +50,7 @@ func Initialise() AppConfig {
|
||||||
if cfg, err := loadConfig(); err == nil {
|
if cfg, err := loadConfig(); err == nil {
|
||||||
app.ListenPort = cfg.Port
|
app.ListenPort = cfg.Port
|
||||||
app.UseCache = cfg.UseCache
|
app.UseCache = cfg.UseCache
|
||||||
|
app.CacheTimer = cfg.CacheTimer
|
||||||
app.LogLevel, err = log.ParseLevel(cfg.LogLevel)
|
app.LogLevel, err = log.ParseLevel(cfg.LogLevel)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.LogLevel = defaults.LogLevel
|
app.LogLevel = defaults.LogLevel
|
||||||
|
@ -56,7 +60,10 @@ func Initialise() AppConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
app.Logger.SetLevel(app.LogLevel)
|
app.Logger.SetLevel(app.LogLevel)
|
||||||
app.Logger.Debug("Loaded config from environment:", "port", app.ListenPort, "useCache", app.UseCache, "log_level", app.LogLevel)
|
app.Logger.Debug("Loaded config from environment:",
|
||||||
|
"port", app.ListenPort, "useCache", app.UseCache,
|
||||||
|
"log_level", app.LogLevel, "cacheTimer", app.CacheTimer,
|
||||||
|
)
|
||||||
|
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,7 +37,10 @@ func Initialise(a *config.AppConfig) {
|
||||||
func RenderTemplate(w http.ResponseWriter, filename string) {
|
func RenderTemplate(w http.ResponseWriter, filename string) {
|
||||||
// TODO: implement this better
|
// TODO: implement this better
|
||||||
if !app.UseCache {
|
if !app.UseCache {
|
||||||
regenerateTemplateCache()
|
err := RegenerateTemplateCache()
|
||||||
|
if err != nil {
|
||||||
|
app.Logger.Fatal("Regenerating template cache failed!", err, "err")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template, err := GetTemplateFromCache(filename)
|
template, err := GetTemplateFromCache(filename)
|
||||||
|
@ -59,7 +62,10 @@ func RenderTemplate(w http.ResponseWriter, filename string) {
|
||||||
|
|
||||||
func RenderTemplateWithData(w http.ResponseWriter, filename string, data *models.TemplateData) {
|
func RenderTemplateWithData(w http.ResponseWriter, filename string, data *models.TemplateData) {
|
||||||
if !app.UseCache {
|
if !app.UseCache {
|
||||||
regenerateTemplateCache()
|
err := RegenerateTemplateCache()
|
||||||
|
if err != nil {
|
||||||
|
app.Logger.Fatal("Regenerating template cache failed!", err, "err")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template, err := GetTemplateFromCache(filename)
|
template, err := GetTemplateFromCache(filename)
|
||||||
|
@ -104,13 +110,15 @@ func GetOrGenerateTemplateData(filename string) (*models.TemplateData, error) {
|
||||||
return &data, nil
|
return &data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// regenerateTemplateCache regenerates the template cache
|
// RegenerateTemplateCache regenerates the template cache
|
||||||
func regenerateTemplateCache() {
|
func RegenerateTemplateCache() error {
|
||||||
c, err := generateNewTemplateCache()
|
c, err := generateNewTemplateCache()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.Logger.Fatal("Error generating template cache, bailing out!", "err", err)
|
// app.Logger.Fatal("Error generating template cache, bailing out!", "err", err)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
app.TemplateCache = c
|
app.TemplateCache = c
|
||||||
|
return nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue