added display of startup warnings on index page

This commit is contained in:
BroodjeAap 2022-12-31 13:04:46 +00:00
parent 756326e193
commit 2dcfa7be74
3 changed files with 41 additions and 17 deletions

43
main.go
View file

@ -34,18 +34,20 @@ import (
var EMBED_FS embed.FS var EMBED_FS embed.FS
type Web struct { type Web struct {
router *gin.Engine router *gin.Engine
templates multitemplate.Renderer templates multitemplate.Renderer
cron *cron.Cron cron *cron.Cron
urlCache map[string]string urlCache map[string]string
cronWatch map[uint]cron.EntryID cronWatch map[uint]cron.EntryID
db *gorm.DB db *gorm.DB
notifiers map[string]notifiers.Notifier notifiers map[string]notifiers.Notifier
startupWarnings []string
} }
func newWeb() *Web { func newWeb() *Web {
web := &Web{ web := &Web{
urlCache: make(map[string]string, 5), urlCache: make(map[string]string, 5),
startupWarnings: make([]string, 0, 10),
} }
web.init() web.init()
return web return web
@ -60,11 +62,17 @@ func (web *Web) init() {
web.initNotifiers() web.initNotifiers()
} }
func (web *Web) startupWarning(m ...any) {
warning := fmt.Sprint(m...)
log.Println(warning)
web.startupWarnings = append(web.startupWarnings, warning)
}
func (web *Web) validateProxyURL() { func (web *Web) validateProxyURL() {
if viper.IsSet("proxy.proxy_url") { if viper.IsSet("proxy.proxy_url") {
_, err := url.Parse(viper.GetString("proxy.proxy_url")) _, err := url.Parse(viper.GetString("proxy.proxy_url"))
if err != nil { if err != nil {
log.Println("Could not parse proxy url, check config") web.startupWarning("Could not parse proxy url, check config")
return return
} }
} }
@ -156,7 +164,7 @@ func (web *Web) initCronJobs() {
cronFilter := &cronFilters[i] cronFilter := &cronFilters[i]
entryID, err := web.cron.AddFunc(cronFilter.Var1, func() { triggerSchedule(cronFilter.WatchID, web, &cronFilter.ID) }) entryID, err := web.cron.AddFunc(cronFilter.Var1, func() { triggerSchedule(cronFilter.WatchID, web, &cronFilter.ID) })
if err != nil { if err != nil {
log.Println("Could not start job for Watch: ", cronFilter.WatchID) web.startupWarning("Could not start job for Watch: ", cronFilter.WatchID)
continue continue
} }
log.Println("Started CronJob for WatchID", cronFilter.WatchID, "with schedule:", cronFilter.Var1) log.Println("Started CronJob for WatchID", cronFilter.WatchID, "with schedule:", cronFilter.Var1)
@ -166,7 +174,7 @@ func (web *Web) initCronJobs() {
pruneSchedule := viper.GetString("database.prune") pruneSchedule := viper.GetString("database.prune")
_, err := web.cron.AddFunc(pruneSchedule, web.pruneDB) _, err := web.cron.AddFunc(pruneSchedule, web.pruneDB)
if err != nil { if err != nil {
log.Fatalln("Could not parse database.prune:", pruneSchedule) web.startupWarning("Could not parse database.prune:", err)
} }
log.Println("Started DB prune cronjob:", pruneSchedule) log.Println("Started DB prune cronjob:", pruneSchedule)
} }
@ -176,7 +184,7 @@ func (web *Web) initCronJobs() {
func (web *Web) initNotifiers() { func (web *Web) initNotifiers() {
web.notifiers = make(map[string]notifiers.Notifier, 5) web.notifiers = make(map[string]notifiers.Notifier, 5)
if !viper.IsSet("notifiers") { if !viper.IsSet("notifiers") {
log.Println("No notifiers set!") web.startupWarning("No notifiers set!")
return return
} }
notifiersMap := viper.GetStringMap("notifiers") notifiersMap := viper.GetStringMap("notifiers")
@ -186,7 +194,7 @@ func (web *Web) initNotifiers() {
notifierType, exists := notifierMap["type"] notifierType, exists := notifierMap["type"]
if !exists { if !exists {
log.Printf("No 'type' for '%s' notifier!", name) web.startupWarning(fmt.Sprintf("No 'type' for '%s' notifier!", name))
continue continue
} }
success := false success := false
@ -230,11 +238,13 @@ func (web *Web) initNotifiers() {
} }
default: default:
{ {
log.Println("Did not recognize notifier type:", notifierType) web.startupWarning("Did not recognize notifier type:", notifierType)
} }
} }
if success { if success {
web.notifiers[name] = notifier web.notifiers[name] = notifier
} else {
web.startupWarning("Could not add notifier:", name)
} }
} }
} }
@ -303,7 +313,10 @@ func (web *Web) index(c *gin.Context) {
watchMap[filter.WatchID].CronEntry = &entry watchMap[filter.WatchID].CronEntry = &entry
} }
c.HTML(http.StatusOK, "index", watches) c.HTML(http.StatusOK, "index", gin.H{
"watches": watches,
"warnings": web.startupWarnings,
})
} }
func (web *Web) watchCreate(c *gin.Context) { func (web *Web) watchCreate(c *gin.Context) {

View file

@ -3,6 +3,18 @@ GoWatch
{{end}} {{end}}
{{define "content"}} {{define "content"}}
{{ if .warnings }}
<div class="h3 text-center text-danger" id="logHeader">Startup Warnings</div>
<table class="table table-striped table-hover">
<tbody>
{{ range .warnings }}
<tr>
<td class="h5 text-center text-danger">{{ . }}</td>
</tr>
{{ end }}
</tbody>
</table>
{{ end }}
<table class="table table-striped table-hover"> <table class="table table-striped table-hover">
<thead class="table-dark"> <thead class="table-dark">
<tr> <tr>
@ -14,7 +26,7 @@ GoWatch
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{{ range . }} {{ range .watches }}
<tr class="pointer" onclick="window.location='/watch/view/{{ .ID }}'"> <tr class="pointer" onclick="window.location='/watch/view/{{ .ID }}'">
<td class="h3">{{ .Name }}</td> <td class="h3">{{ .Name }}</td>
{{ if .CronEntry }} {{ if .CronEntry }}

View file

@ -1,7 +1,6 @@
# Todo # Todo
- comments - comments
- run/fix staticcheck - run/fix staticcheck
- show startup warnings on page?
- add compose templates for: - add compose templates for:
- sqlite - sqlite
- sqlite+apprise - sqlite+apprise