mirror of
https://codeberg.org/VnPower/PixivFE
synced 2024-12-06 19:16:23 +01:00
remove last panic in http handler
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user