Use go.1.22 routing style

This commit is contained in:
Tim Van Baak 2025-01-27 16:38:02 -08:00
parent c18cc73496
commit c49b6c9088
4 changed files with 18 additions and 7 deletions

View File

@ -1,3 +1,4 @@
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">

View File

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

View File

@ -7,7 +7,12 @@ import (
"github.com/Jaculabilis/intake/web/html"
)
func (env *Env) rootHandler(writer http.ResponseWriter, req *http.Request) {
func (env *Env) getRoot(writer http.ResponseWriter, req *http.Request) {
if req.URL.Path != "" {
http.NotFound(writer, req)
return
}
names, err := core.GetSources(env.db)
if err != nil {
writer.Write([]byte(err.Error()))
@ -23,7 +28,7 @@ func (env *Env) rootHandler(writer http.ResponseWriter, req *http.Request) {
html.Home(writer, data)
}
func (env *Env) styleHandler(writer http.ResponseWriter, req *http.Request) {
func (env *Env) getStyle(writer http.ResponseWriter, req *http.Request) {
writer.Header()["Cache-Control"] = []string{"public, max-age=86400"}
writer.Header()["Content-Type"] = []string{"text/css; charset=utf-8"}
writer.Write(html.Stylesheet)

View File

@ -7,8 +7,13 @@ import (
"github.com/Jaculabilis/intake/web/html"
)
func (env *Env) sourceHandler(writer http.ResponseWriter, req *http.Request) {
source := req.URL.Path[len("/source/"):]
func (env *Env) getSource(writer http.ResponseWriter, req *http.Request) {
source := req.PathValue("source")
if source == "" {
http.NotFound(writer, req)
return
}
// TODO this needs to properly error if the source doesn't exist instead of just returning []
items, err := core.GetAllItemsForSource(env.db, source)
if err != nil {