common

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2022 License: MIT Imports: 9 Imported by: 0

README

common

基于gorm、gin、zap、viper实现的对一些常用库的二次封装以及连接mysql、redis、etcd、日志的公共包,简化开发过程,能快速的搭建一个web后端服务器

1. 技术选型

2. 目录结构

目录 说明
dbclient 基于gorm实现的数据库mysql连接
etcdclient 提供etcd连接
httpclient 提供http(get/post)请求方法
logger 基于zap实现的日志管理,实现日志分类以及分割
notify 提供email和webhook两种通知方式
server 基于gin实现对web服务的启动
utils 一些工具类,比如system.go用来获取服务器cpu和内存信息
config.go 配置信息的结构体并基于viper加载配置文件
env.go 一些环境变量
request.go 常见的绑定请求的的结构体
response.go 常见的请求返回的结构体

3. 使用方法

go get -u github.com/tmnhs/common

详细的使用示例可见: common-test

3.1.配置文件

配置文件支持多种环境(testing/prodution)和多种格式(json/yaml/ini)

注意事项:配置文件的目录必须是下面这个样子

├── cmd
├── conf
|     ├── production                #生产环境,支持json、yaml、ini三种配置文件
|     |          └── main.json
|     └── testing
|                └── main.json      #测试环境,支持json、yaml、ini三种配置文件
└── internal

配置文件示例(json格式)

{
  "mysql": {
    "path": "127.0.0.1",
    "port": "3306",
    "config": "charset=utf8mb4&parseTime=True&loc=Local",
    "db-name": "common-test",
    "username": "root",
    "password": "root",
    "max-idle-conns": 100,
    "max-open-conns": 100,
    "log-mode": "info",
    "log-zap": false
  },
  "redis": {
    "addr": "127.0.0.1:6379",
    "password": "",
    "db": 0
  },
  "system": {
    "env": "testing",
    "addr": 8089,
    "version": "v1.0.2"
  },
  "etcd": {
    "endpoints": [
      "http://127.0.0.1:2379"
    ],
    "username": "",
    "password": "",
    "dial-timeout": 2,
    "req-timeout": 5
  },
  "email": {
    "port": 465,
    "from": "test@qq.com",
    "host": "smtp.qq.com",
    "is-ssl": true,
    "secret": "test",
    "nickname": "common-test",
    "to": [
      "test@test.mobi"
    ]
  },
  "webhook": {
    "url": "url",
    "kind": "feishu"
  },
  "log": {
    "level": "debug",
    "format": "console",
    "prefix": "[common]",
    "director": "logs",
    "showLine": false,
    "encode-level": "LowercaseLevelEncoder",
    "stacktrace-key": "stacktrace",
    "log-in-console": true
  }
}
3.2 开启一个web应用
func main() {
  	//参数为需要启动的服务(etcd/mysql/redis)
    //连接成功后可以通过dbclient.GetMysqlDD(),etcdClient.GetEtcd(),redisclient.GetRedis()获取对应的client
    //通过logger.GetLogger()获取日志处理器
    //通过common.GetConfigModels()获取配置文件的信息
	srv, err := server.NewApiServer(server.WithEtcd(),server.WithMysql(),server.WithRedis())
	if err != nil {
		logger.GetLogger().Error(fmt.Sprintf("new api server error:%s", err.Error()))
		os.Exit(1)
	}
	// 注册路由
	srv.RegisterRouters(handler.RegisterRouters)

	// 建表,当然,如果不需要可以直接注释掉
	err = service.RegisterTables(dbclient.GetMysqlDB())
	if err != nil {
		logger.GetLogger().Error(fmt.Sprintf("init db table error:%#v", err))
	}
	err = srv.ListenAndServe()
	if err != nil {
		logger.GetLogger().Error(fmt.Sprintf("startup api server error:%v", err.Error()))
		os.Exit(1)
	}
	os.Exit(0)
}

3.3 注册路由

func RegisterRouters(r *gin.Engine) {

	configRoute(r)

	configNoRoute(r)
}

func configRoute(r *gin.Engine) {

	hello := r.Group("/ping")
	{
		hello.GET("", func(c *gin.Context) {
			c.JSON(200, "pong")
		})
	}

	base := r.Group("")
	{
		base.POST("register", defaultUserRouter.Register)
		base.POST("login", defaultUserRouter.Login)
	}

	user := r.Group("/user")
	user.Use(middlerware.JWTAuth())
	{
		user.POST("del", defaultUserRouter.Delete)
		user.POST("update", defaultUserRouter.Update)
		user.POST("change_pw", defaultUserRouter.ChangePassword)
		user.GET("find", defaultUserRouter.FindById)
		user.POST("search", defaultUserRouter.Search)
	}
}

