Compare commits
No commits in common. "4d8001c283741d7417f6392f0b6b9cd931c1c3a9" and "7031596aeed42eecc9619f15edc4f1968d6cbbe2" have entirely different histories.
4d8001c283
...
7031596aee
|
@ -8,7 +8,6 @@ Code for the [burning.moe](https://burning.moe) homepage
|
|||
- [cleanenv](https://github.com/ilyakaznacheev/cleanenv)
|
||||
- [log](https://github.com/charmbracelet/log)
|
||||
- [mage](https://github.com/magefile/mage)
|
||||
- [tasks](https://github.com/madflojo/tasks)
|
||||
- written using [helix](https://helix-editor.com/)
|
||||
- tested on [void linux](https://voidlinux.org/)
|
||||
- hosted on [debian linux](https://www.debian.org/)
|
||||
|
|
|
@ -5,13 +5,10 @@ package main
|
|||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.burning.moe/celediel/burning.moe/internal/config"
|
||||
"git.burning.moe/celediel/burning.moe/internal/handlers"
|
||||
"git.burning.moe/celediel/burning.moe/internal/render"
|
||||
|
||||
"github.com/madflojo/tasks"
|
||||
)
|
||||
|
||||
// App wide config data and such
|
||||
|
@ -25,26 +22,6 @@ func main() {
|
|||
handlers.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
|
||||
srv := &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", app.ListenPort),
|
||||
|
@ -53,7 +30,7 @@ func main() {
|
|||
|
||||
// and finally, start the server
|
||||
app.Logger.Printf("Starting HTTP Server on port %d", app.ListenPort)
|
||||
err = srv.ListenAndServe()
|
||||
err := srv.ListenAndServe()
|
||||
if err != nil {
|
||||
app.Logger.Fatal("Failed to start HTTP Server!", "err", err)
|
||||
}
|
||||
|
|
2
go.mod
2
go.mod
|
@ -6,7 +6,6 @@ require (
|
|||
github.com/charmbracelet/log v0.3.1
|
||||
github.com/go-chi/chi/v5 v5.0.11
|
||||
github.com/ilyakaznacheev/cleanenv v1.5.0
|
||||
github.com/madflojo/tasks v1.1.0
|
||||
github.com/magefile/mage v1.15.0
|
||||
)
|
||||
|
||||
|
@ -22,7 +21,6 @@ require (
|
|||
github.com/muesli/reflow v0.3.0 // indirect
|
||||
github.com/muesli/termenv v0.15.2 // 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/sys v0.13.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
|
|
4
go.sum
4
go.sum
|
@ -19,8 +19,6 @@ github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
|||
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/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/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
|
||||
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
|
||||
|
@ -37,8 +35,6 @@ 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.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
|
||||
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/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
|
||||
|
|
|
@ -12,28 +12,25 @@ import (
|
|||
|
||||
// AppConfig contains data to be accessed across the app.
|
||||
type AppConfig struct {
|
||||
CacheTimer time.Duration
|
||||
ListenPort uint16
|
||||
Logger *log.Logger
|
||||
LogLevel log.Level
|
||||
TemplateCache models.TemplateCache
|
||||
UseCache bool
|
||||
Logger *log.Logger
|
||||
LogLevel log.Level
|
||||
}
|
||||
|
||||
// defaluts contains default settings that are used if no environmental variables are set
|
||||
var defaults = &AppConfig{
|
||||
CacheTimer: time.Hour * 12,
|
||||
ListenPort: 9001,
|
||||
LogLevel: log.InfoLevel,
|
||||
UseCache: true,
|
||||
LogLevel: log.InfoLevel,
|
||||
}
|
||||
|
||||
// ConfigDatabase contains data to be loaded from environmental variables
|
||||
type ConfigDatabase struct {
|
||||
CacheTimer time.Duration `env:"CACHETIMER" env-description:"How often to automatically regenerate template cache."`
|
||||
LogLevel string `env:"LOGLEVEL" env-description:"Logging level. Default: warn, Possible values: debug info warn error fatal none"`
|
||||
Port uint16 `env:"PORT" env-description:"server port"`
|
||||
UseCache bool `env:"CACHE" env-description:"Use template cache"`
|
||||
Port uint16 `env:"PORT" env-default:"9001" env-description:"server port"`
|
||||
LogLevel string `env:"LOGLEVEL" env-default:"warn" 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"`
|
||||
}
|
||||
|
||||
// Initialises the app wide AppConfig, loads values from environment, and set up the Logger
|
||||
|
@ -50,7 +47,6 @@ func Initialise() AppConfig {
|
|||
if cfg, err := loadConfig(); err == nil {
|
||||
app.ListenPort = cfg.Port
|
||||
app.UseCache = cfg.UseCache
|
||||
app.CacheTimer = cfg.CacheTimer
|
||||
app.LogLevel, err = log.ParseLevel(cfg.LogLevel)
|
||||
if err != nil {
|
||||
app.LogLevel = defaults.LogLevel
|
||||
|
@ -60,10 +56,7 @@ func Initialise() AppConfig {
|
|||
}
|
||||
|
||||
app.Logger.SetLevel(app.LogLevel)
|
||||
app.Logger.Debug("Loaded config from environment:",
|
||||
"port", app.ListenPort, "useCache", app.UseCache,
|
||||
"log_level", app.LogLevel, "cacheTimer", app.CacheTimer,
|
||||
)
|
||||
app.Logger.Debug("Loaded config from environment:", "port", app.ListenPort, "useCache", app.UseCache, "log_level", app.LogLevel)
|
||||
|
||||
return app
|
||||
}
|
||||
|
|
|
@ -37,10 +37,7 @@ func Initialise(a *config.AppConfig) {
|
|||
func RenderTemplate(w http.ResponseWriter, filename string) {
|
||||
// TODO: implement this better
|
||||
if !app.UseCache {
|
||||
err := RegenerateTemplateCache()
|
||||
if err != nil {
|
||||
app.Logger.Fatal("Regenerating template cache failed!", err, "err")
|
||||
}
|
||||
regenerateTemplateCache()
|
||||
}
|
||||
|
||||
template, err := GetTemplateFromCache(filename)
|
||||
|
@ -62,10 +59,7 @@ func RenderTemplate(w http.ResponseWriter, filename string) {
|
|||
|
||||
func RenderTemplateWithData(w http.ResponseWriter, filename string, data *models.TemplateData) {
|
||||
if !app.UseCache {
|
||||
err := RegenerateTemplateCache()
|
||||
if err != nil {
|
||||
app.Logger.Fatal("Regenerating template cache failed!", err, "err")
|
||||
}
|
||||
regenerateTemplateCache()
|
||||
}
|
||||
|
||||
template, err := GetTemplateFromCache(filename)
|
||||
|
@ -110,15 +104,13 @@ func GetOrGenerateTemplateData(filename string) (*models.TemplateData, error) {
|
|||
return &data, nil
|
||||
}
|
||||
|
||||
// RegenerateTemplateCache regenerates the template cache
|
||||
func RegenerateTemplateCache() error {
|
||||
// regenerateTemplateCache regenerates the template cache
|
||||
func regenerateTemplateCache() {
|
||||
c, err := generateNewTemplateCache()
|
||||
if err != nil {
|
||||
// app.Logger.Fatal("Error generating template cache, bailing out!", "err", err)
|
||||
return err
|
||||
app.Logger.Fatal("Error generating template cache, bailing out!", "err", err)
|
||||
}
|
||||
app.TemplateCache = c
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue