owl-blogs/cmd/owl-web/handler.go

133 lines
3.6 KiB
Go
Raw Normal View History

2022-08-03 17:41:13 +00:00
package main
import (
"h4kor/owl-blogs"
"net/http"
2022-08-03 17:43:00 +00:00
"os"
"path"
2022-08-03 17:41:13 +00:00
"github.com/julienschmidt/httprouter"
)
2022-08-06 18:22:09 +00:00
func getUserFromRepo(repo *owl.Repository, ps httprouter.Params) (owl.User, error) {
2022-08-03 18:48:47 +00:00
if repo.SingleUserName() != "" {
return repo.GetUser(repo.SingleUserName())
}
userName := ps.ByName("user")
user, err := repo.GetUser(userName)
if err != nil {
return owl.User{}, err
}
return user, nil
}
2022-08-06 18:22:09 +00:00
func repoIndexHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
2022-08-03 17:41:13 +00:00
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
2022-08-06 18:22:09 +00:00
html, err := owl.RenderUserList(*repo)
2022-08-03 17:41:13 +00:00
if err != nil {
println("Error rendering index: ", err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Internal server error"))
return
}
println("Rendering index")
w.Write([]byte(html))
}
}
2022-08-03 17:43:00 +00:00
2022-08-06 18:22:09 +00:00
func userIndexHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
2022-08-03 17:43:00 +00:00
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
2022-08-03 18:48:47 +00:00
user, err := getUserFromRepo(repo, ps)
2022-08-03 17:43:00 +00:00
if err != nil {
println("Error getting user: ", err.Error())
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("User not found"))
return
}
html, err := owl.RenderIndexPage(user)
if err != nil {
println("Error rendering index page: ", err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Internal server error"))
return
}
2022-08-03 18:48:47 +00:00
println("Rendering index page for user", user.Name())
2022-08-03 17:43:00 +00:00
w.Write([]byte(html))
}
}
2022-08-06 18:22:09 +00:00
func postHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
2022-08-03 17:43:00 +00:00
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
postId := ps.ByName("post")
2022-08-03 18:48:47 +00:00
user, err := getUserFromRepo(repo, ps)
2022-08-03 17:43:00 +00:00
if err != nil {
println("Error getting user: ", err.Error())
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("User not found"))
return
}
post, err := user.GetPost(postId)
if err != nil {
println("Error getting post: ", err.Error())
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("Post not found"))
return
}
html, err := owl.RenderPost(post)
if err != nil {
println("Error rendering post: ", err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Internal server error"))
return
}
println("Rendering post", postId)
w.Write([]byte(html))
}
}
2022-08-06 18:22:09 +00:00
func postMediaHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
2022-08-03 17:43:00 +00:00
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
postId := ps.ByName("post")
filepath := ps.ByName("filepath")
2022-08-03 18:48:47 +00:00
user, err := getUserFromRepo(repo, ps)
2022-08-03 17:43:00 +00:00
if err != nil {
println("Error getting user: ", err.Error())
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("User not found"))
return
}
post, err := user.GetPost(postId)
if err != nil {
println("Error getting post: ", err.Error())
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("Post not found"))
return
}
filepath = path.Join(post.MediaDir(), filepath)
if _, err := os.Stat(filepath); err != nil {
println("Error getting file: ", err.Error())
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("File not found"))
return
}
http.ServeFile(w, r, filepath)
}
}
2022-08-06 18:17:39 +00:00
2022-08-06 18:22:09 +00:00
func notFoundHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Request) {
2022-08-06 18:17:39 +00:00
return func(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
aliases, _ := repo.PostAliases()
if _, ok := aliases[path]; ok {
http.Redirect(w, r, aliases[path].UrlPath(), http.StatusMovedPermanently)
return
}
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("Not found"))
}
}