Skip to content

Commit

Permalink
update https
Browse files Browse the repository at this point in the history
  • Loading branch information
andeya committed Nov 29, 2016
1 parent 41de04b commit 245a485
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions swagger/swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,10 @@ func Reg() {
}

var (
apidoc *Swagger
virtRouter *lessgo.VirtRouter
rwlock sync.RWMutex
jsonUrl = "/swagger.json"
scheme = func() string {
if lessgo.Config.Listen.EnableTLS {
return "https"
} else {
return "http"
}
}()
apidoc *Swagger
virtRouter *lessgo.VirtRouter
rwlock sync.RWMutex
jsonUrl = "/swagger.json"
swaggerHandle = &lessgo.ApiHandler{
Desc: "swagger",
Method: "GET",
Expand All @@ -67,6 +60,7 @@ var (
} else {
apidoc.Host = c.Request().Host // 根据请求动态设置host,修复因首次访问为localhost时,其他ip无法使用的bug
}
apidoc.Schemes = []string{getScheme(c)}
return c.JSON(200, apidoc)
},
}
Expand Down Expand Up @@ -189,7 +183,7 @@ func resetApidoc(host string) {
Host: host,
BasePath: "/",
Tags: []*Tag{rootTag},
Schemes: []string{scheme},
Schemes: []string{"http", "https"},
Paths: map[string]map[string]*Opera{},
// SecurityDefinitions: map[string]map[string]interface{}{},
// Definitions: map[string]Definition{},
Expand Down Expand Up @@ -576,3 +570,16 @@ var mapping2 = map[string]string{
"float64": "number",
"string": "string",
}

func getScheme(c *lessgo.Context) string {
if scheme := c.HeaderParam(lessgo.HeaderXForwardedProto); scheme != "" {
return scheme
}
if c.Request().URL.Scheme != "" {
return c.Request().URL.Scheme
}
if c.Request().TLS == nil {
return "http"
}
return "https"
}

0 comments on commit 245a485

Please sign in to comment.