Code Monkey home page Code Monkey logo

gin-api's Introduction

gin-api

基于 go-gin 开发的 api 框架,封装各种常用组件
有疑问随时联系本人
QQ群:909211071
个人QQ:444216978
微信:AbleYu_

配置相关

支持apollo、json、ini三种格式,文件配置需放到main.go同级目录configs下

  • apollo:嵌套json格式,用于兼容mysql、redis等多实例
  • json:嵌套json格式,用于兼容mysql、redis等多实例
  • ini:section格式,用于兼容mysql、redis等多实例

通过 app_const.server.CONFIG_SOURCE 变量切换
package app_const

const (
	SERVICE_NAME  = "purchase-server"
	SERVICE_PORT  = 777
	PRODUCT       = "gin-api"
	MODULE        = "gin-api"
	ENV           = "development"
	CONFIG_SOURCE = "ini" //apollo、json、ini
	CONFIGS_NUM   = 10     //配置文件数,影响配置file享元map初始化大小
)

ini(默认格式)

env.ini example:

[env]
env = development

log.ini example:

[log]
dir = /data/logs
area = 1
query_field = "logid"
header_field = "X-Logid"

mysql.ini example:

[default_read]
host = 127.0.0.1
user = why
password = why123
port = 3306
db = why
charset = utf8
max_open = 8
max_idle = 4
exec_timeout = 10000

[default_write]
host = 127.0.0.1
user = why
password = why123
port = 3306
db = why
charset = utf8
max_open = 8
max_idle = 4
exec_timeout = 10000

redis.ini example:

[default]
host = 127.0.0.1
port = 6379
db = 0
auth =
max_active = 600
max_idle = 10
exec_timeout = 100000

log.ini example:

[log]
dir = /data/logs/
area = 1
query_field = "logid"
header_field = "X-Logid"

es.ini example:

[default]
host = http://127.0.0.1
port = 9200

env.json example:

{
  "env":{
    "env": "development"
  }
}

log.json example:

{
  "log":{
    "dir":"/data/logs",
    "area":1,
    "query_field":"logid",
    "header_field":"X-Logid"
  }
}

运行

  1. 创建上述基础配置文件
  2. log.ini中的dir目录确保本地存在且有写入权限
  3. go run main.go
[why@localhost] ~/Desktop/go/gin-api$go run main.go 
2020/12/20 17:44:43 load redis.json
2020/12/20 17:44:43 load mysql.json
2020/12/20 17:44:43 new test_dao
2020/12/20 17:44:43 new test_service
2020/12/20 17:44:43 load log.json
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

2020/12/20 17:44:43 load env.json
[GIN-debug] GET    /ping                     --> gin-api/controllers/ping.Ping (6 handlers)
[GIN-debug] GET    /test/rpc                 --> gin-api/controllers/opentracing.Rpc (6 handlers)
[GIN-debug] GET    /test/panic               --> gin-api/controllers/opentracing.Panic (6 handlers)
[GIN-debug] GET    /test/conn                --> gin-api/controllers/conn.Do (6 handlers)
2020/12/20 17:44:43 Actual pid is 9104

注意:测试 /test/conn 接口时,应确检查如下几项:

  1. mysql 和 redis 配置文件符合示例配置文件中的default(当然可以自定义,不过需要更改 test_model.go 和 goods_service.go 中的 DB_NAME )
  2. 创建 test 库
  3. 创建 test 表并随意插入数据
CREATE TABLE `test` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `goods_id` bigint(20) unsigned NOT NULL,
  `name` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  KEY `idx_goods` (`goods_id`)
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin 

gin-api's People

Contributors

why444216978 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.