- Integrated OpenAPI loading functionality into the engine's Load and Reload processes, allowing for better API management. - Updated the OpenAPI configuration to set a default BaseURL and ensure it does not have a trailing slash. - Implemented the Attach method to connect the OpenAPI server to the Gin router, facilitating API endpoint management. - Removed the obsolete hello package to streamline the OpenAPI module.
74 lines
1.6 KiB
Go
74 lines
1.6 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/oauth"
|
|
"github.com/yaoapp/yao/openapi/oauth/types"
|
|
)
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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)
|
|
|
|
// OAuth handlers
|
|
openapi.attachOAuth(group)
|
|
|
|
// Hello World handlers
|
|
openapi.attachHelloWorld(group)
|
|
|
|
// Custom handlers (Defined by developer)
|
|
}
|