server

package
v1.5.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2024 License: Apache-2.0 Imports: 26 Imported by: 1

README

server

server包是用于更加方便的开发web项目而封装的包,开启配置的话,如下

简单示例
# application.yml 内容
base:
  server:
    # 是否启用,默认:false
    enable: true
package main

import (
    "github.com/gin-gonic/gin"
    "github.com/isyscore/isc-gobase/server"
    "github.com/isyscore/isc-gobase/server/rsp"
)

func main() {
    server.Get("api/get", GetData)
    server.Run()
}

func GetData(c *gin.Context) {
    rsp.SuccessOfStandard(c, "value")
}

运行如下

root@user ~> curl http://localhost:8080/api/get
{"code":0,"data":"value","message":"success"}

全部配置

isc-gobase项目内置的一些server的配置

api-module: sample

base:
  api:
    # api前缀,默认包含api前缀,如果路径本身有api,则不再添加api前缀
    prefix: /api
  application:
    # 应用名,默认为空
    name: isc-gobase-demo
    # 服务版本号
    version: vx.x.xx
  server:
    # 是否启用,默认:true
    enable: true
    # 端口号,默认:8080
    port: 8080
    gin:
      # 有三种模式:debug/release/test,默认 release
      mode: debug
      pprof:
        # pprof开关是否可以开启,默认false
        enable: false
    cors:
      # 是否启用跨域配置,默认启用
      enable: true
    request:
      print:
        # 是否打印:true, false;默认 false
        enable: false
        # 打印的话日志级别,默认debug
        level: info
        # 指定要打印请求的uri
        include-uri:
          - /xxx/x/xxx
          - /xxx/x/xxxy
        # 指定不打印请求的uri
        exclude-uri:
          - /xxx/x/xxx
          - /xxx/x/xxxy
    response:
      print:
        # 是否打印:true, false;默认 false
        enable: false
        # 打印的话日志级别,默认debug
        level: info
        # 指定要打印请求的uri
        include-uri:
          - /xxx/x/xxx
          - /xxx/x/xxxy
        # 指定不打印请求的uri
        exclude-uri:
          - /xxx/x/xxx
          - /xxx/x/xxxy
    exception:
      # 异常返回打印
      print:
        # 是否启用:true, false;默认 false
        enable: true
        # 一些异常httpStatus不打印;默认可不填
        exclude:
          - 408
          - 409
    # 版本号设置,默认值:unknown
    version: 1.0.0
  swagger:
    # 是否开启swagger:true, false;默认 false
    enable: false

isc-gobase项目内置的一些endpoint端口

base:
  # 内部开放的 endpoint
  endpoint:
    # 健康检查处理,默认关闭,true/false
    health:
      enable: true
    # 配置的管理(查看和变更),默认关闭,true/false
    config:
      enable: true
    # bean的管理(属性查看、属性修改、函数调用),默认false
    bean:
      enable: true
api.prefix和api-module介绍

其中api和api-module这个配置最后的url前缀是
{api.prefix}/{api-module}/业务代码

比如如上:

root@user ~> curl http://localhost:8080/api/sample/get/data
{"code":0,"data":"ok","message":"success"}
server介绍

额外说明: 提供request和response的打印,用于调试时候使用

# 开启请求的打印,开启后默认打印所有请求,如果想打印指定uri,请先配置uri
curl -X PUT http://localhost:xxx/{api-prefix}/{api-module}/config/update -d '{"key":"base.server.request.print.enable", "value":"true"}'
# 开启响应的打印
curl -X PUT http://localhost:xxx/{api-prefix}/{api-module}/config/update -d '{"key":"base.server.request.print.enable", "value":"true"}'
# 开启异常的打印
curl -X PUT http://localhost:xxx/{api-prefix}/{api-module}/config/update -d '{"key":"base.server.exception.print.enable", "value":"true"}'
指定uri打印

如果不指定uri则会默认打印所有的请求

# 指定要打印的请求的uri
curl -X PUT http://localhost:xxx/{api-prefix}/{api-module}/config/update -d '{"key":"base.server.request.print.include-uri[0]", "value":"/api/xx/xxx"}'
# 指定不要打印的请求uri
curl -X PUT http://localhost:xxx/{api-prefix}/{api-module}/config/update -d '{"key":"base.server.request.print.exclude-uri[0]", "value":"/api/xx/xxx"}'

