The "Play Golang" Beego implements the transparent static file service (the static folder) effect in Martini.

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

For reasons of efficiency, recently the web framework was switched from Martini to Beego, other places are smooth, but the handler signature of two frames is inconsistent and needs to be modified, so it takes a long time, which is expected. But there is a place that is not expected, but also takes more time, that is, the service of static files.

Used Martini TX all know, in Mairtini if we set a directory as a static file directory, just add martini static plug-in, such as set the Web subdirectory for the application of the file path:

        M.use (Martini. Static ("web"))

At this point, if we access a URL, this URL is not registered in Martini, but if it is in the Web directory, it can be responded to, for example:

http:/ ///return index.html http://127.0.0.1:8088/js/ in web directory Jquery.js// return web/js/jquery.js

However, after switching to Beego, no such feature was found. I found that Beego's support for static files was a little less friendly, for example, I set the following

Beego. Setstaticpath ("/web""web")

The results of this visit are as follows

http:// // return 404 page http://127.0.0.1:8088/web//  Return 404 page http://127.0.0.1:8088/web/index.html// return 403 (Forbidden)http ://127.0.0.1:8088/web/chat.html// return to normal http://  127.0.0.1:8088/web/images/test.png// return to normal

As a result, two points were not satisfied:

    1. You must add the path access for the Staticdir map, set to "/" is not valid
    2. Can't return to the default page! Look at the document need to set " beego.DirectoryIndex=true ", not what I need!

So I started to do it myself. By learning the Beego document, you can set the filter. So, write the following code:

The filter is set as follows in main
Beego. Insertfilter ("/*", Beego. Beforerouter, transparentstatic).
.
. Func transparentstatic (CTX*context. Context) {defind:=0Maxind:= Len (defhomes)-1Orpath:=CTX. Request.URL.Path Beego. Debug ("In trasparentstatic filter Orpath", Orpath)ifStrings. Index (Orpath,"api/") >=0|| Strings. Index (Orpath,"web/") >=0 { return}defaultstartpage:p:=OrpathifStrings. Equalfold (P,"/") {p+=Defhomes[defind] Defind++} PS:= Strings. Split (P,"/") PS= Append ([]string{"Web"}, PS ...) RP:= Strings. Join (PS,"/") FP:=FW. MapPath (RP) Beego. Debug ("Test FP", FP)if!fileutils. Exists (FP) {ifDefind >0&& Defind <Maxind {GotoDefaultstartpage}return } Else{Beego. Debug ("found static", FP) http. Servefile (CTX. Responsewriter, CTX. Request, FP)//cannot use redirect! would lead loop//http. Redirect (CTX. Responsewriter, CTX. Request, RP, http. Statusfound) return } //}

After running, the Discovery Access service address, with no end of the "/", can not return to the default page, if explicitly access to/index.html to achieve access. After exploring the discovery, although the Beego Note that "/*" can be adapted to all URLs, but in fact can not be adapted to "/", so you need to register a filter to "/":

Beego. Insertfilter ("/"///must have this fordefault pageBeego. Insertfilter ("/*", Beego. Beforerouter, Transparentstatic)

At this point, everything is fine.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.