func configNoRoute(r *gin.Engine) {
	/*	r.LoadHTMLGlob("./dist/*.html") // npm打包成dist的路径
		r.StaticFile("favicon.ico", "./dist/favicon.ico")
		r.Static("/css", "./dist/css")
		r.Static("/fonts", "./dist/fonts")
		r.Static("/js", "./dist/js")
		r.Static("/img", "./dist/img")
		r.StaticFile("/", "./dist/index.html") // 前端网页入口页面*/
}

4. 可能出现的问题

如果引入包并且go mod tidy 出现以下错误时

go: finding module for package google.golang.org/grpc/naming
github.com/tmnhs/common-test/cmd imports
        github.com/tmnhs/common/server imports
        github.com/tmnhs/common/etcdclient imports
        github.com/coreos/etcd/clientv3 tested by
        github.com/coreos/etcd/clientv3.test imports
        github.com/coreos/etcd/integration imports
        github.com/coreos/etcd/proxy/grpcproxy imports
        google.golang.org/grpc/naming: module google.golang.org/grpc@latest found (v1.50.1), but does not contain package google.golang.org/grpc/naming

可以在go.mod中添加以下一行(这个报错和etcd连接的第三方库有版本冲突)

replace google.golang.org/grpc => google.golang.org/grpc v1.26.0

5. 交流讨论

如有问题欢迎加qq:1685290935一起交流讨论

Documentation

Index

Constants

View Source
const (
	ExtensionJson = ".json"
	ExtensionYaml = ".yaml"
	ExtensionInI  = ".ini"

	NameSpace = "conf"
)
View Source
const (
	EnvTesting    = Environment("testing")
	EnvProduction = Environment("production")
)

use export ENVIRONMENT=testing set global environment

View Source
const (
	SUCCESS = 200
	ERROR   = 1000

	ErrorRequestParameter = 1001
	ErrorTokenGenerate    = 1002
	ErrorUserNameExist    = 1003
)
View Source
const (
	Version   = "v1.1.0"
	ApiModule = "common/api-server"
)

Variables

This section is empty.

Functions

func FailWithCode

func FailWithCode(code int, c *gin.Context)

func FailWithDetailed

func FailWithDetailed(code int, data interface{}, message string, c *gin.Context)

func FailWithMessage

func FailWithMessage(code int, message string, c *gin.Context)

func Ok

func Ok(c *gin.Context)

func OkWithData

func OkWithData(data interface{}, c *gin.Context)

func OkWithDetailed

func OkWithDetailed(data interface{}, message string, c *gin.Context)

func OkWithMessage

func OkWithMessage(message string, c *gin.Context)

func Result

func Result(code int, data interface{}, msg string, c *gin.Context)

Types

type ByID

type ByID struct {
	ID int `json:"id" form:"id"`
}

type ByIDS

type ByIDS struct {
	IDS []int `json:"ids" form:"ids"`
}

type Config

type Config struct {
	WebHook WebHook `mapstructure:"webhook" json:"webhook" yaml:"webhook" ini:"webhook"`
	Log     Log     `mapstructure:"log" json:"log" yaml:"log" ini:"log"`
	Email   Email   `mapstructure:"email" json:"email" yaml:"email" ini:"email"`
	System  System  `mapstructure:"system" json:"system" yaml:"system" ini:"system"`
	Mysql   Mysql   `mapstructure:"mysql" json:"mysql" yaml:"mysql" ini:"mysql"`
	Redis   Redis   `mapstructure:"redis" json:"redis" yaml:"redis" ini:"redis"`
	Etcd    Etcd    `mapstructure:"etcd" json:"etcd" yaml:"etcd" ini:"etcd"`
}

func GetConfigModels

func GetConfigModels() *Config

func LoadConfig

func LoadConfig(env, configFileName string) (*Config, error)

type Email

type Email struct {
	Port     int      `mapstructure:"port" json:"port" yaml:"port"`             // 端口
	From     string   `mapstructure:"from" json:"from" yaml:"from"`             // 收件人
	Host     string   `mapstructure:"host" json:"host" yaml:"host"`             // 服务器地址
	IsSSL    bool     `mapstructure:"is-ssl" json:"isSSL" yaml:"is-ssl"`        // 是否SSL
	Secret   string   `mapstructure:"secret" json:"secret" yaml:"secret"`       // 密钥
	Nickname string   `mapstructure:"nickname" json:"nickname" yaml:"nickname"` // 昵称
	To       []string `mapstructure:"to" json:"to" yaml:"to" ini:"to"`
}

type Environment

type Environment string

func NewGlobalEnvironment

func NewGlobalEnvironment() (Environment, error)

NewGlobalEnvironment 读取系统全局配置的环境变量

