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
type Web struct {
router *gin.Engine
templates multitemplate.Renderer
cron *cron.Cron
urlCache map[string]string
cronWatch map[uint]cron.EntryID
db *gorm.DB
notifiers map[string]notifiers.Notifier
router *gin.Engine
templates multitemplate.Renderer
cron *cron.Cron
urlCache map[string]string
cronWatch map[uint]cron.EntryID
db *gorm.DB
notifiers map[string]notifiers.Notifier
startupWarnings []string
}
func newWeb() *Web {
web := &Web{
urlCache: make(map[string]string, 5),
urlCache: make(map[string]string, 5),
startupWarnings: make([]string, 0, 10),
}
web.init()
return web
@ -60,11 +62,17 @@ func (web *Web) init() {
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() {
if viper.IsSet("proxy.proxy_url") {
_, err := url.Parse(viper.GetString("proxy.proxy_url"))
if err != nil {
log.Println("Could not parse proxy url, check config")
web.startupWarning("Could not parse proxy url, check config")
return
}
}
@ -156,7 +164,7 @@ func (web *Web) initCronJobs() {
cronFilter := &cronFilters[i]
entryID, err := web.cron.AddFunc(cronFilter.Var1, func() { triggerSchedule(cronFilter.WatchID, web, &cronFilter.ID) })
if err != nil {
log.Println("Could not start job for Watch: ", cronFilter.WatchID)
web.startupWarning("Could not start job for Watch: ", cronFilter.WatchID)
continue
}
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")
_, err := web.cron.AddFunc(pruneSchedule, web.pruneDB)
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)
}
@ -176,7 +184,7 @@ func (web *Web) initCronJobs() {
func (web *Web) initNotifiers() {
web.notifiers = make(map[string]notifiers.Notifier, 5)
if !viper.IsSet("notifiers") {
log.Println("No notifiers set!")
web.startupWarning("No notifiers set!")
return
}
notifiersMap := viper.GetStringMap("notifiers")
@ -186,7 +194,7 @@ func (web *Web) initNotifiers() {
notifierType, exists := notifierMap["type"]
if !exists {
log.Printf("No 'type' for '%s' notifier!", name)
web.startupWarning(fmt.Sprintf("No 'type' for '%s' notifier!", name))
continue
}
success := false
@ -230,11 +238,13 @@ func (web *Web) initNotifiers() {
}
default:
{
log.Println("Did not recognize notifier type:", notifierType)
web.startupWarning("Did not recognize notifier type:", notifierType)
}
}
if success {
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
}
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) {

View file

@ -3,6 +3,18 @@ GoWatch
{{end}}
{{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">
<thead class="table-dark">
<tr>
@ -14,7 +26,7 @@ GoWatch
</tr>
</thead>
<tbody>
{{ range . }}
{{ range .watches }}
<tr class="pointer" onclick="window.location='/watch/view/{{ .ID }}'">
<td class="h3">{{ .Name }}</td>
{{ if .CronEntry }}

View file

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