1 Star 2 Fork 0

井24 / cld-quick-cli

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

cld-quick-cli

[toc]

介绍

一个开发Golang的快速脚手架项目!

技术栈

  • gin web框架
  • zap 日志库
  • Go-reids
  • mysqlDriver
  • snowflake 雪花算法分布式id
  • sqlx
  • Validator 参数校验
  • jwt5

目录结构说明

. tree                                                                                                                                                                                                                          ─╯
├── LICENSE
├── README.en.md
├── README.md
├── cld-cli
├── com  							# http 网络请求code + 返回方法函数
│   ├── code.go
│   └── response.go
├── conf  							# 配置文件
│   ├── webapp-prod-config.yaml
│   └── webappDevConfig.yaml
├── config  						# 配置结构体实例
│   └── config.go
├── controllers  					# 控制器 参数校验 调用业务层
│   ├── community.go
│   ├── files.go
│   ├── post.go
│   └── user.go
├── dao  							# 数据库操作封装
│   ├── community.go
│   ├── files.go
│   ├── post.go
│   └── user.go
├── database  						# 数据库链接 models定义数据表对应的结构体 key redis的业务Key
│   ├── keys
│   │   └── post.go
│   ├── models
│   │   ├── common.go
│   │   ├── community_models.go
│   │   ├── post.go
│   │   └── user_model.go
│   ├── mysql.go
│   └── redis.go
├── note  						# 一些小文档
│   ├── 一些库的使用细节.md
│   └── 一些标准.md
├── go.mod
├── go.sum
├── logic  						# 业务层代码
│   ├── community.go
│   ├── files.go
│   ├── post.go
│   └── user.go
├── logs  						# 日志存放位置
│   ├── error_web.log
│   ├── logger.go
│   └── web.log
├── main.go
├── makefile
├── pkg  						# 工具函数,不与任何业务代码耦合的工具代码 
│   ├── bcrypt.go
│   ├── jsonwebtoken.go
│   ├── snowflake.go
│   ├── translator.go
│   ├── utils.go
│   └── validators.go
├── router 						# router
│   ├── middlewares						# middle 存放 gin的中间件!
│   │   ├── authorization.go
│   │   ├── cldLogger.go
│   │   ├── cldRecover.go
│   │   └── cors.go
│   ├── modules							# modules 存放不同业务模块的router!
│   │   ├── community.go
│   │   ├── files.go
│   │   ├── post.go
│   │   └── user.go
│   ├── parameters						# parameters 路由器 控制层的参数校验使用的结构体!
│   │   ├── files.go
│   │   ├── pagination.go
│   │   ├── post.go
│   │   └── user.go
│   └── router.go
├── setup								# 做一些环境的初始化
│   └── setup.go
├── sql									# sql语句
│   └── create_table.sql
├── static								# 本地存储的静态文件夹~ (动态的可有可无)  
├── test								# 测试代码!
│   ├── TestString2float_test.go
│   ├── customValidator_test.go
│   ├── testExif_test.go
│   └── testFile
│       └── test.jpg

项目分层

image-20230223111331856

配置文件模板

app:
  name: cld-quick-cli
  version: 1.0.0
  mode: debug # debug | release
  port: 8888
  machineID: 1
  onlineTime: "2023-02-23"
  staticBaseUrl: ./  # 前缀 指定是相对路径还是绝对路径 / 或者 ./
  staticFileDirPath: static/ # staticBaseUrl 下的 文件路径

  # staticBaseUrl+ staticFileDirPath = ./static/ 相对路径
  # /User/XX/DATA + static/ = /User/XX/DATA/static/ 绝对路径!
log:
  level: debug
  filename: ./logs/web.log
  err_filename: ./logs/error_web.log
  call_skip: 0
  max_size: 200
  max_age: 20
  max_backups: 7
  compress: true
  local_time: true
mysql:
  host: xxx
  port: xxx
  username: xxx
  password: xxxx
  db_name: cld
  charset: utf8mb4
  parse_time: true
  max_open_connes: 200 # 用于设置最大打开的连接数,默认值为0表示不限制。
  max_idle_connes: 50 # 用于设置闲置的连接数。
redis:
  host: xxxx
  port: xxxx
  db: 0
jwt:
  token_expire_duration: 72 #token过期时间,单位小时
  secret: "xx" # jwt 密钥
  issuer: "xxxx" # 签发人

关于router模块的设计

package router

import (
	"cld-quick-cli/config"
	"cld-quick-cli/router/modules"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

// CreateServer 做一些配置工作!
func CreateServer() *http.Server {
	// TODO 向gin里面注入自己的logger
	router := gin.Default()
	// TODO 一些路由服务初始化就在这里面!
	modules.TestRouter(router)
	server := &http.Server{
		Addr:    fmt.Sprintf(":%v", config.Config.App.Port),
		Handler: router,
	}
	return server
}

因为要做一些Server的自定义操作!目前有热关机的操作!所以实例化了一个 http.Server ,gin框架的router当做Server的handler!

CreateServer 返回个 Server 实例,在Starter的时候可以拿去做一些灵活的自定义操作!

路由注册,依然是用 gin 的 engin 来控制! 比如上面的 modules.TestRouter(router)

  • modules 存放业务路由初始化!

  • middlewares 存放中间件模块

package modules

import "github.com/gin-gonic/gin"

func TestRouter(router *gin.Engine) {
	r := router.Group("/test")
	r.GET("/hello_world", func(context *gin.Context) {
		context.JSON(200, gin.H{
			"message": "hello world",
		})
	})
}

一些需要解决的项目问题

  • 项目baseURL和前端静态资源路径一致的问题
MIT License Copyright (c) 2023 井24 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

一个开发Golang的快速脚手架项目! 展开 收起
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Go
1
https://gitee.com/changhao096256/cld-quick-cli.git
git@gitee.com:changhao096256/cld-quick-cli.git
changhao096256
cld-quick-cli
cld-quick-cli
master

搜索帮助