hao1118 / fasthttp-rendering-jet-template

2 stars 0 forks source link

Static files #1

Open nkev opened 7 years ago

nkev commented 7 years ago

Thanks for this. How do you serve your static files? I'm trying github.com/buaazp/fasthttprouter without success:

router.GET("/js/*filepath", fasthttp.FSHandler("../static/js", 0)) 
hao1118 commented 7 years ago

Hi nkev,

According to fasthttprouter readme:

https://github.com/buaazp/fasthttprouter#static-files

// Serve static files from the ./public directory router.NotFound = fasthttp.FSHandler("./public", 0)

You can just create a js folder(or other folders like css,img,fonts,upload...) in static folder, then: router.NotFound = fasthttp.FSHandler("./static", 0)

I have my own router:

func main() {
    views.SetDevelopmentMode(myConfig.Debug)
    log.Println("Listening on", myConfig.Port)
    if err := fasthttp.ListenAndServe(myConfig.Port, requestHandler); err != nil {
        log.Fatalf("Error in ListenAndServe: %s", err)
    }
}
func requestHandler(ctx *fasthttp.RequestCtx) {
    defer func() {
        if err := recover(); err != nil {
            log.Fatalf("Recovery error: %s[%s]", err, debug.Stack())
        }
    }()
    path := strings.ToLower(string(ctx.Path()))
    q := strings.Index(path, "?")
    if q > 0 {
        path = path[:q]
    }
    routers := strings.Split(path, "/")[1:]
    switch routers[0] {
    case "", "home", "index.html":
        home(ctx)
    case "info":
        if len(routers) > 1 {
            content(ctx, 0, routers[1])
        }
    case "category":
        if len(routers) > 1 {
            category(ctx, routers[1])
        }
    case "product":
        if len(routers) > 1 {
            product(ctx, routers[1])
        }
    case "search":
        search(ctx)
    case "news":
                news(ctx)
    default:   //for static files
        filesHandler := fasthttp.FSHandler("./static", 0)
        filesHandler(ctx)
    }
}