Skip to content

Commit

Permalink
初始化项目
Browse files Browse the repository at this point in the history
  • Loading branch information
songcser committed Apr 6, 2023
0 parents commit 9b6b783
Show file tree
Hide file tree
Showing 56 changed files with 3,779 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
bin
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Gingo

##

Gingo是


54 changes: 54 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"github.com/fvbock/endless"
"github.com/gin-gonic/gin"
"github.com/songcser/gingo/config"
"github.com/songcser/gingo/initialize"
"go.uber.org/zap"
"time"
)

type server interface {
ListenAndServe() error
}

func RunWindowServer() {

Router := initialize.Routers()
address := ":8080"
s := initServer(address, Router)

if err := s.ListenAndServe(); err != nil {
}
}

func initServer(address string, router *gin.Engine) server {
s := endless.NewServer(address, router)
s.ReadHeaderTimeout = 20 * time.Second
s.WriteTimeout = 20 * time.Second
s.MaxHeaderBytes = 1 << 20
return s
}

func main() {
// 初始化配置
config.GVA_VP = initialize.Viper()

// 初始化日志
config.GVA_LOG = initialize.Zap()
zap.ReplaceGlobals(config.GVA_LOG)

config.GVA_DB = initialize.Gorm() // gorm连接数据库
if config.GVA_DB != nil {
initialize.RegisterTables(config.GVA_DB) // 初始化表
// 程序结束前关闭数据库链接
db, _ := config.GVA_DB.DB()
defer db.Close()
}

if config.GVA_JOB != nil {
defer config.GVA_JOB.Stop()
}
RunWindowServer()
}
17 changes: 17 additions & 0 deletions cmd/migrate/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"github.com/songcser/gingo/config"
"github.com/songcser/gingo/initialize"
)

func main() {
config.GVA_VP = initialize.Viper()
config.GVA_DB = initialize.Gorm() // gorm连接数据库
if config.GVA_DB != nil {
initialize.RegisterTables(config.GVA_DB) // 初始化表
// 程序结束前关闭数据库链接
db, _ := config.GVA_DB.DB()
defer db.Close()
}
}
27 changes: 27 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
domain: 'localhost'

mysql:
path: 'localhost'
port: '3306'
config: 'charset=utf8mb4&parseTime=True&loc=Local'
db-name: 'gingo'
username: 'root'
password: '123456'
prefix: "t_" # 全局表前缀,单独定义 TableName 则不生效
singular: false # 是否开启全局禁用复数,true表示不开启
engine: "" # 引擎,默认InnoDB
max-idle-conns: 10
max-open-conns: 100
log-mode: true
log-zap: false

zap: # 日志配置
level: info # 日志级别
prefix: '[east_white_common_admin/server]' # 日志前缀
format: console # 输出
director: log # 日志存放的文件
encode_level: LowercaseColorLevelEncoder # 编码级别
stacktrace_key: stacktrace # 栈名
max_age: 0 # 日志留存时间
show_line: true # 显示行
log_in_console: true # 输出控制台
9 changes: 9 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package config

type Configuration struct {
Domain string `mapstructure:"domain" json:"domain" yaml:"domain"`
DbType string `mapstructure:"dbType" json:"dbType" yaml:"dbType"`
Mysql Mysql `mapstructure:"mysql" json:"mysql" yaml:"mysql"`
Zap Zap `mapstructure:"zap" json:"zap" yaml:"zap"`
JWT JWT `mapstructure:"jwt" json:"jwt" yaml:"jwt"`
}
33 changes: 33 additions & 0 deletions config/global.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package config

import (
"context"
"github.com/robfig/cron/v3"
"github.com/spf13/viper"
"go.uber.org/zap"
"gorm.io/gorm"
)

type Context struct {
Ctx context.Context
Cancel context.CancelFunc
}

var (
GVA_CONFIG Configuration
GVA_DB *gorm.DB
GVA_LOG *zap.Logger
GVA_VP *viper.Viper

GVA_CTX *Context
GVA_JOB *cron.Cron
)

func NewContext() *Context {
ctx, cancel := context.WithCancel(context.Background())
GVA_CTX = &Context{
Ctx: ctx,
Cancel: cancel,
}
return GVA_CTX
}
8 changes: 8 additions & 0 deletions config/jwt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package config

type JWT struct {
SigningKey string `mapstructure:"signing-key" json:"signing-key" yaml:"signing-key"` // jwt签名
ExpiresTime int64 `mapstructure:"expires-time" json:"expires-time" yaml:"expires-time"` // 过期时间
BufferTime int64 `mapstructure:"buffer-time" json:"buffer-time" yaml:"buffer-time"` // 缓冲时间
Issuer string `mapstructure:"issuer" json:"issuer" yaml:"issuer"` // 签发者
}
29 changes: 29 additions & 0 deletions config/mysql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package config

