Use go.1.22 routing style
This commit is contained in:
parent
c18cc73496
commit
c49b6c9088
@ -1,3 +1,4 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
@ -27,9 +27,9 @@ func RunServer(db *core.DB, addr string, port string) {
|
|||||||
env := &Env{db}
|
env := &Env{db}
|
||||||
bind := net.JoinHostPort(addr, port)
|
bind := net.JoinHostPort(addr, port)
|
||||||
|
|
||||||
handleFunc("/", env.rootHandler)
|
handleFunc("GET /", env.getRoot)
|
||||||
handleFunc("/style.css", env.styleHandler)
|
handleFunc("GET /style.css", env.getStyle)
|
||||||
handleFunc("/source/", env.sourceHandler)
|
handleFunc("GET /source/{source}", env.getSource)
|
||||||
|
|
||||||
log.Fatal(http.ListenAndServe(bind, nil))
|
log.Fatal(http.ListenAndServe(bind, nil))
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,12 @@ import (
|
|||||||
"github.com/Jaculabilis/intake/web/html"
|
"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)
|
names, err := core.GetSources(env.db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writer.Write([]byte(err.Error()))
|
writer.Write([]byte(err.Error()))
|
||||||
@ -23,7 +28,7 @@ func (env *Env) rootHandler(writer http.ResponseWriter, req *http.Request) {
|
|||||||
html.Home(writer, data)
|
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()["Cache-Control"] = []string{"public, max-age=86400"}
|
||||||
writer.Header()["Content-Type"] = []string{"text/css; charset=utf-8"}
|
writer.Header()["Content-Type"] = []string{"text/css; charset=utf-8"}
|
||||||
writer.Write(html.Stylesheet)
|
writer.Write(html.Stylesheet)
|
||||||
|
@ -7,8 +7,13 @@ import (
|
|||||||
"github.com/Jaculabilis/intake/web/html"
|
"github.com/Jaculabilis/intake/web/html"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (env *Env) sourceHandler(writer http.ResponseWriter, req *http.Request) {
|
func (env *Env) getSource(writer http.ResponseWriter, req *http.Request) {
|
||||||
source := req.URL.Path[len("/source/"):]
|
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 []
|
// TODO this needs to properly error if the source doesn't exist instead of just returning []
|
||||||
items, err := core.GetAllItemsForSource(env.db, source)
|
items, err := core.GetAllItemsForSource(env.db, source)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user