# 指定要打印的响应的uri
curl -X PUT http://localhost:xxx/{api-prefix}/{api-module}/config/update -d '{"key":"base.server.request.print.include-uri[0]", "value":"/api/xx/xxx"}'
# 指定不要打印的响应uri
curl -X PUT http://localhost:xxx/{api-prefix}/{api-module}/config/update -d '{"key":"base.server.request.print.exclude-uri[0]", "value":"/api/xx/xxx"}'

提示:

  • 如果"请求"和"响应"都开启打印,则只会打印"响应",因为响应中已经包括了"请求"
  • 指定多个uri的话,如下,配置其实是按照properties的方式进行指定的
curl -X PUT http://localhost:xxx/{api-prefix}/{api-module}/config/update -d '{"key":"base.server.request.print.include-uri[0]", "value":"/api/xx/xxx"}'
curl -X PUT http://localhost:xxx/{api-prefix}/{api-module}/config/update -d '{"key":"base.server.request.print.include-uri[1]", "value":"/api/xx/xxy"}'
curl -X PUT http://localhost:xxx/{api-prefix}/{api-module}/config/update -d '{"key":"base.server.request.print.include-uri[2]", "value":"/api/xx/xxz"}'
...

swagger 使用介绍

如果想基于 gobase 来使用 swagger 这里需要按照如下步骤来处理

1. 安装命令

这个是 go-swagger 必需

go install github.com/swaggo/swag/cmd/swag
2. 添加注解

这里按照go-swagger官网的注解进行编写即可,比如

// @Summary xxx
// @title xxx
// @Tags xxx
// @Router /api/xx/xx/xxxx/xxx [post]
// @param param body Addxxxx true "用户请求参数"
// @Success 200 {object} any
3. 生成swagger文件

这里按照go-swagger官网的注解进行编写即可

swag init
4. 添加swagger的doc引入

执行命令swag init后会生成docs文件夹,里面有相关的swagger配置。这里需要代码显示的引入,否则swagger解析不出来,建议在main.go中引入,示例:

package main

import (
    "github.com/isyscore/isc-gobase/server"
    // 这里:不引入就会在swagger生成的页面中找不到doc.json文件 
    _ "isc-xx-service/docs"
)

// @Title xxx
// @Version 1.0.0
func main() {
    server.Run()
}
5. 开启开关,运行程序

代码开启如下开关

base:
  swagger:
    enable: true

启动程序后,打开网页即可看到

http://xxxx:port/swagger/index.html
问题

如果遇到如下问题,则执行下如下即可

../../../go/src/pkg/mod/github.com/swaggo/swag@v1.8.5/gen/gen.go:18:2: missing go.sum entry for module providing package github.com/ghodss/yaml (imported by github.com/swaggo/swag/gen); to add:
        go get github.com/swaggo/swag/gen@v1.8.5
../../../go/src/pkg/mod/github.com/swaggo/swag@v1.8.5/cmd/swag/main.go:10:2: missing go.sum entry for module providing package github.com/urfave/cli/v2 (imported by github.com/swaggo/swag/cmd/swag); to add:
        go get github.com/swaggo/swag/cmd/swag@v1.8.5

执行

go get github.com/swaggo/swag/gen
go get github.com/swaggo/swag/cmd/swag

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ApiPrefix = "/api"
View Source
var GoBaseVersion = "1.5.1"

Functions

func AddGinHandlers added in v1.4.5

func AddGinHandlers(handler gin.HandlerFunc)

提供给外部注册使用

func All added in v1.0.0

func All(path string, handler gin.HandlerFunc) gin.IRoutes

func AllWith added in v1.0.0

func AllWith(path string, header []string, versionName []string, handler gin.HandlerFunc) gin.IRoutes

func ConfigChangeListener added in v1.3.0

func ConfigChangeListener(event listener.BaseEvent)

func Cors

func Cors() gin.HandlerFunc

Cors 配置允许跨域请求

func Delete added in v1.0.0

func Delete(path string, handler gin.HandlerFunc) gin.IRoutes

func DeleteWith added in v1.0.0

func DeleteWith(path string, header []string, versionName []string, handler gin.HandlerFunc) gin.IRoutes

func Engine

func Engine() *gin.Engine

func ErrHandler added in v1.4.5

func ErrHandler() gin.HandlerFunc

func Get added in v1.0.0

func Get(path string, handler gin.HandlerFunc) gin.IRoutes

