Compare commits

...

3 commits
main ... method

Author SHA1 Message Date
Felix Niederwanger 2e55b23f7f
tmp
Staging commit
2024-02-24 10:44:28 +01:00
Felix Niederwanger d5f79eae01 Merge pull request 'Add default page' (#20) from default into main
Reviewed-on: https://codeberg.org/grisu48/weblug/pulls/20
2023-11-25 22:13:14 +00:00
Felix Niederwanger 9713b0d2c1
Add default page
The default page handler now also accepts / as URL path for the default
page.
2023-11-25 23:12:51 +01:00
3 changed files with 47 additions and 16 deletions

View file

@ -2,7 +2,7 @@ package main
import (
"fmt"
"io/ioutil"
"os"
"gopkg.in/yaml.v2"
)
@ -54,7 +54,7 @@ func (cf *Config) Check() error {
}
func (cf *Config) LoadYAML(filename string) error {
content, err := ioutil.ReadFile(filename)
content, err := os.ReadFile(filename)
if err != nil {
return err
}

View file

@ -12,6 +12,7 @@ import (
type Hook struct {
Name string `yaml:"name"` // name of the hook
Route string `yaml:"route"` // http route
Methods []string `yaml:"methods"` // acceptable http methods
Command string `yaml:"command"` // Actual command to execute
Background bool `yaml:"background"` // Run in background
Concurrency int `yaml:"concurrency"` // Number of allowed concurrent runs
@ -180,3 +181,16 @@ func (hook *Hook) IsAddressAllowed(addr string) (bool, error) {
return true, nil
}
// mapHooks creates a map[string][]Hook instance based on the hooks route.
func mapHooks(hooks []Hook) map[string][]Hook {
ret := make(map[string][]Hook, 0)
for _, hook := range hooks {
route := hook.Route
if _, ok := ret[route]; !ok {
ret[route] = make([]Hook, 0)
}
ret[route] = append(ret[route], hook)
}
return ret
}

View file

@ -9,6 +9,7 @@ import (
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
)
@ -52,6 +53,12 @@ func sanityCheckHooks(hooks []Hook) error {
if hook.GID != 0 && uid != 0 {
fmt.Fprintf(os.Stderr, "Warning: Hook '%s' sets 'gid = %d' but we're not running as root\n", hook.Name, hook.GID)
}
if hook.Concurrency < 1 {
return fmt.Errorf("'%s' has concurrency < 1\n", hook.Name)
}
if hook.Route == "" {
return fmt.Errorf("'%s' defines no route\n", hook.Name)
}
}
return nil
}
@ -101,23 +108,24 @@ func main() {
}
// Create default handlers
http.HandleFunc("/", createDefaultHandler())
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)
hooks := mapHooks(cf.Hooks)
for route, hooks := range hooks {
if route == "" {
panic("invalid route: <empty>")
}
if hook.Concurrency < 1 {
hook.Concurrency = 1
if len(hooks) == 1 {
log.Printf("Webhook for %s - '%s'\n", route, hooks[0].Name)
} else {
log.Printf("Webhooks for %s: %d\n", route, len(hooks))
}
log.Printf("Webhook %d: '%s' [%s] \"%s\"\n", i, hook.Name, hook.Route, hook.Command)
http.HandleFunc(hook.Route, createHandler(hook))
http.HandleFunc(route, createHandler(hooks))
}
awaitTerminationSignal()
@ -135,9 +143,10 @@ func main() {
}
// create a http handler function from the given hook
func createHandler(hook Hook) Handler {
func createHandler(hooks []Hook) Handler {
return func(w http.ResponseWriter, r *http.Request) {
log.Printf("GET %s %s", r.RemoteAddr, hook.Name)
method := strings.ToUpper(r.Method)
log.Printf("%s %s %s", method, r.RemoteAddr, hook.Name)
// Check if adresses are allowed or blocked
allowed, err := hook.IsAddressAllowed(r.RemoteAddr)
@ -223,8 +232,16 @@ func createHealthHandler() Handler {
func createDefaultHandler() Handler {
return func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
fmt.Fprintf(w, "weblug - webhook receiver program\nSee https://codeberg.org/grisu48/weblug\n")
if r.URL.Path == "/" || r.URL.Path == "/index.txt" {
w.WriteHeader(200)
fmt.Fprintf(w, "weblug - webhook receiver program\nhttps://codeberg.org/grisu48/weblug\n")
} else if r.URL.Path == "/index.htm" || r.URL.Path == "/index.html" {
w.WriteHeader(200)
fmt.Fprintf(w, "<!DOCTYPE html><html><head><title>weblug</title></head>\n<body><p><a href=\"https://codeberg.org/grisu48/weblug\">weblug</a> - webhook receiver program</p>\n</body></html>")
} else {
w.WriteHeader(404)
fmt.Fprintf(w, "not found\n")
}
}
}