diff --git a/go.mod b/go.mod index 1c4ce36..c317431 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,10 @@ module github.com/Theodor-Springmann-Stiftung/kgpz_web go 1.23.2 require ( + github.com/fsnotify/fsnotify v1.8.0 github.com/go-git/go-git/v5 v5.12.0 github.com/gofiber/fiber/v2 v2.52.5 + github.com/gofiber/storage/memory/v2 v2.0.1 github.com/kelseyhightower/envconfig v1.4.0 github.com/yalue/merged_fs v1.3.0 ) @@ -27,10 +29,12 @@ require ( github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-runewidth v0.0.15 // indirect + github.com/philhofer/fwd v1.1.2 // indirect github.com/pjbgf/sha1cd v0.3.0 // indirect github.com/rivo/uniseg v0.2.0 // indirect github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect github.com/skeema/knownhosts v1.2.2 // indirect + github.com/tinylib/msgp v1.1.8 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasthttp v1.51.0 // indirect github.com/valyala/tcplisten v1.0.0 // indirect diff --git a/go.sum b/go.sum index 0c51794..62d90ca 100644 --- a/go.sum +++ b/go.sum @@ -24,6 +24,8 @@ github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a h1:mATvB/9r/3gvcej github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM= github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= +github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M= +github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0= github.com/gliderlabs/ssh v0.3.7 h1:iV3Bqi942d9huXnzEF2Mt+CY9gLu8DNM4Obd+8bODRE= github.com/gliderlabs/ssh v0.3.7/go.mod h1:zpHEXBstFnQYtGnB8k8kQLol82umzn/2/snG7alWVD8= github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI= @@ -36,6 +38,8 @@ github.com/go-git/go-git/v5 v5.12.0 h1:7Md+ndsjrzZxbddRDZjF14qK+NN56sy6wkqaVrjZt github.com/go-git/go-git/v5 v5.12.0/go.mod h1:FTM9VKtnI2m65hNI/TenDDDnUf2Q9FHnXYjuz9i5OEY= github.com/gofiber/fiber/v2 v2.52.5 h1:tWoP1MJQjGEe4GB5TUGOi7P2E0ZMMRx5ZTG4rT+yGMo= github.com/gofiber/fiber/v2 v2.52.5/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ= +github.com/gofiber/storage/memory/v2 v2.0.1 h1:tAETnom9uvEB9B3I2LkgewiuqYDAH0ItrIsmT8MUEwk= +github.com/gofiber/storage/memory/v2 v2.0.1/go.mod h1:RRo3RfX6nTD/UhERyE/u5LcSfqtMo9dA4ltmieSe+QM= github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= @@ -66,6 +70,8 @@ github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZ github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI= github.com/onsi/gomega v1.27.10/go.mod h1:RsS8tutOdbdgzbPtzzATp12yT7kM5I5aElG3evPbQ0M= +github.com/philhofer/fwd v1.1.2 h1:bnDivRJ1EWPjUIRXV5KfORO897HTbpFAQddBdE8t7Gw= +github.com/philhofer/fwd v1.1.2/go.mod h1:qkPdfjR2SIEbspLqpe1tO4n5yICnr2DY7mqEx2tUTP0= github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4= github.com/pjbgf/sha1cd v0.3.0/go.mod h1:nZ1rrWOcGJ5uZgEEVL1VUM9iRQiZvWdbZjkKyFzPPsI= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= @@ -86,6 +92,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/tinylib/msgp v1.1.8 h1:FCXC1xanKO4I8plpHGH2P7koL/RzZs12l/+r7vakfm0= +github.com/tinylib/msgp v1.1.8/go.mod h1:qkpG+2ldGg4xRFmx+jfTvZPxfGFhi64BcnL9vkCm/Tw= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA= @@ -105,6 +113,7 @@ golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= @@ -113,6 +122,7 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= +golang.org/x/net v0.3.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc= @@ -141,6 +151,7 @@ golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= +golang.org/x/term v0.3.0/go.mod h1:q750SLmJuPmVoN1blW3UFBPREJfb1KmY3vwxfr+nFDA= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8= @@ -150,6 +161,7 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= @@ -157,6 +169,7 @@ golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.4.0/go.mod h1:UE5sM2OK9E/d67R0ANs2xJizIymRP5gJU295PvKXxjQ= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ= golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= diff --git a/helpers/watcher.go b/helpers/watcher.go new file mode 100644 index 0000000..2e3f768 --- /dev/null +++ b/helpers/watcher.go @@ -0,0 +1,75 @@ +package helpers + +import ( + "log" + + "github.com/fsnotify/fsnotify" +) + +type IFileWatcher interface { + GetEvents() chan string +} + +type FileWatcher struct { + path []string + events chan string + watcher *fsnotify.Watcher +} + +func NewFileWatcher(path []string) (*FileWatcher, error) { + fw := &FileWatcher{path: path, events: make(chan string, 48)} + err := fw.Watch(path) + if err != nil { + return nil, err + } + return fw, nil +} + +func (fw *FileWatcher) Watch(paths []string) error { + fw.events = make(chan string, 48) + + watcher, err := fsnotify.NewWatcher() + if err != nil { + return err + } + fw.watcher = watcher + + // Start listening for events. + go func() { + for { + select { + case event, ok := <-watcher.Events: + if !ok { + return + } + log.Println("event:", event) + if !event.Has(fsnotify.Chmod) { + fw.events <- event.Name + } + case err, ok := <-watcher.Errors: + if !ok { + return + } + log.Println("error:", err) + } + } + }() + + for _, path := range paths { + err = watcher.Add(path) + if err != nil { + return err + } + } + + return nil +} + +func (fw *FileWatcher) GetEvents() chan string { + return fw.events +} + +func (fw *FileWatcher) Close() { + fw.watcher.Close() + close(fw.events) +} diff --git a/server/server.go b/server/server.go index 639b777..8fd2683 100644 --- a/server/server.go +++ b/server/server.go @@ -2,25 +2,38 @@ package server import ( "fmt" + "io/fs" + "path/filepath" "sync" "time" "github.com/Theodor-Springmann-Stiftung/kgpz_web/app" + "github.com/Theodor-Springmann-Stiftung/kgpz_web/helpers" "github.com/Theodor-Springmann-Stiftung/kgpz_web/providers" + "github.com/Theodor-Springmann-Stiftung/kgpz_web/templating" "github.com/Theodor-Springmann-Stiftung/kgpz_web/views" "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/middleware/cache" "github.com/gofiber/fiber/v2/middleware/logger" "github.com/gofiber/fiber/v2/middleware/recover" + "github.com/gofiber/storage/memory/v2" ) const ( + // INFO: This timeout is stupid. Uploads can take a long time, others might not. It's messy. REQUEST_TIMEOUT = 8 * time.Second SERVER_TIMEOUT = 8 * time.Second STATIC_PREFIX = "/assets" ) +const ( + STATIC_FILEPATH = "./views/assets" + ROUTES_FILEPATH = "./views/routes" + LAYOUT_FILEPATH = "./views/layouts" +) + // INFO: Server is a meta-package that handles the current router, which it starts in a goroutine. // The router must be able to restart itself, if the data validation fails, so we subscribe to a channel on the app, // which indicates that the data has changed @@ -33,6 +46,7 @@ type Server struct { Config *providers.ConfigProvider running chan bool shutdown *sync.WaitGroup + cache *memory.Storage } func Start(k *app.KGPZ, c *providers.ConfigProvider) *Server { @@ -41,7 +55,44 @@ func Start(k *app.KGPZ, c *providers.ConfigProvider) *Server { } } +// INFO: this is a hacky way to add watchers to the server, which will restart the server if the files change +// It is very rudimentary and just restarts everything +// TODO: send a reload on a websocket +func (e *Server) AddWatchers(paths []string) error { + var dirs []string + for _, path := range paths { + // Get all subdirectories for paths + filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error { + if d.IsDir() { + dirs = append(dirs, path) + } + return nil + }) + } + + watcher, err := helpers.NewFileWatcher(dirs) + if err != nil { + return err + } + + go func() { + w := watcher.GetEvents() + <-w + watcher.Close() + time.Sleep(200 * time.Millisecond) + e.Restart() + }() + + return nil +} + func (s *Server) Start() { + s.cache = memory.New(memory.Config{ + GCInterval: 30 * time.Second, + }) + + engine := templating.NewEngine(&views.LayoutFS, &views.RoutesFS) + srv := fiber.New(fiber.Config{ AppName: s.Config.Address, CaseSensitive: false, @@ -55,6 +106,11 @@ func (s *Server) Start() { StreamRequestBody: false, WriteTimeout: REQUEST_TIMEOUT, ReadTimeout: REQUEST_TIMEOUT, + + PassLocalsToViews: true, + + Views: engine, + ViewsLayout: templating.DEFAULT_LAYOUT_NAME, }) if s.Config.Debug { @@ -62,13 +118,42 @@ func (s *Server) Start() { } srv.Use(recover.New()) + + // TODO: Dont cache static assets, bc storage gets huge + if s.Config.Debug { + srv.Use(cache.New(cache.Config{ + Next: func(c *fiber.Ctx) bool { + return c.Query("noCache") == "true" + }, + Expiration: 30 * time.Minute, + CacheControl: false, + Storage: s.cache, + })) + } else { + srv.Use(cache.New(cache.Config{ + Next: func(c *fiber.Ctx) bool { + return c.Query("noCache") == "true" + }, + Expiration: 30 * time.Minute, + CacheControl: true, + Storage: s.cache, + })) + } + srv.Use(STATIC_PREFIX, static(&views.StaticFS)) srv.Get("/", func(c *fiber.Ctx) error { - return c.SendString("I'm a GET request!") + return c.Render("/", fiber.Map{}) }) s.runner(srv) + + if s.Config.Debug { + err := s.AddWatchers([]string{ROUTES_FILEPATH, LAYOUT_FILEPATH}) + if err != nil { + fmt.Println(err) + } + } } func (s *Server) Stop() { @@ -125,6 +210,7 @@ func (s *Server) runner(srv *fiber.App) { fmt.Println(err) fmt.Println("Error shutting down server") } + s.cache.Close() } else { if err := srv.ShutdownWithTimeout(0); err != nil { fmt.Println(err) diff --git a/templating/engine.go b/templating/engine.go index af54f45..d91d249 100644 --- a/templating/engine.go +++ b/templating/engine.go @@ -4,27 +4,72 @@ import ( "html/template" "io" "io/fs" + "path/filepath" "sync" + "time" + + "github.com/Theodor-Springmann-Stiftung/kgpz_web/helpers" ) type Engine struct { - // NOTE: LayoutRegistry and TemplateRegistry have their own syncronization and do not require a mutex here + // NOTE: LayoutRegistry and TemplateRegistry have their own syncronization & cache and do not require a mutex here + regmu *sync.Mutex LayoutRegistry *LayoutRegistry TemplateRegistry *TemplateRegistry mu *sync.Mutex FuncMap template.FuncMap + + paths []string + layouts *fs.FS + templates *fs.FS } func NewEngine(layouts, templates *fs.FS) *Engine { return &Engine{ + regmu: &sync.Mutex{}, mu: &sync.Mutex{}, LayoutRegistry: NewLayoutRegistry(*layouts), TemplateRegistry: NewTemplateRegistry(*templates), FuncMap: template.FuncMap{}, + layouts: layouts, + templates: templates, } } +func (e *Engine) AddWatchers(paths []string) error { + e.paths = paths + var dirs []string + for _, path := range paths { + // Get all subdirectories for paths + filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error { + if d.IsDir() { + dirs = append(dirs, path) + } + return nil + }) + } + + watcher, err := helpers.NewFileWatcher(dirs) + defer watcher.Close() + if err != nil { + return err + } + + go func() { + w := watcher.GetEvents() + <-w + time.Sleep(100 * time.Millisecond) + e.regmu.Lock() + defer e.regmu.Unlock() + e.LayoutRegistry = NewLayoutRegistry(*e.layouts) + e.TemplateRegistry = NewTemplateRegistry(*e.templates) + e.AddWatchers(e.paths) + }() + + return nil +} + func (e *Engine) Load() error { wg := sync.WaitGroup{} wg.Add(2) diff --git a/views/assets/hello.txt b/views/assets/hello.txt new file mode 100644 index 0000000..ce01362 --- /dev/null +++ b/views/assets/hello.txt @@ -0,0 +1 @@ +hello diff --git a/views/embed_dev.go b/views/embed_dev.go index 9f5689d..e29e9eb 100644 --- a/views/embed_dev.go +++ b/views/embed_dev.go @@ -1,4 +1,5 @@ //go:build dev +// +build dev package views diff --git a/views/routes/body.tmpl b/views/routes/body.tmpl index 0dca1b8..62c2052 100644 --- a/views/routes/body.tmpl +++ b/views/routes/body.tmpl @@ -1,3 +1,3 @@ -{{define "body"}} -

Hello from body

-{{end}} +{{ define "body" }} +

Changed again! Hello from body

+{{ end }}