113 lines
2.7 KiB
Go
113 lines
2.7 KiB
Go
/*
|
|
* weblug main program
|
|
*/
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
)
|
|
|
|
var cf Config
|
|
var hooks []Hook
|
|
|
|
type Handler func(http.ResponseWriter, *http.Request)
|
|
|
|
func main() {
|
|
cf.SetDefaults()
|
|
if err := cf.LoadYAML(("../../weblug.yaml")); err != nil {
|
|
fmt.Fprintf(os.Stderr, "yaml error: %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
if len(cf.Hooks) == 0 {
|
|
fmt.Fprintf(os.Stderr, "error: no webhooks defined\n")
|
|
os.Exit(2)
|
|
}
|
|
|
|
// Create default handlers
|
|
http.HandleFunc("/health", createHealthHandler())
|
|
http.HandleFunc("/health.json", createHealthHandler())
|
|
http.HandleFunc("/index", createDefaultHandler())
|
|
http.HandleFunc("/index.htm", createDefaultHandler())
|
|
http.HandleFunc("/index.html", createDefaultHandler())
|
|
http.HandleFunc("/robots.txt", createRobotsHandler())
|
|
|
|
// Register hooks
|
|
for i, hook := range cf.Hooks {
|
|
if hook.Route == "" {
|
|
fmt.Fprintf(os.Stderr, "Invalid hook %s: No route defined\n", hook.Name)
|
|
}
|
|
log.Printf("Webhook %d: '%s' [%s] \"%s\"\n", i, hook.Name, hook.Route, hook.Command)
|
|
http.HandleFunc(hook.Route, createHandler(hook))
|
|
}
|
|
|
|
// TODO: Await termination signal
|
|
|
|
log.Printf("Launching webserver on %s", cf.Settings.BindAddress)
|
|
log.Fatal(http.ListenAndServe(cf.Settings.BindAddress, nil))
|
|
}
|
|
|
|
// Execute the given command and return it's return code
|
|
func execute(command string) error {
|
|
split := strings.Split(command, " ")
|
|
args := make([]string, 0)
|
|
if len(split) > 1 {
|
|
args = split[1:]
|
|
}
|
|
cmd := exec.Command(split[0], args...)
|
|
return cmd.Run()
|
|
}
|
|
|
|
// create a http handler function from the given hook
|
|
func createHandler(hook Hook) Handler {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
log.Printf("%s %s", r.RemoteAddr, hook.Name)
|
|
if hook.Background { // Execute command in background
|
|
w.WriteHeader(200)
|
|
fmt.Fprintf(w, "{\"status\":\"ok\"}")
|
|
go func() {
|
|
if err := execute(hook.Command); err != nil {
|
|
log.Printf("Hook %s failed: %s", hook.Name, err)
|
|
} else {
|
|
log.Printf("Hook %s completed", hook.Name)
|
|
}
|
|
}()
|
|
} else {
|
|
if err := execute(hook.Command); err != nil {
|
|
log.Printf("Hook %s failed: %s", hook.Name, err)
|
|
w.WriteHeader(500)
|
|
fmt.Fprintf(w, "{\"status\":\"fail\"}")
|
|
} else {
|
|
w.WriteHeader(200)
|
|
fmt.Fprintf(w, "{\"status\":\"ok\"}")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func createHealthHandler() Handler {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(200)
|
|
fmt.Fprintf(w, "{\"status\":\"ok\"}")
|
|
}
|
|
}
|
|
|
|
func createDefaultHandler() Handler {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(200)
|
|
fmt.Fprintf(w, "weblug")
|
|
}
|
|
}
|
|
|
|
func createRobotsHandler() Handler {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(200)
|
|
fmt.Fprintf(w, "User-agent: *\nDisallow: /")
|
|
}
|
|
}
|