- Integrated Captcha functionality by adding a new route for captcha handlers within the OpenAPI structure. - Updated the Attach method to include the Captcha endpoint, ensuring OAuth protection for the new route. - Enhanced overall routing organization by grouping captcha-related handlers under a dedicated path.
108 lines
2.5 KiB
Go
108 lines
2.5 KiB
Go
package openapi
|
|
|
|
import (
|
|
"path/filepath"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/yaoapp/gou/application"
|
|
"github.com/yaoapp/yao/config"
|
|
"github.com/yaoapp/yao/openapi/captcha"
|
|
"github.com/yaoapp/yao/openapi/chat"
|
|
"github.com/yaoapp/yao/openapi/dsl"
|
|
"github.com/yaoapp/yao/openapi/file"
|
|
"github.com/yaoapp/yao/openapi/hello"
|
|
"github.com/yaoapp/yao/openapi/kb"
|
|
"github.com/yaoapp/yao/openapi/oauth"
|
|
"github.com/yaoapp/yao/openapi/oauth/types"
|
|
"github.com/yaoapp/yao/openapi/signin"
|
|
)
|
|
|
|
// Server is the OpenAPI server
|
|
var Server *OpenAPI = nil
|
|
|
|
// OpenAPI is the OpenAPI server
|
|
type OpenAPI struct {
|
|
Config *Config // OpenAPI configuration
|
|
OAuth types.OAuth // OAuth service interface
|
|
}
|
|
|
|
// Load loads the OpenAPI server from the configuration
|
|
func Load(appConfig config.Config) (*OpenAPI, error) {
|
|
|
|
var configPath string = filepath.Join("openapi", "openapi.yao")
|
|
var configRaw, err = application.App.Read(configPath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Parse the configuration
|
|
var config Config
|
|
err = application.Parse(configPath, configRaw, &config)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Convert the configuration to an OAuth configuration
|
|
oauthConfig, err := config.OAuthConfig(appConfig)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Create the OAuth service
|
|
oauthService, err := oauth.NewService(oauthConfig)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Load signin configurations
|
|
err = signin.Load(appConfig)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Create the OpenAPI server
|
|
Server = &OpenAPI{Config: &config, OAuth: oauthService}
|
|
return Server, nil
|
|
}
|
|
|
|
// Attach attaches the OpenAPI server to the router
|
|
func (openapi *OpenAPI) Attach(router *gin.Engine) {
|
|
|
|
// Ignore if the OpenAPI server is not configured
|
|
if openapi.Config == nil {
|
|
return
|
|
}
|
|
|
|
// Basic Groups
|
|
baseURL := openapi.Config.BaseURL
|
|
group := router.Group(baseURL)
|
|
|
|
// Well-known handlers
|
|
openapi.attachWellKnown(router)
|
|
|
|
// OAuth handlers
|
|
openapi.attachOAuth(group)
|
|
|
|
// Hello World handlers
|
|
hello.Attach(group.Group("/helloworld"), openapi.OAuth)
|
|
|
|
// DSL handlers
|
|
dsl.Attach(group.Group("/dsl"), openapi.OAuth)
|
|
|
|
// File handlers
|
|
file.Attach(group.Group("/file"), openapi.OAuth)
|
|
|
|
// Knowledge Base handlers
|
|
kb.Attach(group.Group("/kb"), openapi.OAuth)
|
|
|
|
// Chat handlers
|
|
chat.Attach(group.Group("/chat"), openapi.OAuth)
|
|
|
|
// Signin handlers
|
|
signin.Attach(group, openapi.OAuth)
|
|
|
|
// Captcha handlers
|
|
captcha.Attach(group.Group("/captcha"), openapi.OAuth)
|
|
|
|
// Custom handlers (Defined by developer)
|
|
}
|