intake/web/main.go
Tim Van Baak af77322755 Add link to item page
This will be useful later for editing items
2025-01-27 20:28:46 -08:00

37 lines
765 B
Go

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("GET /", env.getRoot)
handleFunc("GET /style.css", env.getStyle)
handleFunc("GET /source/{source}", env.getSource)
handleFunc("GET /item/{source}/{id}", env.getItem)
log.Fatal(http.ListenAndServe(bind, nil))
}