mirror of
https://github.com/Theodor-Springmann-Stiftung/kgpz_web.git
synced 2025-10-28 16:45:32 +00:00
trying to stop instability from happening
This commit is contained in:
@@ -21,15 +21,19 @@ func GetIssue(kgpz *xmlmodels.Library, pics *pictures.PicturesProvider) fiber.Ha
|
|||||||
return func(c *fiber.Ctx) error {
|
return func(c *fiber.Ctx) error {
|
||||||
y := c.Params("year")
|
y := c.Params("year")
|
||||||
yi, err := strconv.Atoi(y)
|
yi, err := strconv.Atoi(y)
|
||||||
if err != nil || yi < MINYEAR || yi > MAXYEAR {
|
if err != nil {
|
||||||
logging.Error(err, "Year is not a valid number")
|
return c.SendStatus(fiber.StatusNotFound)
|
||||||
|
}
|
||||||
|
if yi < MINYEAR || yi > MAXYEAR {
|
||||||
return c.SendStatus(fiber.StatusNotFound)
|
return c.SendStatus(fiber.StatusNotFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
d := c.Params("issue")
|
d := c.Params("issue")
|
||||||
di, err := strconv.Atoi(d)
|
di, err := strconv.Atoi(d)
|
||||||
if err != nil || di < 1 {
|
if err != nil {
|
||||||
logging.Error(err, "Issue is not a valid number")
|
return c.SendStatus(fiber.StatusNotFound)
|
||||||
|
}
|
||||||
|
if di < 1 {
|
||||||
return c.SendStatus(fiber.StatusNotFound)
|
return c.SendStatus(fiber.StatusNotFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,133 +0,0 @@
|
|||||||
package helpers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"io/fs"
|
|
||||||
"path/filepath"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/Theodor-Springmann-Stiftung/kgpz_web/helpers/logging"
|
|
||||||
"github.com/fsnotify/fsnotify"
|
|
||||||
)
|
|
||||||
|
|
||||||
var NotInitializedError = errors.New("FileWatcher not initialized")
|
|
||||||
var NoWatchFunctionError = errors.New("No watch function provided")
|
|
||||||
|
|
||||||
type IFileWatcher interface {
|
|
||||||
RecursiveDir(path string) error
|
|
||||||
Dir(path string) error
|
|
||||||
Append(fn func(string))
|
|
||||||
Prepend(fn func(string))
|
|
||||||
Watch() error
|
|
||||||
Close()
|
|
||||||
Restart()
|
|
||||||
}
|
|
||||||
|
|
||||||
type FileWatcher struct {
|
|
||||||
mu sync.Mutex
|
|
||||||
wf []func(string)
|
|
||||||
paths []string
|
|
||||||
watcher *fsnotify.Watcher
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewFileWatcher() (*FileWatcher, error) {
|
|
||||||
fw := &FileWatcher{mu: sync.Mutex{}}
|
|
||||||
fw.Watch()
|
|
||||||
return fw, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fw *FileWatcher) RecursiveDir(path string) error {
|
|
||||||
err := filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error {
|
|
||||||
if d.IsDir() {
|
|
||||||
err := fw.Dir(path)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fw *FileWatcher) Dir(path string) error {
|
|
||||||
fw.mu.Lock()
|
|
||||||
defer fw.mu.Unlock()
|
|
||||||
if fw.watcher != nil {
|
|
||||||
err := fw.watcher.Add(path)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fw.paths = append(fw.paths, path)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fw *FileWatcher) Append(fn func(string)) {
|
|
||||||
fw.mu.Lock()
|
|
||||||
defer fw.mu.Unlock()
|
|
||||||
fw.wf = append(fw.wf, fn)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fw *FileWatcher) Prepend(fn func(string)) {
|
|
||||||
fw.mu.Lock()
|
|
||||||
defer fw.mu.Unlock()
|
|
||||||
fw.wf = append([]func(string){fn}, fw.wf...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fw *FileWatcher) Watch() error {
|
|
||||||
watcher, err := fsnotify.NewWatcher()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
fw.mu.Lock()
|
|
||||||
fw.watcher = watcher
|
|
||||||
fw.mu.Unlock()
|
|
||||||
|
|
||||||
// Start listening for events.
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case event, ok := <-watcher.Events:
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if !event.Has(fsnotify.Chmod) {
|
|
||||||
fw.mu.Lock()
|
|
||||||
for _, wf := range fw.wf {
|
|
||||||
wf(event.Name)
|
|
||||||
}
|
|
||||||
fw.mu.Unlock()
|
|
||||||
}
|
|
||||||
case err, ok := <-watcher.Errors:
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
logging.Error(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// INFO: After closing the watcher, you can't use it anymore.
|
|
||||||
// Also, after a restart, you need to re add the paths
|
|
||||||
func (fw *FileWatcher) Close() {
|
|
||||||
fw.mu.Lock()
|
|
||||||
defer fw.mu.Unlock()
|
|
||||||
|
|
||||||
if fw.watcher != nil {
|
|
||||||
fw.watcher.Close()
|
|
||||||
}
|
|
||||||
fw.watcher = nil
|
|
||||||
fw.paths = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fw *FileWatcher) Restart() {
|
|
||||||
fw.Close()
|
|
||||||
fw.Watch()
|
|
||||||
}
|
|
||||||
59
kgpz_web.go
59
kgpz_web.go
@@ -91,65 +91,6 @@ func Run(app *App) {
|
|||||||
done <- true
|
done <- true
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// INFO: hot reloading for poor people
|
|
||||||
if app.Config.Watch {
|
|
||||||
go func() {
|
|
||||||
_, routesexist := os.Stat(server.ROUTES_FILEPATH)
|
|
||||||
_, layoutexist := os.Stat(server.LAYOUT_FILEPATH)
|
|
||||||
|
|
||||||
if routesexist != nil && layoutexist != nil {
|
|
||||||
logging.Info("Routes or Layout folder does not exist. Watcher disabled.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
watcher, err := helpers.NewFileWatcher()
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
watcher.Append(func(path string) {
|
|
||||||
logging.Info("File changed: ", path)
|
|
||||||
time.Sleep(200 * time.Millisecond)
|
|
||||||
engine := Engine(app.KGPZ, app.Config)
|
|
||||||
app.Server.ClearPre()
|
|
||||||
app.Server.AddPre(engine)
|
|
||||||
app.Server.Engine(engine)
|
|
||||||
})
|
|
||||||
|
|
||||||
if routesexist != nil {
|
|
||||||
err = watcher.RecursiveDir(server.ROUTES_FILEPATH)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if layoutexist != nil {
|
|
||||||
err = watcher.RecursiveDir(server.LAYOUT_FILEPATH)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Interactive listening for input
|
|
||||||
// if k.IsDebug() {
|
|
||||||
// go func() {
|
|
||||||
// for {
|
|
||||||
// var input string
|
|
||||||
// fmt.Scanln(&input)
|
|
||||||
// if input == "r" {
|
|
||||||
// s.Restart()
|
|
||||||
// } else if input == "p" {
|
|
||||||
// k.Pull()
|
|
||||||
// } else if input == "q" {
|
|
||||||
// sigs <- os.Signal(syscall.SIGTERM)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }()
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
<-done
|
<-done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -46,7 +46,6 @@ type Config struct {
|
|||||||
WebHookEndpoint string `json:"webhook_endpoint" envconfig:"WEBHOOK_ENDPOINT"`
|
WebHookEndpoint string `json:"webhook_endpoint" envconfig:"WEBHOOK_ENDPOINT"`
|
||||||
WebHookSecret string `json:"webhook_secret" envconfig:"WEBHOOK_SECRET"`
|
WebHookSecret string `json:"webhook_secret" envconfig:"WEBHOOK_SECRET"`
|
||||||
Debug bool `json:"debug" envconfig:"DEBUG"`
|
Debug bool `json:"debug" envconfig:"DEBUG"`
|
||||||
Watch bool `json:"watch" envconfig:"WATCH"`
|
|
||||||
LogData bool `json:"log_data" envconfig:"LOG_DATA"`
|
LogData bool `json:"log_data" envconfig:"LOG_DATA"`
|
||||||
Environment string `json:"environment" envconfig:"ENVIRONMENT"`
|
Environment string `json:"environment" envconfig:"ENVIRONMENT"`
|
||||||
NoIndex bool `json:"no_index" envconfig:"NO_INDEX"`
|
NoIndex bool `json:"no_index" envconfig:"NO_INDEX"`
|
||||||
|
|||||||
@@ -195,11 +195,6 @@ func (s *Server) Kill() {
|
|||||||
s.shutdown.Wait()
|
s.shutdown.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Restart() {
|
|
||||||
s.Stop()
|
|
||||||
s.Start()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Server) runner(srv *fiber.App) {
|
func (s *Server) runner(srv *fiber.App) {
|
||||||
s.running = make(chan bool)
|
s.running = make(chan bool)
|
||||||
s.shutdown = &sync.WaitGroup{}
|
s.shutdown = &sync.WaitGroup{}
|
||||||
|
|||||||
@@ -457,24 +457,6 @@ func (e *Engine) Load() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Engine) Reload() error {
|
|
||||||
wg := sync.WaitGroup{}
|
|
||||||
wg.Add(2)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer wg.Done()
|
|
||||||
e.LayoutRegistry.Reset()
|
|
||||||
}()
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer wg.Done()
|
|
||||||
e.TemplateRegistry.Reset()
|
|
||||||
}()
|
|
||||||
|
|
||||||
wg.Wait()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// INFO: fn is a function that returns either one value or two values, the second one being an error
|
// INFO: fn is a function that returns either one value or two values, the second one being an error
|
||||||
func (e *Engine) AddFunc(name string, fn interface{}) {
|
func (e *Engine) AddFunc(name string, fn interface{}) {
|
||||||
e.mu.Lock()
|
e.mu.Lock()
|
||||||
|
|||||||
Reference in New Issue
Block a user