Skip to content

Commit

Permalink
1.config新增Set方法,2.config新增解析配置到目标结构体,3.补充代码接口注释,4.跨域中间件新增仅针对options请求…
Browse files Browse the repository at this point in the history
…的方法,5.更新README,6.更换彩色Logo
  • Loading branch information
keepchen committed Feb 21, 2024
1 parent 90fe803 commit e29c804
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 12 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div align="center">
<h1><img src="./sailboat-solid.svg" alt="sailboat-solid" title="sailboat-solid" width="300" /></h1>
<h1><img src="./sailboat-solid-colorful.svg" alt="sailboat-solid" title="sailboat-solid" width="300" /></h1>
</div>

[![Go](https://github.com/keepchen/go-sail/actions/workflows/go.yml/badge.svg)](https://github.com/keepchen/go-sail/actions/workflows/go.yml) [![CodeQL](https://github.com/keepchen/go-sail/actions/workflows/codeql.yml/badge.svg)](https://github.com/keepchen/go-sail/actions/workflows/codeql.yml) [![Go Report Card](https://goreportcard.com/badge/github.com/keepchen/go-sail/v3)](https://goreportcard.com/report/github.com/keepchen/go-sail/v3)
Expand All @@ -20,6 +20,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/keepchen/go-sail/v3/constants"
"github.com/keepchen/go-sail/v3/http/api"
"github.com/keepchen/go-sail/v3/lib/logger"
"github.com/keepchen/go-sail/v3/sail"
"github.com/keepchen/go-sail/v3/sail/config"
)
Expand Down Expand Up @@ -51,7 +52,7 @@ var (
ForceHttpCode200: true,
}
registerRoutes = func(ginEngine *gin.Engine) {
ginEngine.GET("/hello", func(c *gin.Conext){
ginEngine.GET("/hello", func(c *gin.Context){
c.String(http.StatusOK, "%s", "hello, world!")
})
}
Expand All @@ -63,7 +64,10 @@ var (
}
)

sail.WakeupHttp("go-sail", conf, apiOption).Launch(registerRoutes, before, after)
sail.WakeupHttp("go-sail", conf).
SetupApiOption(apiOption).
Hook(registerRoutes, before, after).
Launch()
```
当你看到终端如下图所示内容就表示服务启动成功了:

Expand Down Expand Up @@ -138,7 +142,7 @@ type UserInfo struct {

//implement dto.IResponse interface
func (v UserInfo) GetData() interface{} {
return v.Data
return v.Data
}

//handler
Expand Down
12 changes: 8 additions & 4 deletions README_EN.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div align="center">
<h1><img src="./sailboat-solid.svg" alt="sailboat-solid" title="sailboat-solid" width="300" /></h1>
<h1><img src="./sailboat-solid-colorful.svg" alt="sailboat-solid" title="sailboat-solid" width="300" /></h1>
</div>

[![Go](https://github.com/keepchen/go-sail/actions/workflows/go.yml/badge.svg)](https://github.com/keepchen/go-sail/actions/workflows/go.yml) [![CodeQL](https://github.com/keepchen/go-sail/actions/workflows/codeql.yml/badge.svg)](https://github.com/keepchen/go-sail/actions/workflows/codeql.yml) [![Go Report Card](https://goreportcard.com/badge/github.com/keepchen/go-sail/v3)](https://goreportcard.com/report/github.com/keepchen/go-sail/v3)
Expand All @@ -20,6 +20,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/keepchen/go-sail/v3/constants"
"github.com/keepchen/go-sail/v3/http/api"
"github.com/keepchen/go-sail/v3/lib/logger"
"github.com/keepchen/go-sail/v3/sail"
"github.com/keepchen/go-sail/v3/sail/config"
)
Expand Down Expand Up @@ -51,7 +52,7 @@ var (
ForceHttpCode200: true,
}
registerRoutes = func(ginEngine *gin.Engine) {
ginEngine.GET("/hello", func(c *gin.Conext){
ginEngine.GET("/hello", func(c *gin.Context){
c.String(http.StatusOK, "%s", "hello, world!")
})
}
Expand All @@ -63,7 +64,10 @@ var (
}
)

sail.WakeupHttp("go-sail", conf, apiOption).Launch(registerRoutes, before, after)
sail.WakeupHttp("go-sail", conf).
SetupApiOption(apiOption).
Hook(registerRoutes, before, after).
Launch()
```
Console screenshot after launched like this:

Expand Down Expand Up @@ -138,7 +142,7 @@ type UserInfo struct {

// implement dto.IResponse interface
func (v UserInfo) GetData() interface{} {
return v.Data
return v.Data
}

//handler
Expand Down
31 changes: 29 additions & 2 deletions http/middleware/withcors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/gin-gonic/gin"
)

// WithCors 允许浏览器跨域请求
func WithCors(headers map[string]string) gin.HandlerFunc {
// WithCorsOnlyOptions 允许浏览器跨域请求,仅处理options探测请求
func WithCorsOnlyOptions(headers map[string]string) gin.HandlerFunc {
return func(c *gin.Context) {
//处理浏览器跨域options探测请求
if c.Request.Method == http.MethodOptions {
Expand Down Expand Up @@ -37,3 +37,30 @@ func WithCors(headers map[string]string) gin.HandlerFunc {
c.Next()
}
}

// WithCors 允许浏览器跨域请求
func WithCors(headers map[string]string) gin.HandlerFunc {
return func(c *gin.Context) {
origin := c.Request.Header.Get("Origin")
if len(origin) == 0 {
origin = "*"
}

if headers == nil {
c.Writer.Header().Set("Access-Control-Allow-Origin", origin)
c.Writer.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type, Content-Length")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, PATCH, DELETE, OPTIONS, HEAD")
c.Writer.Header().Set("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, "+
"Access-Control-Allow-Headers, Content-Type, Authorization")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
} else {
for key, value := range headers {
c.Writer.Header().Set(key, value)
}
}

c.Writer.WriteHeader(http.StatusNoContent)

c.Next()
}
}
5 changes: 5 additions & 0 deletions sail/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ type KafkaExtraConf struct {

var config *Config

// Set 设置配置
func Set(conf *Config) {
config = conf
}

// Get 获取配置
func Get() *Config {
return config
Expand Down
32 changes: 31 additions & 1 deletion sail/config/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func PrintTemplateConfig(format string, writeToFile ...string) {
//
// @param format 内容格式,支持: json|yaml|toml
//
// @param writeToFile 写入到目标文件(可选)
// @param source 配置源字符
func ParseConfigFromBytes(format string, source []byte) (*Config, error) {
var (
formatList = [...]string{"json", "yaml", "toml"}
Expand All @@ -75,3 +75,33 @@ func ParseConfigFromBytes(format string, source []byte) (*Config, error) {

return &cfg, err
}

// ParseConfigFromBytesToDst 从字符串解析配置到目标结构
//
// @param format 内容格式,支持: json|yaml|toml
//
// @param source 配置源字符
//
// @param dst 目标结构
//
// @return 返回值与参数dst类型相同,需要进行断言
func ParseConfigFromBytesToDst(format string, source []byte, dst interface{}) (interface{}, error) {
var (
formatList = [...]string{"json", "yaml", "toml"}
err error
)

switch format {
case formatList[0]:
err = json.Unmarshal(source, dst)
case formatList[1]:
err = yaml.Unmarshal(source, dst)
case formatList[2]:
err = toml.Unmarshal(source, dst)
default:
fmt.Printf("[GO-SAIL] <Config> dump config by using unknown format: %s\n", format)
err = fmt.Errorf("[GO-SAIL] <Config> dump config by using unknown format: %s\n", format)
}

return dst, err
}
24 changes: 23 additions & 1 deletion sail/sail.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,26 @@ import (

// Sailor 船员就位
type Sailor interface {
// SetupApiOption
//
// 设置统一返回配置
SetupApiOption(opt *api.Option) Sailor
Launch(registerRoutes func(ginEngine *gin.Engine))
// Hook 挂载相关方法
//
// @param registerRoutes 注册路由函数
//
// @param beforeFunc 前置自定义处理函数(可选),在框架函数之前执行,注意自定义函数是同步执行的
//
// @param afterFunc 后置自定义处理函数(可选),在框架函数之后执行,注意自定义函数是同步执行的
Hook(registerRoutes func(ginEngine *gin.Engine), beforeFunc, afterFunc func()) Launchpad
// Launch 启动
//
// @param registerRoutes 注册路由函数
//
// # Note:
//
// 未设置前置自动函数、未设置后置自定义函数
Launch(registerRoutes func(ginEngine *gin.Engine))
}

// Framework 框架配置
Expand All @@ -37,6 +54,11 @@ var _ Sailor = &Framework{}

// Launchpad 启动台
type Launchpad interface {
// Launch 启动
//
// # Note:
//
// 已注册路由、已设置前置自动函数、已设置后置自定义函数
Launch()
}

Expand Down
Binary file added sailboat-solid-colorful.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions sailboat-solid-colorful.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e29c804

Please sign in to comment.