func GetPost added in v1.0.0

func GetPost(path string, handler gin.HandlerFunc) gin.IRoutes

func GetPostWith added in v1.0.0

func GetPostWith(path string, header []string, versionName []string, handler gin.HandlerFunc) gin.IRoutes

func GetWith added in v1.0.0

func GetWith(path string, header []string, versionName []string, handler gin.HandlerFunc) gin.IRoutes
func Head(path string, handler gin.HandlerFunc) gin.IRoutes

func HeadWith added in v1.0.0

func HeadWith(path string, header []string, versionName []string, handler gin.HandlerFunc) gin.IRoutes

func InitServer

func InitServer()

func Options added in v1.0.0

func Options(path string, handler gin.HandlerFunc) gin.IRoutes

func OptionsWith added in v1.0.0

func OptionsWith(path string, header []string, versionName []string, handler gin.HandlerFunc) gin.IRoutes

func Post added in v1.0.0

func Post(path string, handler gin.HandlerFunc) gin.IRoutes

func PostWith added in v1.0.0

func PostWith(path string, header []string, versionName []string, handler gin.HandlerFunc) gin.IRoutes

func Put added in v1.0.0

func Put(path string, handler gin.HandlerFunc) gin.IRoutes

func PutWith added in v1.0.0

func PutWith(path string, header []string, versionName []string, handler gin.HandlerFunc) gin.IRoutes

func RegisterBeanWatchEndpoint added in v1.1.1

func RegisterBeanWatchEndpoint(apiBase string) gin.IRoutes

func RegisterConfigWatchEndpoint added in v0.3.0

func RegisterConfigWatchEndpoint(apiBase string) gin.IRoutes

func RegisterCustomHealthCheck added in v0.2.0

func RegisterCustomHealthCheck(apiBase string, status func() string, init func() string, destroy func() string) gin.IRoutes

func RegisterHealthCheckEndpoint added in v0.3.0

func RegisterHealthCheckEndpoint(apiBase string) gin.IRoutes

func RegisterHelpEndpoint added in v1.3.0

func RegisterHelpEndpoint(apiBase string) gin.IRoutes

func RegisterPlugin

func RegisterPlugin(plugin gin.HandlerFunc) gin.IRoutes

func RegisterRoute

func RegisterRoute(path string, method HttpMethod, handler gin.HandlerFunc) gin.IRoutes

func RegisterRouteWithHeaders added in v1.0.0

func RegisterRouteWithHeaders(path string, method HttpMethod, header []string, versionName []string, handler gin.HandlerFunc) gin.IRoutes

func RegisterStatic

func RegisterStatic(relativePath string, rootPath string) gin.IRoutes

func RegisterStaticFile

func RegisterStaticFile(relativePath string, filePath string) gin.IRoutes

func RegisterSwaggerEndpoint added in v1.3.1

func RegisterSwaggerEndpoint() gin.IRoutes

func RegisterWebSocketRoute added in v0.8.0

func RegisterWebSocketRoute(path string, svr *websocket.Server) gin.IRoutes

func RequestSaveHandler added in v1.4.5

func RequestSaveHandler() gin.HandlerFunc

func Run added in v1.0.0

func Run()

func StartServer

func StartServer()

func Timeout added in v1.3.3

func Timeout(timeout time.Duration) gin.HandlerFunc

func Use added in v1.1.1

func Use(middleware ...gin.HandlerFunc) gin.IRoutes

Types

type ApiPath added in v1.0.0

type ApiPath struct {
	Path     string
	Handler  gin.HandlerFunc
	Method   HttpMethod
	Versions ISCList[*ApiVersion]
}

func GetApiPath added in v1.0.0

func GetApiPath(path string, method HttpMethod) *ApiPath

func NewApiPath added in v1.0.0

func NewApiPath(path string, method HttpMethod) *ApiPath

func (*ApiPath) AddVersion added in v1.0.0

func (ap *ApiPath) AddVersion(header []string, version []string, handler gin.HandlerFunc)

type ApiVersion added in v1.0.0

type ApiVersion struct {
	Header  []string
	Version []string
	Handler gin.HandlerFunc
}

type HttpMethod

type HttpMethod int
const (
	HmAll HttpMethod = iota
	HmGet
	HmPost
	HmPut
	HmDelete
	HmOptions
	HmHead
	HmGetPost
)

Directories

Path Synopsis
test

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL