add swagger

This commit is contained in:
yanghao05
2023-06-09 14:32:21 +08:00
parent 37bf456102
commit 3f27dee0b2
23 changed files with 245 additions and 89 deletions

View File

@@ -26,7 +26,7 @@ func Provide(opts ...opt.Option) error {
o := opt.New(opts...)
var conf Config
if err := o.UnmarshalConfig(&conf); err != nil {
log.Fatal(err)
return err
}
return container.Container.Provide(func() (*Captcha, error) {

View File

@@ -27,7 +27,6 @@ type Config struct {
OpenTimeOut string // 防爆破验证码超时时间单位s(秒)
MaxScrew float64 // MaxSkew max absolute skew factor of a single digit.
DotCount int // Number of background circles.
}
func (c *Config) OpenCaptchaTimeOutDuration() time.Duration {

View File

@@ -1,22 +1,19 @@
package casdoor
import (
"log"
"github.com/casdoor/casdoor-go-sdk/casdoorsdk"
"github.com/rogeecn/atom/container"
"github.com/rogeecn/atom/providers/cert"
"github.com/rogeecn/atom/utils/opt"
)
type Casdoor struct {
}
type Casdoor struct{}
func Provide(opts ...opt.Option) error {
o := opt.New(opts...)
var config Config
if err := o.UnmarshalConfig(&config); err != nil {
log.Fatal(err)
return err
}
return container.Container.Provide(func(cert *cert.Cert) *Casdoor {
certificate := config.Certificate

View File

@@ -1,8 +1,6 @@
package cert
import (
"log"
"github.com/rogeecn/atom/container"
"github.com/rogeecn/atom/utils/opt"
)
@@ -11,7 +9,7 @@ func Provide(opts ...opt.Option) error {
o := opt.New(opts...)
var config Cert
if err := o.UnmarshalConfig(&config); err != nil {
log.Fatal(err)
return err
}
return container.Container.Provide(func() (*Cert, error) {

View File

@@ -1,9 +1,11 @@
package config
import (
"log"
"os"
"path/filepath"
"github.com/pkg/errors"
"github.com/rogeecn/atom/container"
"github.com/spf13/viper"
)
@@ -14,15 +16,15 @@ func Load(file, app string) (*viper.Viper, error) {
if file == "" {
v.SetConfigType("toml")
v.SetConfigName(app)
v.SetConfigName(app + ".toml")
//execute path
// execute path
execPath, err := os.Executable()
if err == nil {
v.AddConfigPath(filepath.Dir(execPath))
}
//home path
// home path
homePath, err := os.UserHomeDir()
if err == nil {
v.AddConfigPath(homePath)
@@ -39,10 +41,13 @@ func Load(file, app string) (*viper.Viper, error) {
v.SetConfigFile(file)
}
if err := v.ReadInConfig(); err != nil {
return nil, err
err := v.ReadInConfig()
log.Println("config file:", v.ConfigFileUsed())
if err != nil {
return nil, errors.Wrap(err, "config file read error")
}
err := container.Container.Provide(func() (*viper.Viper, error) {
err = container.Container.Provide(func() (*viper.Viper, error) {
return v, nil
})
if err != nil {

View File

@@ -36,7 +36,7 @@ func Provide(opts ...opt.Option) error {
o := opt.New(opts...)
var config Config
if err := o.UnmarshalConfig(&config); err != nil {
log.Fatal(err)
return err
}
return container.Container.Provide(func() (*Grpc, error) {

View File

@@ -2,7 +2,6 @@ package hashids
import (
"github.com/rogeecn/atom/container"
"github.com/rogeecn/atom/providers/log"
"github.com/rogeecn/atom/utils/opt"
"github.com/speps/go-hashids/v2"
@@ -12,7 +11,7 @@ func Provide(opts ...opt.Option) error {
o := opt.New(opts...)
var config Config
if err := o.UnmarshalConfig(&config); err != nil {
log.Fatal(err)
return err
}
return container.Container.Provide(func() (*hashids.HashID, error) {
data := hashids.NewData()

View File

@@ -45,7 +45,7 @@ func Provide(opts ...opt.Option) error {
o := opt.New(opts...)
var config http.Config
if err := o.UnmarshalConfig(&config); err != nil {
log.Fatal(err)
return err
}
return container.Container.Provide(func() (http.Service, error) {

View File

@@ -12,7 +12,6 @@ import (
"github.com/imroc/req/v3"
"github.com/rogeecn/atom/container"
"github.com/rogeecn/atom/providers/httpclient/cookiejar"
"github.com/rogeecn/atom/providers/log"
"github.com/rogeecn/atom/utils/opt"
)
@@ -25,7 +24,7 @@ func Provide(opts ...opt.Option) error {
o := opt.New(opts...)
var config Config
if err := o.UnmarshalConfig(&config); err != nil {
log.Fatal(err)
return err
}
return container.Container.Provide(func() (*Client, error) {
c := &Client{}
@@ -38,7 +37,7 @@ func Provide(opts ...opt.Option) error {
if config.CookieJarFile != "" {
dir := filepath.Dir(config.CookieJarFile)
if _, err := os.Stat(dir); os.IsNotExist(err) {
err = os.MkdirAll(dir, 0755)
err = os.MkdirAll(dir, 0o755)
if err != nil {
return nil, err
}
@@ -146,6 +145,7 @@ func (c *Client) AllCookies() []*http.Cookie {
func (c *Client) AllCookiesKV() map[string]string {
return c.jar.KVData()
}
func (c *Client) SetCookie(u *url.URL, cookies []*http.Cookie) {
c.jar.SetCookies(u, cookies)
}

View File

@@ -47,7 +47,7 @@ func (j *Jar) save(now time.Time) error {
return err
}
defer locked.Close()
f, err := os.OpenFile(j.filename, os.O_RDWR|os.O_CREATE, 0600)
f, err := os.OpenFile(j.filename, os.O_RDWR|os.O_CREATE, 0o600)
if err != nil {
return err
}

View File

@@ -6,7 +6,6 @@ import (
"time"
"github.com/rogeecn/atom/container"
"github.com/rogeecn/atom/providers/log"
"github.com/rogeecn/atom/utils/opt"
jwt "github.com/golang-jwt/jwt/v4"
@@ -48,7 +47,7 @@ func Provide(opts ...opt.Option) error {
o := opt.New(opts...)
var config Config
if err := o.UnmarshalConfig(&config); err != nil {
log.Fatal(err)
return err
}
return container.Container.Provide(func() (*JWT, error) {
return &JWT{

View File

@@ -3,7 +3,6 @@ package gomicro
import (
"github.com/rogeecn/atom/container"
"github.com/rogeecn/atom/providers/http"
"github.com/rogeecn/atom/providers/log"
"github.com/rogeecn/atom/providers/micro_service"
"github.com/rogeecn/atom/utils/opt"
"go-micro.dev/v4"
@@ -23,7 +22,7 @@ func Provide(opts ...opt.Option) error {
o := opt.New(opts...)
var config micro_service.Config
if err := o.UnmarshalConfig(&config); err != nil {
log.Fatal(err)
return err
}
return container.Container.Provide(func() (micro_service.Service, error) {

View File

@@ -0,0 +1,28 @@
package swagger
import (
"github.com/rogeecn/atom/container"
"github.com/rogeecn/atom/utils/opt"
ginSwagger "github.com/swaggo/gin-swagger"
)
const DefaultPrefix = "Swagger"
type Config struct {
BaseRoute string
Version string
Host string
BasePath string
Title string
Description string
HandlerConfig *ginSwagger.Config
}
func DefaultProvider() container.ProviderContainer {
return container.ProviderContainer{
Provider: Provide,
Options: []opt.Option{
opt.Prefix(DefaultPrefix),
},
}
}

55
providers/swagger/docs.go Normal file
View File

@@ -0,0 +1,55 @@
package swagger
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/rogeecn/atom/container"
"github.com/rogeecn/atom/providers/http"
"github.com/rogeecn/atom/utils/opt"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"github.com/swaggo/swag"
)
type Swagger struct {
}
func Provide(opts ...opt.Option) error {
o := opt.New(opts...)
var config Config
if err := o.UnmarshalConfig(&config); err != nil {
return err
}
return container.Container.Provide(func(http http.Service) *Swagger {
if config.BaseRoute == "" {
config.BaseRoute = "swagger"
}
swaggerInfo := &swag.Spec{
Version: config.Version,
Host: config.Host,
BasePath: config.BasePath,
Schemes: []string{},
Title: config.Title,
Description: config.Description,
InfoInstanceName: "swagger",
SwaggerTemplate: docTemplate,
LeftDelim: "{{",
RightDelim: "}}",
}
swag.Register(swaggerInfo.InstanceName(), swaggerInfo)
engine := http.GetEngine().(*gin.Engine)
var handler gin.HandlerFunc
if config.HandlerConfig != nil {
handler = ginSwagger.CustomWrapHandler(config.HandlerConfig, swaggerFiles.Handler)
} else {
handler = ginSwagger.WrapHandler(swaggerFiles.Handler)
}
engine.GET(fmt.Sprintf("/%s/*any", config.BaseRoute), handler)
return &Swagger{}
}, o.DiOptions()...)
}

View File

@@ -0,0 +1,39 @@
package swagger
const docTemplate = `{
"schemes": {{ marshal .Schemes }},
"swagger": "2.0",
"info": {
"description": "{{escape .Description}}",
"title": "{{.Title}}",
"contact": {},
"version": "{{.Version}}"
},
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/example/helloworld": {
"get": {
"description": "do ping",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"example"
],
"summary": "ping example",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
}
}
}
}
}
}`