type GeneralDB 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:"db-name" yaml:"db-name"` // 数据库名
Username string `mapstructure:"username" json:"username" yaml:"username"` // 数据库用户名
Password string `mapstructure:"password" json:"password" yaml:"password"` // 数据库密码
Prefix string `mapstructure:"prefix" json:"prefix" yaml:"prefix"` // 全局表前缀,单独定义TableName则不生效
Singular bool `mapstructure:"singular" json:"singular" yaml:"singular"` // 是否开启全局禁用复数,true表示开启
Engine string `mapstructure:"engine" json:"engine" yaml:"engine" default:"InnoDB"` // 数据库引擎,默认InnoDB
MaxIdleConns int `mapstructure:"max-idle-conns" json:"max-idle-conns" yaml:"max-idle-conns"` // 空闲中的最大连接数
MaxOpenConns int `mapstructure:"max-open-conns" json:"max-open-conns" yaml:"max-open-conns"` // 打开到数据库的最大连接数
LogMode string `mapstructure:"log-mode" json:"log-mode" yaml:"log-mode"` // 是否开启Gorm全局日志
LogZap bool `mapstructure:"log-zap" json:"log-zap" yaml:"log-zap"` // 是否通过zap写入日志文件
}

type Mysql struct {
GeneralDB `yaml:",inline" mapstructure:",squash"`
}

func (m *Mysql) Dsn() string {
return m.Username + ":" + m.Password + "@tcp(" + m.Path + ":" + m.Port + ")/" + m.Dbname + "?" + m.Config
}

func (m *Mysql) GetLogMode() string {
return m.LogMode
}
58 changes: 58 additions & 0 deletions config/zap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package config

import (
"go.uber.org/zap/zapcore"
"strings"
)

type Zap struct {
Level string `mapstructure:"level" json:"level" yaml:"level"` // 级别
Prefix string `mapstructure:"prefix" json:"prefix" yaml:"prefix"` // 日志前缀
Format string `mapstructure:"format" json:"format" yaml:"format"` // 输出
Director string `mapstructure:"director" json:"director" yaml:"director"` // 日志文件夹
EncodeLevel string `mapstructure:"encode_level" json:"encode_level" yaml:"encode_level"` // 编码级
StacktraceKey string `mapstructure:"stacktrace_key" json:"stacktrace_key" yaml:"stacktrace_key"` // 栈名

MaxAge int `mapstructure:"max_age" json:"max_age" yaml:"max_age"` // 日志留存时间
ShowLine bool `mapstructure:"show_line" json:"show_line" yaml:"show_line"` // 显示行
LogInConsole bool `mapstructure:"log_in_console" json:"log_in_console" yaml:"log_in_console"` // 输出控制台
}

// ZapEncodeLevel 根据 EncodeLevel 返回 zapcore.LevelEncoder
func (z *Zap) ZapEncodeLevel() zapcore.LevelEncoder {
switch {
case z.EncodeLevel == "LowercaseLevelEncoder": // 小写编码器(默认)
return zapcore.LowercaseLevelEncoder
case z.EncodeLevel == "LowercaseColorLevelEncoder": // 小写编码器带颜色
return zapcore.LowercaseColorLevelEncoder
case z.EncodeLevel == "CapitalLevelEncoder": // 大写编码器
return zapcore.CapitalLevelEncoder
case z.EncodeLevel == "CapitalColorLevelEncoder": // 大写编码器带颜色
return zapcore.CapitalColorLevelEncoder
default:
return zapcore.LowercaseLevelEncoder
}
}

// TransportLevel 根据字符串转化为 zapcore.Level
func (z *Zap) TransportLevel() zapcore.Level {
z.Level = strings.ToLower(z.Level)
switch z.Level {
case "debug":
return zapcore.DebugLevel
case "info":
return zapcore.InfoLevel
case "warn":
return zapcore.WarnLevel
case "error":
return zapcore.WarnLevel
case "dpanic":
return zapcore.DPanicLevel
case "panic":
return zapcore.PanicLevel
case "fatal":
return zapcore.FatalLevel
default:
return zapcore.DebugLevel
}
}
73 changes: 73 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
module github.com/songcser/gingo

go 1.18

require (
github.com/coocood/freecache v1.2.3
github.com/fsnotify/fsnotify v1.6.0
github.com/fvbock/endless v0.0.0-20170109170031-447134032cb6
github.com/gin-gonic/gin v1.8.1
github.com/go-openapi/spec v0.20.7
github.com/go-playground/locales v0.14.0
github.com/go-playground/universal-translator v0.18.0
github.com/go-playground/validator/v10 v10.11.1
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/google/go-querystring v1.1.0
github.com/gphper/multitemplate v0.1.0
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
github.com/pkg/errors v0.9.1
github.com/robfig/cron/v3 v3.0.1
github.com/satori/go.uuid v1.2.0
github.com/spf13/viper v1.14.0
github.com/swaggo/files v1.0.0
github.com/swaggo/gin-swagger v1.5.3
go.uber.org/zap v1.24.0
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e
golang.org/x/sync v0.1.0
gorm.io/driver/mysql v1.4.4
gorm.io/gorm v1.24.2
)

require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/swag v0.19.15 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/goccy/go-json v0.9.7 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/jonboulle/clockwork v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/lestrrat-go/strftime v1.0.6 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/spf13/afero v1.9.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
github.com/swaggo/swag v1.8.1 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/net v0.3.1-0.20221206200815-1e63c2f08a10 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect
golang.org/x/tools v0.1.12 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 9b6b783

Please sign in to comment.