func (Environment) Invalid

func (env Environment) Invalid() bool

func (*Environment) Production

func (env *Environment) Production() Environment

func (*Environment) String

func (env *Environment) String() string

func (*Environment) Testing

func (env *Environment) Testing() Environment

type Etcd

type Etcd struct {
	Endpoints   []string `mapstructure:"endpoints" json:"endpoints" yaml:"endpoints" ini:"endpoints"`
	Username    string   `mapstructure:"username" json:"username" yaml:"username" ini:"username"`
	Password    string   `mapstructure:"password" json:"password" yaml:"password" ini:"password"`
	DialTimeout int64    `mapstructure:"dial-timeout" json:"dial-timeout" yaml:"dial-timeout" ini:"dial-timeout"`
	ReqTimeout  int64    `mapstructure:"req-timeout" json:"req-timeout" yaml:"req-timeout" ini:"req-timeout"`
}

type Log

type Log struct {
	Level         string `mapstructure:"level" json:"level" yaml:"level"`                           // 级别
	Format        string `mapstructure:"format" json:"format" yaml:"format"`                        // 输出
	Prefix        string `mapstructure:"prefix" json:"prefix" yaml:"prefix"`                        // 日志前缀
	Director      string `mapstructure:"director" json:"director"  yaml:"director"`                 // 日志文件夹
	ShowLine      bool   `mapstructure:"show-line" json:"showLine" yaml:"showLine"`                 // 显示行
	EncodeLevel   string `mapstructure:"encode-level" json:"encodeLevel" yaml:"encode-level"`       // 编码级
	StacktraceKey string `mapstructure:"stacktrace-key" json:"stacktraceKey" yaml:"stacktrace-key"` // 栈名
	LogInConsole  bool   `mapstructure:"log-in-console" json:"logInConsole" yaml:"log-in-console"`  // 输出控制台
}

type Mysql

type Mysql struct {
	Path         string `mapstructure:"path" json:"path" yaml:"path"`                             // 服务器地址
	Port         string `mapstructure:"port" json:"port" yaml:"port"`                             // 端口
	Config       string `mapstructure:"config" json:"config" yaml:"config"`                       // 高级配置
	Dbname       string `mapstructure:"db-name" json:"dbname" yaml:"db-name"`                     // 数据库名
	Username     string `mapstructure:"username" json:"username" yaml:"username"`                 // 数据库用户名
	Password     string `mapstructure:"password" json:"password" yaml:"password"`                 // 数据库密码
	MaxIdleConns int    `mapstructure:"max-idle-conns" json:"maxIdleConns" yaml:"max-idle-conns"` // 空闲中的最大连接数
	MaxOpenConns int    `mapstructure:"max-open-conns" json:"maxOpenConns" yaml:"max-open-conns"` // 打开到数据库的最大连接数
	LogMode      string `mapstructure:"log-mode" json:"logMode" yaml:"log-mode"`                  // 是否开启Gorm全局日志
	LogZap       bool   `mapstructure:"log-zap" json:"logZap" yaml:"log-zap"`                     // 是否通过zap写入日志文件
}

func (*Mysql) Dsn

func (m *Mysql) Dsn() string

func (*Mysql) EmptyDsn

func (m *Mysql) EmptyDsn() string

type PageInfo

type PageInfo struct {
	Page     int `json:"page" form:"page"`           // 页码
	PageSize int `json:"page_size" form:"page_size"` // 每页大小
}

PageInfo Paging common input parameter structure

func (*PageInfo) Check

func (page *PageInfo) Check()

type PageResult

type PageResult struct {
	List     interface{} `json:"list"`
	Total    int64       `json:"total"`
	Page     int         `json:"page"`
	PageSize int         `json:"page_size"`
}

type Redis

type Redis struct {
	DB       int    `mapstructure:"db" json:"db" yaml:"db"`                   // redis的哪个数据库
	Addr     string `mapstructure:"addr" json:"addr" yaml:"addr"`             // 服务器地址:端口
	Password string `mapstructure:"password" json:"password" yaml:"password"` // 密码
}

type Response

type Response struct {
	Code int         `json:"code"`
	Data interface{} `json:"data"`
	Msg  string      `json:"msg"`
}

type System

type System struct {
	Env     string `mapstructure:"env" json:"env" yaml:"env" ini:"env"`
	Addr    int    `mapstructure:"addr" json:"addr" yaml:"addr" ini:"addr"`
	Version string `mapstructure:"version" json:"version" yaml:"version" ini:"version"`
}

type WebHook

type WebHook struct {
	Kind string `mapstructure:"kind" json:"kind" yaml:"kind" ini:"kind"`
	Url  string `mapstructure:"url" json:"url" yaml:"url" ini:"kind"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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