http router
This commit is contained in:
parent
9d14e1a8fc
commit
c39d8ea01d
|
@ -2,54 +2,54 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"h4kor/kiss-social"
|
"h4kor/kiss-social"
|
||||||
"h4kor/kiss-social/cmd/kiss-web/static"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
|
"github.com/julienschmidt/httprouter"
|
||||||
)
|
)
|
||||||
|
|
||||||
func handler(repo kiss.Repository) func(http.ResponseWriter, *http.Request) {
|
// func handler(repo kiss.Repository) func(http.ResponseWriter, *http.Request) {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
// return func(w http.ResponseWriter, r *http.Request) {
|
||||||
// normalize the path
|
// // normalize the path
|
||||||
path := r.URL.Path
|
// path := r.URL.Path
|
||||||
// remove leading '/'
|
// // remove leading '/'
|
||||||
if len(path) > 0 && path[0] == '/' {
|
// if len(path) > 0 && path[0] == '/' {
|
||||||
path = path[1:]
|
// path = path[1:]
|
||||||
}
|
// }
|
||||||
// remove trailing '/'
|
// // remove trailing '/'
|
||||||
if len(path) > 0 && path[len(path)-1] == '/' {
|
// if len(path) > 0 && path[len(path)-1] == '/' {
|
||||||
path = path[:len(path)-1]
|
// path = path[:len(path)-1]
|
||||||
}
|
// }
|
||||||
|
|
||||||
// index page
|
// // index page
|
||||||
if path == "" {
|
// if path == "" {
|
||||||
println("Index page")
|
// println("Index page")
|
||||||
indexHandler(repo)(w, r)
|
// indexHandler(repo)(w, r)
|
||||||
return
|
// return
|
||||||
}
|
// }
|
||||||
|
|
||||||
// parse the path
|
// // parse the path
|
||||||
parts := strings.Split(path, "/")
|
// parts := strings.Split(path, "/")
|
||||||
userName := parts[0]
|
// userName := parts[0]
|
||||||
|
|
||||||
// only one part -> user page
|
// // only one part -> user page
|
||||||
if len(parts) == 1 {
|
// if len(parts) == 1 {
|
||||||
println("User page")
|
// println("User page")
|
||||||
userHandler(repo, userName)(w, r)
|
// userHandler(repo, userName)(w, r)
|
||||||
return
|
// return
|
||||||
}
|
// }
|
||||||
|
|
||||||
// multiple parts -> post page
|
// // multiple parts -> post page
|
||||||
println("Post page")
|
// println("Post page")
|
||||||
postId := strings.Join(parts[1:], "/")
|
// postId := strings.Join(parts[1:], "/")
|
||||||
postHandler(repo, userName, postId)(w, r)
|
// postHandler(repo, userName, postId)(w, r)
|
||||||
|
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
func indexHandler(repo kiss.Repository) func(http.ResponseWriter, *http.Request) {
|
func indexHandler(repo kiss.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||||
html, err := kiss.RenderUserList(repo)
|
html, err := kiss.RenderUserList(repo)
|
||||||
|
|
||||||
if err != nil {
|
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) {
|
func userHandler(repo kiss.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||||
|
userName := ps.ByName("user")
|
||||||
user, err := repo.GetUser(userName)
|
user, err := repo.GetUser(userName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
println("Error getting user: ", err.Error())
|
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) {
|
func postHandler(repo kiss.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||||
|
userName := ps.ByName("user")
|
||||||
|
postId := ps.ByName("post")
|
||||||
user, err := repo.GetUser(userName)
|
user, err := repo.GetUser(userName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
println("Error getting user: ", err.Error())
|
println("Error getting user: ", err.Error())
|
||||||
|
@ -141,10 +144,13 @@ func main() {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
http.Handle("/static/", static.StaticHandler(repo))
|
router := httprouter.New()
|
||||||
http.HandleFunc("/", handler(repo))
|
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)
|
println("Listening on port", port)
|
||||||
http.ListenAndServe(":"+strconv.Itoa(port), nil)
|
http.ListenAndServe(":"+strconv.Itoa(port), router)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -3,6 +3,7 @@ module h4kor/kiss-social
|
||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/julienschmidt/httprouter v1.3.0 // indirect
|
||||||
github.com/yuin/goldmark v1.4.13 // indirect
|
github.com/yuin/goldmark v1.4.13 // indirect
|
||||||
github.com/yuin/goldmark-meta v1.1.0 // indirect
|
github.com/yuin/goldmark-meta v1.1.0 // indirect
|
||||||
gopkg.in/yaml.v2 v2.3.0 // indirect
|
gopkg.in/yaml.v2 v2.3.0 // indirect
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -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 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE=
|
||||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
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=
|
github.com/yuin/goldmark-meta v1.1.0 h1:pWw+JLHGZe8Rk0EGsMVssiNb/AaPMHfSRszZeUeiOUc=
|
||||||
|
|
2
post.go
2
post.go
|
@ -26,7 +26,7 @@ func (post Post) MediaDir() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (post Post) UrlPath() string {
|
func (post Post) UrlPath() string {
|
||||||
return post.user.Path() + "/" + post.id
|
return post.user.Path() + "/posts/" + post.id
|
||||||
}
|
}
|
||||||
|
|
||||||
func (post Post) Title() string {
|
func (post Post) Title() string {
|
||||||
|
|
Loading…
Reference in New Issue