mirror of
https://github.com/APIParkLab/APIPark.git
synced 2026-06-04 10:13:53 +08:00
add openapi
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
_ "github.com/APIParkLab/APIPark/frontend"
|
||||
_ "github.com/APIParkLab/APIPark/gateway/apinto"
|
||||
_ "github.com/APIParkLab/APIPark/plugins/core"
|
||||
_ "github.com/APIParkLab/APIPark/plugins/openapi"
|
||||
_ "github.com/APIParkLab/APIPark/plugins/permit"
|
||||
_ "github.com/APIParkLab/APIPark/plugins/publish_flow"
|
||||
_ "github.com/APIParkLab/APIPark/resources/locale"
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package openapi
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/eolinker/go-common/pm3"
|
||||
)
|
||||
|
||||
func (p *plugin) appAuthorizationApis() []pm3.Api {
|
||||
return []pm3.Api{
|
||||
pm3.CreateApiWidthDoc(http.MethodPost, "/openapi/v1/app/authorization", []string{"context", "query:app", "body"}, []string{"authorization"}, p.authorizationController.AddAuthorization),
|
||||
pm3.CreateApiWidthDoc(http.MethodPut, "/openapi/v1/app/authorization", []string{"context", "query:app", "query:authorization", "body"}, []string{"authorization"}, p.authorizationController.EditAuthorization),
|
||||
pm3.CreateApiWidthDoc(http.MethodDelete, "/openapi/v1/app/authorization", []string{"context", "query:app", "query:authorization"}, nil, p.authorizationController.DeleteAuthorization),
|
||||
pm3.CreateApiWidthDoc(http.MethodGet, "/openapi/v1/app/authorization", []string{"context", "query:app", "query:authorization"}, []string{"authorization"}, p.authorizationController.Info),
|
||||
pm3.CreateApiWidthDoc(http.MethodGet, "/openapi/v1/app/authorizations", []string{"context", "query:app"}, []string{"authorizations"}, p.authorizationController.Authorizations),
|
||||
pm3.CreateApiWidthDoc(http.MethodGet, "/openapi/v1/app/authorization/details", []string{"context", "query:app", "query:authorization"}, []string{"details"}, p.authorizationController.Detail),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package openapi
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/eolinker/eosc/env"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var (
|
||||
defaultAPIKey = "37eb0ebf"
|
||||
openCheck = newOpenapiCheck()
|
||||
)
|
||||
|
||||
type openapiCheck struct {
|
||||
apikey string
|
||||
}
|
||||
|
||||
func newOpenapiCheck() *openapiCheck {
|
||||
apikey, has := env.GetEnv("API_KEY")
|
||||
if !has {
|
||||
apikey = defaultAPIKey
|
||||
}
|
||||
return &openapiCheck{apikey: apikey}
|
||||
}
|
||||
|
||||
func (o *openapiCheck) Check(method string, path string) (bool, []gin.HandlerFunc) {
|
||||
if strings.HasPrefix(path, "/openapi/") {
|
||||
return true, []gin.HandlerFunc{o.Handler}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (o *openapiCheck) Sort() int {
|
||||
return -1
|
||||
}
|
||||
|
||||
func (o *openapiCheck) Handler(ginCtx *gin.Context) {
|
||||
authorization := ginCtx.GetHeader("Authorization")
|
||||
if authorization == "" {
|
||||
ginCtx.AbortWithStatusJSON(403, gin.H{"code": -8, "msg": "invalid token", "success": "fail"})
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package openapi
|
||||
|
||||
import (
|
||||
"github.com/eolinker/go-common/autowire"
|
||||
"github.com/eolinker/go-common/pm3"
|
||||
)
|
||||
|
||||
func init() {
|
||||
pm3.Register("openapi", new(Driver))
|
||||
}
|
||||
|
||||
type Driver struct {
|
||||
}
|
||||
|
||||
func (d *Driver) Create() (pm3.IPlugin, error) {
|
||||
p := new(plugin)
|
||||
autowire.Autowired(p)
|
||||
return p, nil
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package openapi
|
||||
|
||||
import (
|
||||
application_authorization "github.com/APIParkLab/APIPark/controller/application-authorization"
|
||||
"github.com/eolinker/go-common/pm3"
|
||||
)
|
||||
|
||||
var (
|
||||
_ pm3.IPlugin = (*plugin)(nil)
|
||||
_ pm3.IPluginMiddleware = (*plugin)(nil)
|
||||
)
|
||||
|
||||
type plugin struct {
|
||||
apis []pm3.Api
|
||||
authorizationController application_authorization.IAuthorizationController `autowired:""`
|
||||
}
|
||||
|
||||
func (p *plugin) Middlewares() []pm3.IMiddleware {
|
||||
return []pm3.IMiddleware{
|
||||
openCheck,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *plugin) APis() []pm3.Api {
|
||||
return p.apis
|
||||
}
|
||||
|
||||
func (p *plugin) Name() string {
|
||||
return "openapi"
|
||||
}
|
||||
func (p *plugin) OnComplete() {
|
||||
p.apis = p.appAuthorizationApis()
|
||||
}
|
||||
Reference in New Issue
Block a user