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

140 lines
3.7 KiB
Go
Raw Normal View History

2022-07-23 15:19:47 +00:00
package main
import (
2022-08-03 14:55:48 +00:00
"h4kor/owl-blogs"
2022-07-23 15:19:47 +00:00
"net/http"
"os"
2022-08-03 16:16:35 +00:00
"path"
2022-07-23 15:19:47 +00:00
"strconv"
2022-08-01 19:06:48 +00:00
"github.com/julienschmidt/httprouter"
)
2022-07-23 15:19:47 +00:00
2022-08-03 14:55:48 +00:00
func userHandler(repo owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
2022-08-01 19:06:48 +00:00
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
userName := ps.ByName("user")
2022-07-23 15:19:47 +00:00
user, err := repo.GetUser(userName)
if err != nil {
println("Error getting user: ", err.Error())
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("User not found"))
return
}
2022-08-03 14:55:48 +00:00
html, err := owl.RenderIndexPage(user)
2022-07-23 15:19:47 +00:00
if err != nil {
println("Error rendering index page: ", err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Internal server error"))
return
}
println("Rendering index page for user", userName)
w.Write([]byte(html))
}
}
2022-08-03 14:55:48 +00:00
func postHandler(repo owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
2022-08-01 19:06:48 +00:00
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
userName := ps.ByName("user")
postId := ps.ByName("post")
2022-07-23 15:19:47 +00:00
user, err := repo.GetUser(userName)
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
}
2022-08-03 14:55:48 +00:00
html, err := owl.RenderPost(post)
2022-07-23 15:19:47 +00:00
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-03 16:16:35 +00:00
func postMediaHandler(repo owl.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")
filepath := ps.ByName("filepath")
user, err := repo.GetUser(userName)
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)
2022-08-03 17:41:13 +00:00
if _, err := os.Stat(filepath); err != nil {
println("Error getting file: ", err.Error())
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("File not found"))
return
}
2022-08-03 16:16:35 +00:00
http.ServeFile(w, r, filepath)
}
}
2022-08-03 17:23:37 +00:00
func Router(repo owl.Repository) http.Handler {
router := httprouter.New()
router.ServeFiles("/static/*filepath", http.Dir(repo.StaticDir()))
router.GET("/", RepoIndexHandler(repo))
router.GET("/user/:user/", userHandler(repo))
router.GET("/user/:user/posts/:post/", postHandler(repo))
router.GET("/user/:user/posts/:post/media/*filepath", postMediaHandler(repo))
return router
}
2022-07-23 15:19:47 +00:00
func main() {
2022-08-03 14:55:48 +00:00
println("owl web server")
2022-07-23 15:19:47 +00:00
println("Parameters")
println("-repo <repo> - Specify the repository to use. Defaults to '.'")
println("-port <port> - Specify the port to use, Default is '8080'")
var repoName string
var port int
for i, arg := range os.Args[0 : len(os.Args)-1] {
if arg == "-port" {
port, _ = strconv.Atoi(os.Args[i+1])
}
if arg == "-repo" {
repoName = os.Args[i+1]
}
}
if repoName == "" {
repoName = "."
}
if port == 0 {
port = 8080
}
2022-08-03 14:55:48 +00:00
repo, err := owl.OpenRepository(repoName)
2022-07-23 15:19:47 +00:00
if err != nil {
println("Error opening repository: ", err.Error())
os.Exit(1)
}
2022-08-03 17:23:37 +00:00
router := Router(repo)
2022-07-23 15:19:47 +00:00
println("Listening on port", port)
2022-08-01 19:06:48 +00:00
http.ListenAndServe(":"+strconv.Itoa(port), router)
2022-07-23 15:19:47 +00:00
}