diff --git a/doc/dev/framework-migration.md b/doc/dev/framework-migration.md index 00cf65f..b5bc4a0 100644 --- a/doc/dev/framework-migration.md +++ b/doc/dev/framework-migration.md @@ -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. diff --git a/main.go b/main.go index f360573..50298e6 100644 --- a/main.go +++ b/main.go @@ -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})) diff --git a/routes/ranking.go b/routes/ranking.go index e3161c7..3962e13 100644 --- a/routes/ranking.go +++ b/routes/ranking.go @@ -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)