mirror of
https://codeberg.org/video-prize-ranch/rimgo.git
synced 2026-01-28 01:21:16 +00:00
port most routes
This commit is contained in:
64
main.go
64
main.go
@@ -3,7 +3,9 @@ package main
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"codeberg.org/rimgo/rimgo/pages"
|
||||
"codeberg.org/rimgo/rimgo/render"
|
||||
@@ -20,7 +22,8 @@ func wrapHandler(h handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
err := h(w, r)
|
||||
if err != nil {
|
||||
http.Error(w, "oop", 500)
|
||||
fmt.Println(err)
|
||||
http.Error(w, http.StatusText(500), 500)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -37,10 +40,63 @@ func main() {
|
||||
render.Initialize(views)
|
||||
|
||||
app := http.NewServeMux()
|
||||
app.Handle("/static/", http.StripPrefix("/static/", http.FileServerFS(static)))
|
||||
app.Handle("GET /test-render", wrapHandler(func(w http.ResponseWriter, r *http.Request) error {
|
||||
|
||||
app.Handle("GET /static/", http.StripPrefix("/static/", http.FileServerFS(static)))
|
||||
app.Handle("GET /robots.txt", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
file, _ := static.Open("robots.txt")
|
||||
defer file.Close()
|
||||
io.Copy(w, file)
|
||||
}))
|
||||
app.Handle("GET /favicon.ico", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
file, _ := static.Open("favicon/favicon.ico")
|
||||
defer file.Close()
|
||||
io.Copy(w, file)
|
||||
}))
|
||||
|
||||
app.Handle("GET /{$}", wrapHandler(pages.HandleFrontpage))
|
||||
// app.Handle("GET /{postID}/embed", wrapHandler(pages.HandleEmbed)) // fix this conflict
|
||||
app.Handle("GET /a/{postID}", wrapHandler(pages.HandlePost))
|
||||
app.Handle("GET /a/{postID}/embed", wrapHandler(pages.HandleEmbed))
|
||||
// app.Handle("GET /t/:tag.:type", pages.HandleTagRSS)
|
||||
app.Handle("GET /t/{tag}", wrapHandler(pages.HandleTag))
|
||||
app.Handle("GET /t/{tag}/{postID}", wrapHandler(pages.HandlePost))
|
||||
app.Handle("GET /r/{sub}/{postID}", wrapHandler(pages.HandlePost))
|
||||
// app.Handle("GET /user/:userID.:type", pages.HandleUserRSS)
|
||||
app.Handle("GET /user/{userID}", wrapHandler(pages.HandleUser))
|
||||
app.Handle("GET /user/{userID}/favorites", wrapHandler(pages.HandleUserFavorites))
|
||||
app.Handle("GET /user/{userID}/comments", wrapHandler(pages.HandleUserComments))
|
||||
app.Handle("GET /user/{userID}/cover", wrapHandler(pages.HandleUserCover))
|
||||
app.Handle("GET /user/{userID}/avatar", wrapHandler(pages.HandleUserAvatar))
|
||||
app.Handle("GET /gallery/{postID}", wrapHandler(pages.HandlePost))
|
||||
app.Handle("GET /gallery/{postID}/embed", wrapHandler(pages.HandleEmbed))
|
||||
app.Handle("GET /{component}", wrapHandler(func(w http.ResponseWriter, r *http.Request) error {
|
||||
component := r.PathValue("component")
|
||||
switch {
|
||||
case component == "about":
|
||||
return pages.HandleAbout(w, r)
|
||||
case component == "privacy":
|
||||
return pages.HandlePrivacy(w, r)
|
||||
case component == "search":
|
||||
return pages.HandleSearch(w, r)
|
||||
case component == "trending":
|
||||
return pages.HandleTrending(w, r)
|
||||
case strings.HasPrefix(component, "trending."):
|
||||
// return pages.HandleTrendingRSS(w, r)
|
||||
case strings.HasSuffix(component, ".gifv"):
|
||||
r.SetPathValue("postID", component)
|
||||
return pages.HandleGifv(w, r)
|
||||
case strings.Contains(component, "."):
|
||||
return pages.HandleMedia(w, r)
|
||||
default:
|
||||
r.SetPathValue("postID", component)
|
||||
return pages.HandlePost(w, r)
|
||||
}
|
||||
return nil
|
||||
}))
|
||||
app.Handle("GET /stack/:baseName.:extension", wrapHandler(pages.HandleMedia))
|
||||
// matches anything with no more specific route
|
||||
app.Handle("GET /", wrapHandler(func(w http.ResponseWriter, r *http.Request) error {
|
||||
err := render.Render(w, "errors/404", nil)
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user