Perfect solution to the beego root directory cannot access static files

  • 2020-06-03 06:55:29
  • OfStack

beego is the most documented in the Go framework. It's easy to learn. But the limitations of the framework itself pose a big problem.

I recently came across a living example of this in dealing with the flash cross-domain problem:

as3 in flash has no problem accessing the external network. However, in the case of web page retrieval, flash visits the external network and the site is not a domain. Cross-domain problems arise. When you open your browser to f12, what you see is that flash is not visiting your url, but the configuration file crossdomain.xml in the root directory of the domain name where url is located. This configuration file sets the permissions for cross-domain access.

You need to place an ES19en.xml file in your domain root. I happen to be using beego, which does not support this feature! ~

Solutions:

Write beego as 1 file server since it is not supported. Put my ES27en.xml under static as well. When you receive this request, just write the file back to response. The code:

package main


import (
"fmt"
http "net/http"
"path/filepath"
"strings"


"github.com/astaxie/beego"
)


type MainController struct {
beego.Controller
}


func (this *MainController) Get() {
//this.Ctx.Request.URL= http://127.0.0.1/crossdomain.xml
orpath := this.Ctx.Request.URL.Path



if strings.Index(orpath, "crossdomain.xml") >= 0 {
fmt.Println(orpath) // /crossdomain.xml

path := filepath.Join(`static`, "crossdomain.xml")

http.ServeFile(this.Ctx.ResponseWriter, this.Ctx.Request, path)
}

this.Ctx.WriteString("hello world" + orpath)

}


func (this *MainController) Post() {
name := this.GetString("name")
sex := this.GetString("sex")
this.Ctx.WriteString(name + sex+" successful ")
fmt.Println(name + sex)
}



func main() {

beego.Router("/*", &MainController{})
beego.Run()
}

Of course, that's not a good idea. It's just throwing a brick at the door. All requests need to be judged 1, and if you want to perfect 1, it would be nice to be able to accurately match the controller processed by url in routing configuration, but not affect your own controller


Related articles: