remove last panic in http handler

This commit is contained in:
iacore
2024-08-27 12:49:08 +00:00
parent 71d29e279b
commit bb2920d2e8
3 changed files with 16 additions and 23 deletions
+5
View File
@@ -11,7 +11,12 @@
- Rate limit (optional, could be loosely-coupled)
- Caching (optional, could be loosely-coupled)
## Problem
net/http handlers don't return errors. We have to make our own ServeMux that allows functions to return `error`, possibly.
## Tips
To access `/:abc`, use `r.PathValue("abc")`.
Idea: we create a compat layer of {w, r} that has the same API as *fiber.Ctx.
+10 -22
View File
@@ -185,22 +185,8 @@ func main() {
method := r.Method
path := r.URL.Path
status := r.Response.Status
// err := ???
// todo: logger
// server.Use(logger.New(
// logger.Config{
// Format: "${time} +${latency} ${ip} ${method} ${path} ${status} ${error} \n",
// Next: CanRequestSkipLogger,
// CustomTags: map[string]logger.LogFunc{
// // make latency always print in seconds
// logger.TagLatency: func(output logger.Buffer, c *http.Request, data *logger.Data, extraParam string) (int, error) {
// latency := data.Stop.Sub(data.Start).Seconds()
// return output.WriteString(fmt.Sprintf("%.6f", latency))
// },
// },
// },
// ))
log.Printf("%v +%v %v %v %v %v {todo: print error (where is error?)}", time, latency, ip, method, path, status)
}
}
@@ -261,15 +247,17 @@ func setGlobalHeaders(r *http.Request) {
header.Add("Permissions-Policy", "accelerometer=(), ambient-light-sensor=(), battery=(), camera=(), display-capture=(), document-domain=(), encrypted-media=(), execution-while-not-rendered=(), execution-while-out-of-viewport=(), geolocation=(), gyroscope=(), magnetometer=(), microphone=(), midi=(), navigation-override=(), payment=(), publickey-credentials-get=(), screen-wake-lock=(), sync-xhr=(), usb=(), web-share=(), xr-spatial-tracking=()")
}
func serveFile(filename string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, filename) }
}
func defineRoutes() http.ServeMux {
router := http.ServeMux{}
// todo
// server.Static("/favicon.ico", "./assets/img/favicon.ico")
// server.Static("/robots.txt", "./assets/robots.txt")
// server.Static("/img/", "./assets/img")
// server.Static("/css/", "./assets/css")
// server.Static("/js/", "./assets/js")
router.HandleFunc("/favicon.ico", serveFile("./assets/img/favicon.ico"))
router.HandleFunc("/robots.txt", serveFile("./assets/robots.txt"))
router.Handle("/img/", http.FileServer(http.Dir("./assets/img")))
router.Handle("/css/", http.FileServer(http.Dir("./assets/css")))
router.Handle("/js/", http.FileServer(http.Dir("./assets/js")))
// server.Use(recover.New(recover.Config{EnableStackTrace: config.GlobalServerConfig.InDevelopment}))
+1 -1
View File
@@ -15,7 +15,7 @@ func RankingPage(c *http.Request) error {
page := c.Query("page", "1")
pageInt, err := strconv.Atoi(page)
if err != nil {
panic(err)
return err
}
works, err := core.GetRanking(c, mode, content, date, page)