intake/web/main.go

36 lines
705 B
Go
Raw Normal View History

2025-01-24 15:15:54 +00:00
package web
import (
"log"
"net"
"net/http"
"github.com/Jaculabilis/intake/core"
)
type Env struct {
db *core.DB
}
func logged(handler http.HandlerFunc) http.HandlerFunc {
return func(writer http.ResponseWriter, req *http.Request) {
log.Printf("%s %s", req.Method, req.URL.Path)
handler(writer, req)
}
}
func handleFunc(pattern string, handler http.HandlerFunc) {
http.HandleFunc(pattern, logged(handler))
}
func RunServer(db *core.DB, addr string, port string) {
env := &Env{db}
bind := net.JoinHostPort(addr, port)
handleFunc("/", env.rootHandler)
handleFunc("/style.css", env.styleHandler)
handleFunc("/source/", env.sourceHandler)
2025-01-24 15:15:54 +00:00
log.Fatal(http.ListenAndServe(bind, nil))
}