yao/openapi/openapi.go
Max 0fff602c93 Implement JSON marshaling and unmarshaling for configuration with duration parsing
- Enhanced the Config struct to support JSON marshaling and unmarshaling with human-readable duration strings for various OAuth settings.
- Introduced temporary structures to facilitate the conversion of string duration fields to time.Duration types during JSON operations.
- Added utility functions for parsing and formatting duration strings, ensuring accurate handling of time-related configurations.
- Updated tests to validate the correct parsing and formatting of duration fields in the configuration.
2025-07-20 17:49:01 +08:00

52 lines
1.2 KiB
Go

package openapi
import (
"path/filepath"
"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
}