Simple blogging system written in Go https://goblog.app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

136 lines
3.1 KiB

package main
import (
"bytes"
"html/template"
"log"
"os/exec"
"time"
)
func (a *goBlog) preStartHooks() {
cfg := a.cfg.Hooks
for _, cmd := range cfg.PreStart {
func(cmd string) {
executeHookCommand("pre-start", cfg.Shell, cmd)
}(cmd)
}
}
type postHookFunc func(*post)
func (a *goBlog) postPostHooks(p *post) {
// Hooks after post published
if hc := a.cfg.Hooks; hc != nil {
for _, cmdTmplString := range hc.PostPost {
go func(p *post, cmdTmplString string) {
a.cfg.Hooks.executeTemplateCommand("post-post", cmdTmplString, map[string]interface{}{
"URL": a.fullPostURL(p),
"Post": p,
})
}(p, cmdTmplString)
}
}
for _, f := range a.pPostHooks {
go f(p)
}
}
func (a *goBlog) postUpdateHooks(p *post) {
// Hooks after post updated
if hc := a.cfg.Hooks; hc != nil {
for _, cmdTmplString := range hc.PostUpdate {
go func(p *post, cmdTmplString string) {
a.cfg.Hooks.executeTemplateCommand("post-update", cmdTmplString, map[string]interface{}{
"URL": a.fullPostURL(p),
"Post": p,
})
}(p, cmdTmplString)
}
}
for _, f := range a.pUpdateHooks {
go f(p)
}
}
func (a *goBlog) postDeleteHooks(p *post) {
if hc := a.cfg.Hooks; hc != nil {
for _, cmdTmplString := range hc.PostDelete {
go func(p *post, cmdTmplString string) {
a.cfg.Hooks.executeTemplateCommand("post-delete", cmdTmplString, map[string]interface{}{
"URL": a.fullPostURL(p),
"Post": p,
})
}(p, cmdTmplString)
}
}
for _, f := range a.pDeleteHooks {
go f(p)
}
}
func (cfg *configHooks) executeTemplateCommand(hookType string, tmpl string, data map[string]interface{}) {
cmdTmpl, err := template.New("cmd").Parse(tmpl)
if err != nil {
log.Println("Failed to parse cmd template:", err.Error())
return
}
var cmdBuf bytes.Buffer
if err = cmdTmpl.Execute(&cmdBuf, data); err != nil {
log.Println("Failed to execute cmd template:", err.Error())
return
}
cmd := cmdBuf.String()
executeHookCommand(hookType, cfg.Shell, cmd)
}
type hourlyHookFunc func()
func (a *goBlog) startHourlyHooks() {
cfg := a.cfg.Hooks
// Add configured hourly hooks
for _, cmd := range cfg.Hourly {
c := cmd
f := func() {
executeHookCommand("hourly", cfg.Shell, c)
}
a.hourlyHooks = append(a.hourlyHooks, f)
}
// When there are hooks, start ticker
if len(a.hourlyHooks) > 0 {
// Wait for next full hour
tr := time.AfterFunc(time.Until(time.Now().Truncate(time.Hour).Add(time.Hour)), func() {
// Execute once
for _, f := range a.hourlyHooks {
go f()
}
// Start ticker and execute regularly
ticker := time.NewTicker(1 * time.Hour)
a.shutdown.Add(func() {
ticker.Stop()
log.Println("Stopped hourly hooks")
})
for range ticker.C {
for _, f := range a.hourlyHooks {
go f()
}
}
})
a.shutdown.Add(func() {
if tr.Stop() {
log.Println("Canceled hourly hooks")
}
})
}
}
func executeHookCommand(hookType, shell, cmd string) {
log.Printf("Executing %v hook: %v", hookType, cmd)
out, err := exec.Command(shell, "-c", cmd).CombinedOutput()
if err != nil {
log.Println("Failed to execute command:", err.Error())
}
if len(out) > 0 {
log.Printf("Output:\n%v", string(out))
}
}