Beego: after set CORS InsertFilter, swagger stop working

Created on 19 Apr 2016  ·  13Comments  ·  Source: astaxie/beego

Please answer these questions before submitting your issue. Thanks!

  1. What version of Go and beego are you using (bee version)?
    bee :1.4.1
    beego :1.6.0
    Go :go version go1.5.1 darwin/amd64
  2. What operating system and processor architecture are you using (go env)?
    GOARCH="amd64"
    GOBIN=""
    GOEXE=""
    GOHOSTARCH="amd64"
    GOHOSTOS="darwin"
    GOOS="darwin"
    GOPATH="/Users/Xargin/go"
    GORACE=""
    GOROOT="/usr/local/go"
    GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
    GO15VENDOREXPERIMENT=""
    CC="clang"
    GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"
    CXX="clang++"
    CGO_ENABLED="1"
  3. What did you do?
    If possible, provide a recipe for reproducing the error.
    A complete runnable program is good.
    i have insert cors settings to support cors requests,
    in main.go
 beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{

          AllowOrigins:     []string{"*"},
          AllowCredentials: true,
      }))

     if beego.BConfig.RunMode == "dev" {
          beego.BConfig.WebConfig.DirectoryIndex = true
          beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
      }

when open 127.0.0.1:8080/swagger/swagger-1
I get the following message:
Can't read swagger JSON from http://127.0.0.1:8080/docs

If I delete the cors settings, everything is ok

Most helpful comment

ok, I checked the InsertFilter function
and solve this by insert the cors filter by:

 beego.InsertFilter("*", beego.FinishRouter, cors.Allow(&cors.Options{
         AllowOrigins: []string{"*"},
 }))

change beego.BeforeRouter -> beego.FinishRouter

Maybe the former filter execution point is not correct.

But if I deploy the swagger away from the beego project
this kind of setting is not correct again, I must change the FinishRouter to BeforeExec or sth...
Hope there is a better way to solve this...

All 13 comments

@cch123 Maybe you can refer to this issue https://github.com/astaxie/beego/issues/1857

@youngsterxyf
after I change beego.BeforeRouter to beego.BeforeExec
swagger still cannot work.
I think the main reason of this is that after I add filter, http://127.0.0.1:8080/docs responds with a 404

swagger request path, log shows match

2016/04/19 15:22:36 [router.go:828][D] | GET        | /docs  | 54.31µs          | match      | /docs   |

but response shows:
http get http://127.0.0.1:8080/docs

HTTP/1.1 404 Not Found
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Origin,Accept,Content-Type,Authorization
Access-Control-Allow-Origin:
Content-Length: 0
Content-Type: text/plain; charset=utf-8
Date: Tue, 19 Apr 2016 07:22:36 GMT
Server: beegoServer:1.6.0

@cch123 the relative url is "/docs", not "/swagger"?

@youngsterxyf
the swagger page is

http://127.0.0.1:8080/swagger/swagger-1

it requests the url

http://127.0.0.1:8080/docs

the swagger page can be correctly loaded,

but the /docs url returns 404
though the log shows match。

@cch123 Do you run app with bee run -gendoc=true -downdoc=true ?

ok, I checked the InsertFilter function
and solve this by insert the cors filter by:

 beego.InsertFilter("*", beego.FinishRouter, cors.Allow(&cors.Options{
         AllowOrigins: []string{"*"},
 }))

change beego.BeforeRouter -> beego.FinishRouter

Maybe the former filter execution point is not correct.

But if I deploy the swagger away from the beego project
this kind of setting is not correct again, I must change the FinishRouter to BeforeExec or sth...
Hope there is a better way to solve this...

+1

I also got this

the new version swagger2.0 totally don't need any server now. just generate the swagger.json

Today I downloaded swagger2.0 (https://github.com/beego/swagger/releases), but all the same, instead of PUT / POST request, it sends OPTIONS.
I need to update the framework?

@ihippik yea, you need to upgrade to bee and beego

go get -u github.com/beego/bee

updatet to bee v.1.5.0
run
go get -u github.com/astaxie/beego/swagger
remove swagger folder, runbee run -downdoc=true -gendoc=true
download new v2 swagger version :+1: , but i got
docs/docs.go:17: undefined: swagger.ResourceListing

docs/docs.go:18: undefined: swagger.APIDeclaration

docs/docs.go:30: undefined: beego.GlobalDocAPI

docs/docs.go:37: undefined: beego.GlobalDocAPI

Got it - changed the structure.
Now the files are not needed. JSON generated on a different principle
Many thanks!

Was this page helpful?
0 / 5 - 0 ratings