http router

This commit is contained in:
Niko Abeler 2022-08-01 21:06:48 +02:00
parent 9d14e1a8fc
commit c39d8ea01d
5 changed files with 55 additions and 46 deletions

View File

@ -2,54 +2,54 @@ package main
import (
"h4kor/kiss-social"
"h4kor/kiss-social/cmd/kiss-web/static"
"net/http"
"os"
"strconv"
"strings"
"github.com/julienschmidt/httprouter"
)
func handler(repo kiss.Repository) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
// normalize the path
path := r.URL.Path
// remove leading '/'
if len(path) > 0 && path[0] == '/' {
path = path[1:]
}
// remove trailing '/'
if len(path) > 0 && path[len(path)-1] == '/' {
path = path[:len(path)-1]
}
// func handler(repo kiss.Repository) func(http.ResponseWriter, *http.Request) {
// return func(w http.ResponseWriter, r *http.Request) {
// // normalize the path
// path := r.URL.Path
// // remove leading '/'
// if len(path) > 0 && path[0] == '/' {
// path = path[1:]
// }
// // remove trailing '/'
// if len(path) > 0 && path[len(path)-1] == '/' {
// path = path[:len(path)-1]
// }
// index page
if path == "" {
println("Index page")
indexHandler(repo)(w, r)
return
}
// // index page
// if path == "" {
// println("Index page")
// indexHandler(repo)(w, r)
// return
// }
// parse the path
parts := strings.Split(path, "/")
userName := parts[0]
// // parse the path
// parts := strings.Split(path, "/")
// userName := parts[0]
// only one part -> user page
if len(parts) == 1 {
println("User page")
userHandler(repo, userName)(w, r)
return
}
// // only one part -> user page
// if len(parts) == 1 {
// println("User page")
// userHandler(repo, userName)(w, r)
// return
// }
// multiple parts -> post page
println("Post page")
postId := strings.Join(parts[1:], "/")
postHandler(repo, userName, postId)(w, r)
// // multiple parts -> post page
// println("Post page")
// postId := strings.Join(parts[1:], "/")
// postHandler(repo, userName, postId)(w, r)
}
}
// }
// }
func indexHandler(repo kiss.Repository) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
func indexHandler(repo kiss.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
html, err := kiss.RenderUserList(repo)
if err != nil {
@ -63,8 +63,9 @@ func indexHandler(repo kiss.Repository) func(http.ResponseWriter, *http.Request)
}
}
func userHandler(repo kiss.Repository, userName string) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
func userHandler(repo kiss.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
userName := ps.ByName("user")
user, err := repo.GetUser(userName)
if err != nil {
println("Error getting user: ", err.Error())
@ -84,8 +85,10 @@ func userHandler(repo kiss.Repository, userName string) func(http.ResponseWriter
}
}
func postHandler(repo kiss.Repository, userName string, postId string) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
func postHandler(repo kiss.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
userName := ps.ByName("user")
postId := ps.ByName("post")
user, err := repo.GetUser(userName)
if err != nil {
println("Error getting user: ", err.Error())
@ -141,10 +144,13 @@ func main() {
os.Exit(1)
}
http.Handle("/static/", static.StaticHandler(repo))
http.HandleFunc("/", handler(repo))
router := httprouter.New()
router.ServeFiles("/static/*filepath", http.Dir(repo.StaticDir()))
router.GET("/", indexHandler(repo))
router.GET("/user/:user", userHandler(repo))
router.GET("/user/:user/posts/*post", postHandler(repo))
println("Listening on port", port)
http.ListenAndServe(":"+strconv.Itoa(port), nil)
http.ListenAndServe(":"+strconv.Itoa(port), router)
}

1
go.mod
View File

@ -3,6 +3,7 @@ module h4kor/kiss-social
go 1.18
require (
github.com/julienschmidt/httprouter v1.3.0 // indirect
github.com/yuin/goldmark v1.4.13 // indirect
github.com/yuin/goldmark-meta v1.1.0 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect

2
go.sum
View File

@ -1,3 +1,5 @@
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yuin/goldmark-meta v1.1.0 h1:pWw+JLHGZe8Rk0EGsMVssiNb/AaPMHfSRszZeUeiOUc=

View File

@ -26,7 +26,7 @@ func (post Post) MediaDir() string {
}
func (post Post) UrlPath() string {
return post.user.Path() + "/" + post.id
return post.user.Path() + "/posts/" + post.id
}
func (post Post) Title() string {

View File

@ -27,7 +27,7 @@ func (user User) Dir() string {
}
func (user User) Path() string {
return "/" + user.name
return "/user/" + user.name
}
func (user User) PostDir() string {