openapi

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 13, 2021 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DICE_METADATA = apis.ApiSpec{
	Path:   "/metadata.json",
	Scheme: "http",
	Method: "GET",
	Custom: func(rw http.ResponseWriter, req *http.Request) {
		meta := make(map[string]interface{})
		meta["version"] = map[string]interface{}{
			"dice_version": version.Version,
			"git_commit":   version.CommitID,
			"go_version":   version.GoVersion,
			"built":        version.BuildTime,
		}
		meta["openapi_public_url"] = conf.SelfPublicURL()

		metaBytes, _ := json.MarshalIndent(meta, "", "  ")
		rw.Write(metaBytes)
	},
	Doc: "Dice 平台对外的元信息",
}
View Source
var DOC_JSON = apis.ApiSpec{
	Path:   "/api/openapi/swagger.json",
	Method: "GET",
	Scheme: "http",
	Custom: getDocJSON,
	Doc:    `summary: 返回 swagger.json`,
}
View Source
var OPENAPI_DOC = apis.ApiSpec{
	Path:      "/api/openapi-doc",
	Method:    "GET",
	Scheme:    "http",
	Custom:    getOpenAPIDoc,
	Doc:       "返回 openapi 文档",
	IsOpenAPI: true,
}
View Source
var OPENAPI_EVENT_DOC = apis.ApiSpec{
	Path:      "/api/openapi-event-doc",
	Method:    "GET",
	Scheme:    "http",
	Custom:    getEventDoc,
	Doc:       "获取 openevent 文档",
	IsOpenAPI: true,
}
View Source
var OPENAPI_GEN_CLIENT_TOKEN = apis.ApiSpec{
	Path:       "/api/openapi/client-token",
	Scheme:     "http",
	Method:     "POST",
	CheckLogin: false,
	Custom: func(rw http.ResponseWriter, req *http.Request) {
		basic := req.Header.Get("Authorization")
		if basic == "" {
			http.Error(rw, "not provide Basic Authorization header", http.StatusBadRequest)
			return
		}

		oauthToken, err := ucauth.GenClientToken(discover.UC(), basic)
		if err != nil {
			http.Error(rw, err.Error(), http.StatusInternalServerError)
			return
		}
		res, err := json.Marshal(oauthToken)
		if err != nil {
			http.Error(rw, err.Error(), http.StatusForbidden)
			return
		}
		rw.Header().Set("Content-Type", "application/json")
		rw.Write(res)
	},
	Doc: `
summary: client token 发放接口
description: 通过 header Basic 认证. Basic Header:"Basic " + base64(<clientid>+":"+<clientsecret>)
produces:
  - application/json
responses:
  '200':
    description: OK
    schema:
      type: object
      properties:
        access_token:
          type: string
        token_type:
          type: string
        refresh_token:
          type: string
        expires_in:
          type: int64
        scope:
          type: string
        jti:
          type: string

  '400':
    description: 没有提供 Authorization header`,
}
View Source
var OPENAPI_LIST_CLIENT = apis.ApiSpec{
	Path:   "/api/openapi/manager/clients",
	Scheme: "http",
	Method: "GET",
	Custom: func(rw http.ResponseWriter, req *http.Request) {
		token, err := auth.GetDiceClientToken()
		if err != nil {
			errStr := fmt.Sprintf("get token fail: %v", err)
			logrus.Error(errStr)
			http.Error(rw, errStr, http.StatusForbidden)
			return
		}
		logrus.Infof("diceclienttoken: %+v", token)
		var body bytes.Buffer
		r, err := httpclient.New(httpclient.WithCompleteRedirect()).Get(discover.UC()).Path("/api/open-client/manager/clients").
			Header("Authorization", "Bearer "+token.AccessToken).Do().Body(&body)
		if err != nil {
			errStr := fmt.Sprintf("list client fail: %v", err)
			logrus.Error(errStr)
			http.Error(rw, errStr, http.StatusForbidden)
			return
		}
		if !r.IsOK() {
			errStr := fmt.Sprintf("list client fail, statuscode: %d, body: %v", r.StatusCode(), body.String())
			logrus.Error(errStr)
			http.Error(rw, errStr, http.StatusForbidden)
			return
		}
		rw.Write([]byte(body.String()))
	},
	CheckLogin: false,
	Doc: `
summary: 获取client列表
description: 认证: 通过 Authorization 头信息进行认证。 格式为“Bearer <token>”, 注意空格
produces:
  - application/json
`,
}
View Source
var OPENAPI_METRICS = apis.ApiSpec{
	Path: "/api/openapi/metrics",
	Custom: func(rw http.ResponseWriter, req *http.Request) {
		monitor.Metrics(rw, req)
	},
	Scheme: "http",
	Method: "GET",
	Doc:    "openapi metrics",
}
View Source
var OPENAPI_NEW_CLIENT = apis.ApiSpec{
	Path:   "/api/openapi/manager/clients",
	Scheme: "http",
	Method: "POST",
	Custom: func(rw http.ResponseWriter, req *http.Request) {
		var newClientReq ucauth.NewClientRequest
		d := json.NewDecoder(req.Body)
		if err := d.Decode(&newClientReq); err != nil {
			errStr := fmt.Sprintf("new client fail: %v, buffered: %v", err, d.Buffered())
			logrus.Error(errStr)
			http.Error(rw, errStr, http.StatusBadRequest)
			return
		}
		res, err := auth.NewUCTokenClient(&newClientReq)
		if err != nil {
			errStr := fmt.Sprintf("new client fail: %v", err)
			logrus.Error(errStr)
			http.Error(rw, errStr, http.StatusBadGateway)
			return
		}
		resBody, err := json.Marshal(res)
		if err != nil {
			errStr := fmt.Sprintf("new client marshal fail: %v", err)
			logrus.Error(errStr)
			http.Error(rw, errStr, http.StatusBadGateway)
			return
		}
		rw.Write(resBody)
	},
	CheckLogin: false,
	Doc: `
summary: 创建新client
description: 认证: 通过 Authorization 头信息进行认证。 格式为“Bearer <token>”, 注意空格
parameters:
  - in: body
    name: request-json
    description: request json body
    schema:
      type: object
      properties:
        accessTokenValiditySeconds:
          type: integer
        autoApprove:
          type: boolean
        clientId:
          type: string
        clientLogoUrl:
          type: string
        clientName:
          type: string
        clientSecret:
          type: string
        refreshTokenValiditySeconds:
          type: integer
        userId:
          type: int

produces:
  - application/json

responses:
  '200':
    description: OK
`,
}
View Source
var OPENAPI_STAT = apis.ApiSpec{
	Path: "/api/openapi/stat",
	Custom: func(rw http.ResponseWriter, req *http.Request) {
		monitor.Stat(rw, req)
	},
	Scheme: "http",
	Method: "GET",
	Doc:    "summary: openapi 统计",
}
View Source
var OPENAPI_VERSION = apis.ApiSpec{
	Path:   "/api/openapi/version",
	Scheme: "http",
	Method: "GET",
	Custom: func(rw http.ResponseWriter, req *http.Request) {
		if req.URL.Query().Get("short") != "" {
			rw.Write([]byte(version.Version))
			return
		}
		rw.Write([]byte(version.String()))
	},
	Doc: `
summary: 返回 openapi 版本信息
`,
}

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL