This commit is contained in:
2025-01-17 17:09:32 +08:00
commit d696206dc7
40 changed files with 2357 additions and 0 deletions
+35
View File
@@ -0,0 +1,35 @@
*.o
*.a
*.so
_obj
_test
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
*.exe
*.exe~
*.test
*.prof
*.rar
*.zip
*.gz
*.psd
*.bmd
*.cfg
*.pptx
*.log
*nohup.out
*settings.pyc
*.sublime-project
*.sublime-workspace
!.gitkeep
.DS_Store
/.idea
/.vscode
/output
*.local.yml
+11
View File
@@ -0,0 +1,11 @@
package dal
import (
"github.com/suyiiyii/hertz101/app/user/biz/dal/mysql"
"github.com/suyiiyii/hertz101/app/user/biz/dal/redis"
)
func Init() {
redis.Init()
mysql.Init()
}
+25
View File
@@ -0,0 +1,25 @@
package mysql
import (
"github.com/suyiiyii/hertz101/app/user/conf"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
var (
DB *gorm.DB
err error
)
func Init() {
DB, err = gorm.Open(mysql.Open(conf.GetConf().MySQL.DSN),
&gorm.Config{
PrepareStmt: true,
SkipDefaultTransaction: true,
},
)
if err != nil {
panic(err)
}
}
+24
View File
@@ -0,0 +1,24 @@
package redis
import (
"context"
"github.com/redis/go-redis/v9"
"github.com/suyiiyii/hertz101/app/user/conf"
)
var (
RedisClient *redis.Client
)
func Init() {
RedisClient = redis.NewClient(&redis.Options{
Addr: conf.GetConf().Redis.Address,
Username: conf.GetConf().Redis.Username,
Password: conf.GetConf().Redis.Password,
DB: conf.GetConf().Redis.DB,
})
if err := RedisClient.Ping(context.Background()).Err(); err != nil {
panic(err)
}
}
+20
View File
@@ -0,0 +1,20 @@
package service
import (
"context"
user "github.com/suyiiyii/hertz101/rpc_gen/kitex_gen/user"
)
type LoginService struct {
ctx context.Context
} // NewLoginService new LoginService
func NewLoginService(ctx context.Context) *LoginService {
return &LoginService{ctx: ctx}
}
// Run create note info
func (s *LoginService) Run(req *user.LoginReq) (resp *user.LoginResp, err error) {
// Finish your business logic.
return
}
+21
View File
@@ -0,0 +1,21 @@
package service
import (
"context"
"testing"
user "github.com/suyiiyii/hertz101/rpc_gen/kitex_gen/user"
)
func TestLogin_Run(t *testing.T) {
ctx := context.Background()
s := NewLoginService(ctx)
// init req and assert value
req := &user.LoginReq{}
resp, err := s.Run(req)
t.Logf("err: %v", err)
t.Logf("resp: %v", resp)
// todo: edit your unit test
}
+20
View File
@@ -0,0 +1,20 @@
package service
import (
"context"
user "github.com/suyiiyii/hertz101/rpc_gen/kitex_gen/user"
)
type RegisterService struct {
ctx context.Context
} // NewRegisterService new RegisterService
func NewRegisterService(ctx context.Context) *RegisterService {
return &RegisterService{ctx: ctx}
}
// Run create note info
func (s *RegisterService) Run(req *user.RegisterResp) (resp *user.RegisterResp, err error) {
// Finish your business logic.
return
}
+21
View File
@@ -0,0 +1,21 @@
package service
import (
"context"
"testing"
user "github.com/suyiiyii/hertz101/rpc_gen/kitex_gen/user"
)
func TestRegister_Run(t *testing.T) {
ctx := context.Background()
s := NewRegisterService(ctx)
// init req and assert value
req := &user.RegisterResp{}
resp, err := s.Run(req)
t.Logf("err: %v", err)
t.Logf("resp: %v", resp)
// todo: edit your unit test
}
+7
View File
@@ -0,0 +1,7 @@
#!/usr/bin/env bash
RUN_NAME="user"
mkdir -p output/bin output/conf
cp script/* output/
cp -r conf/* output/conf
chmod +x output/bootstrap.sh
go build -o output/bin/${RUN_NAME}
+110
View File
@@ -0,0 +1,110 @@
package conf
import (
"io/ioutil"
"os"
"path/filepath"
"sync"
"github.com/cloudwego/kitex/pkg/klog"
"github.com/kr/pretty"
"gopkg.in/validator.v2"
"gopkg.in/yaml.v2"
)
var (
conf *Config
once sync.Once
)
type Config struct {
Env string
Kitex Kitex `yaml:"kitex"`
MySQL MySQL `yaml:"mysql"`
Redis Redis `yaml:"redis"`
Registry Registry `yaml:"registry"`
}
type MySQL struct {
DSN string `yaml:"dsn"`
}
type Redis struct {
Address string `yaml:"address"`
Username string `yaml:"username"`
Password string `yaml:"password"`
DB int `yaml:"db"`
}
type Kitex struct {
Service string `yaml:"service"`
Address string `yaml:"address"`
LogLevel string `yaml:"log_level"`
LogFileName string `yaml:"log_file_name"`
LogMaxSize int `yaml:"log_max_size"`
LogMaxBackups int `yaml:"log_max_backups"`
LogMaxAge int `yaml:"log_max_age"`
}
type Registry struct {
RegistryAddress []string `yaml:"registry_address"`
Username string `yaml:"username"`
Password string `yaml:"password"`
}
// GetConf gets configuration instance
func GetConf() *Config {
once.Do(initConf)
return conf
}
func initConf() {
prefix := "conf"
confFileRelPath := filepath.Join(prefix, filepath.Join(GetEnv(), "conf.yaml"))
content, err := ioutil.ReadFile(confFileRelPath)
if err != nil {
panic(err)
}
conf = new(Config)
err = yaml.Unmarshal(content, conf)
if err != nil {
klog.Error("parse yaml error - %v", err)
panic(err)
}
if err := validator.Validate(conf); err != nil {
klog.Error("validate config error - %v", err)
panic(err)
}
conf.Env = GetEnv()
pretty.Printf("%+v\n", conf)
}
func GetEnv() string {
e := os.Getenv("GO_ENV")
if len(e) == 0 {
return "test"
}
return e
}
func LogLevel() klog.Level {
level := GetConf().Kitex.LogLevel
switch level {
case "trace":
return klog.LevelTrace
case "debug":
return klog.LevelDebug
case "info":
return klog.LevelInfo
case "notice":
return klog.LevelNotice
case "warn":
return klog.LevelWarn
case "error":
return klog.LevelError
case "fatal":
return klog.LevelFatal
default:
return klog.LevelInfo
}
}
+23
View File
@@ -0,0 +1,23 @@
kitex:
service: "user"
address: ":8888"
log_level: info
log_file_name: "log/kitex.log"
log_max_size: 10
log_max_age: 3
log_max_backups: 50
registry:
registry_address:
- 127.0.0.1:2379
username: ""
password: ""
mysql:
dsn: "gorm:gorm@tcp(127.0.0.1:3306)/gorm?charset=utf8mb4&parseTime=True&loc=Local"
redis:
address: "127.0.0.1:6379"
username: ""
password: ""
db: 0
+23
View File
@@ -0,0 +1,23 @@
kitex:
service: "user"
address: ":8888"
log_level: info
log_file_name: "log/kitex.log"
log_max_size: 10
log_max_age: 3
log_max_backups: 50
registry:
registry_address:
- 127.0.0.1:2379
username: ""
password: ""
mysql:
dsn: "gorm:gorm@tcp(127.0.0.1:3306)/gorm?charset=utf8mb4&parseTime=True&loc=Local"
redis:
address: "127.0.0.1:6379"
username: ""
password: ""
db: 0
+23
View File
@@ -0,0 +1,23 @@
kitex:
service: "user"
address: ":8888"
log_level: info
log_file_name: "log/kitex.log"
log_max_size: 10
log_max_age: 3
log_max_backups: 50
registry:
registry_address:
- 127.0.0.1:2379
username: ""
password: ""
mysql:
dsn: "gorm:gorm@tcp(127.0.0.1:3306)/gorm?charset=utf8mb4&parseTime=True&loc=Local"
redis:
address: "127.0.0.1:6379"
username: ""
password: ""
db: 0
+15
View File
@@ -0,0 +1,15 @@
version: '3'
services:
mysql:
image: 'mysql:latest'
ports:
- 3306:3306
environment:
- MYSQL_DATABASE=gorm
- MYSQL_USER=gorm
- MYSQL_PASSWORD=gorm
- MYSQL_RANDOM_ROOT_PASSWORD="yes"
redis:
image: 'redis:latest'
ports:
- 6379:6379
+54
View File
@@ -0,0 +1,54 @@
module github.com/suyiiyii/hertz101/app/user
go 1.23.4
replace github.com/apache/thrift => github.com/apache/thrift v0.13.0
require (
github.com/cloudwego/kitex v0.12.1
github.com/kr/pretty v0.1.0
gopkg.in/yaml.v2 v2.2.2
)
require (
github.com/bytedance/gopkg v0.1.1 // indirect
github.com/bytedance/sonic v1.12.5 // indirect
github.com/bytedance/sonic/loader v0.2.0 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/configmanager v0.2.2 // indirect
github.com/cloudwego/dynamicgo v0.4.7-0.20241220085612-55704ea4ca8f // indirect
github.com/cloudwego/fastpb v0.0.5 // indirect
github.com/cloudwego/frugal v0.2.3 // indirect
github.com/cloudwego/gopkg v0.1.3 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/cloudwego/localsession v0.1.1 // indirect
github.com/cloudwego/netpoll v0.6.5 // indirect
github.com/cloudwego/runtimex v0.1.0 // indirect
github.com/cloudwego/thriftgo v0.3.18 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fatih/structtag v1.2.0 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8 // indirect
github.com/iancoleman/strcase v0.2.0 // indirect
github.com/jhump/protoreflect v1.8.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/gls v0.0.0-20220109145502-612d0167dce5 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/tidwall/gjson v1.17.3 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
golang.org/x/arch v0.2.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto v0.0.0-20210513213006-bf773b8c8384 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
+50
View File
@@ -0,0 +1,50 @@
github.com/bytedance/gopkg v0.1.1 h1:3azzgSkiaw79u24a+w9arfH8OfnQQ4MHUt9lJFREEaE=
github.com/bytedance/sonic v1.12.5 h1:hoZxY8uW+mT+OpkcUWw4k0fDINtOcVavEsGfzwzFU/w=
github.com/bytedance/sonic/loader v0.2.0 h1:zNprn+lsIP06C/IqCHs3gPQIvnvpKbbxyXQP1iU4kWM=
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
github.com/cloudwego/configmanager v0.2.2 h1:sVrJB8gWYTlPV2OS3wcgJSO9F2/9Zbkmcm1Z7jempOU=
github.com/cloudwego/dynamicgo v0.4.7-0.20241220085612-55704ea4ca8f h1:IERXjxDg3Pbatb5z/dR8Qr8XUA1FpDVa73BnwbeQ76U=
github.com/cloudwego/fastpb v0.0.5 h1:vYnBPsfbAtU5TVz5+f9UTlmSCixG9F9vRwaqE0mZPZU=
github.com/cloudwego/frugal v0.2.3 h1:t1hhhAi8lXcx7Ncs4PR1pSZ90vlDU1cy5K2btDMFpoA=
github.com/cloudwego/gopkg v0.1.3 h1:y9VA5Zn5yqd1+QBV9aB0Zxy56JlAS7x4ZUoED/vJdxA=
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
github.com/cloudwego/kitex v0.12.1 h1:Ib+RWDRzXNz/9mObZmWasjFgrIVNrLSyvMveFDA2tTM=
github.com/cloudwego/localsession v0.1.1 h1:tbK7laDVrYfFDXoBXo4uCGMAxU4qmz2dDm8d4BGBnDo=
github.com/cloudwego/netpoll v0.6.5 h1:6E/BWhSzQoyLg9Kx/4xiMdIIpovzwBtXvuqSqaTUzDQ=
github.com/cloudwego/runtimex v0.1.0 h1:HG+WxWoj5/CDChDZ7D99ROwvSMkuNXAqt6hnhTTZDiI=
github.com/cloudwego/thriftgo v0.3.18 h1:gnr1vz7G3RbwwCK9AMKHZf63VYGa7ene6WbI9VrBJSw=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8 h1:FKHo8hFI3A+7w0aUQuYXQ+6EN5stWmeY/AZqtM8xk9k=
github.com/iancoleman/strcase v0.2.0 h1:05I4QRnGpI0m37iZQRuskXh+w77mr6Z41lwQzuHLwW0=
github.com/jhump/protoreflect v1.8.2 h1:k2xE7wcUomeqwY0LDCYA16y4WWfyTcMx5mKhk0d4ua0=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/gls v0.0.0-20220109145502-612d0167dce5 h1:uiS4zKYKJVj5F3ID+5iylfKPsEQmBEOucSD9Vgmn0i0=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/tidwall/gjson v1.17.3 h1:bwWLZU7icoKRG+C+0PNwIKC6FCJO/Q3p2pZvuP0jN94=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
golang.org/x/arch v0.2.0 h1:W1sUEHXiJTfjaFJ5SLo0N6lZn+0eO5gWD1MFeTGqQEY=
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
google.golang.org/genproto v0.0.0-20210513213006-bf773b8c8384 h1:z+j74wi4yV+P7EtK9gPLGukOk7mFOy9wMQaC0wNb7eY=
google.golang.org/grpc v1.36.1 h1:cmUfbeGKnz9+2DD/UYsMQXeqbHZqZDs4eQwW0sFOpBY=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
+24
View File
@@ -0,0 +1,24 @@
package main
import (
"context"
user "github.com/suyiiyii/hertz101/rpc_gen/kitex_gen/user"
"github.com/suyiiyii/hertz101/app/user/biz/service"
)
// UserServiceImpl implements the last service interface defined in the IDL.
type UserServiceImpl struct{}
// Register implements the UserServiceImpl interface.
func (s *UserServiceImpl) Register(ctx context.Context, req *user.RegisterResp) (resp *user.RegisterResp, err error) {
resp, err = service.NewRegisterService(ctx).Run(req)
return resp, err
}
// Login implements the UserServiceImpl interface.
func (s *UserServiceImpl) Login(ctx context.Context, req *user.LoginReq) (resp *user.LoginResp, err error) {
resp, err = service.NewLoginService(ctx).Run(req)
return resp, err
}
+3
View File
@@ -0,0 +1,3 @@
kitexinfo:
ServiceName: 'user'
ToolVersion: 'v0.9.1'
+59
View File
@@ -0,0 +1,59 @@
package main
import (
"net"
"time"
"github.com/cloudwego/kitex/pkg/klog"
"github.com/cloudwego/kitex/pkg/rpcinfo"
"github.com/cloudwego/kitex/server"
kitexlogrus "github.com/kitex-contrib/obs-opentelemetry/logging/logrus"
"github.com/suyiiyii/hertz101/app/user/conf"
"github.com/suyiiyii/hertz101/rpc_gen/kitex_gen/user/userservice"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)
func main() {
opts := kitexInit()
svr := userservice.NewServer(new(UserServiceImpl), opts...)
err := svr.Run()
if err != nil {
klog.Error(err.Error())
}
}
func kitexInit() (opts []server.Option) {
// address
addr, err := net.ResolveTCPAddr("tcp", conf.GetConf().Kitex.Address)
if err != nil {
panic(err)
}
opts = append(opts, server.WithServiceAddr(addr))
// service info
opts = append(opts, server.WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{
ServiceName: conf.GetConf().Kitex.Service,
}))
// klog
logger := kitexlogrus.NewLogger()
klog.SetLogger(logger)
klog.SetLevel(conf.LogLevel())
asyncWriter := &zapcore.BufferedWriteSyncer{
WS: zapcore.AddSync(&lumberjack.Logger{
Filename: conf.GetConf().Kitex.LogFileName,
MaxSize: conf.GetConf().Kitex.LogMaxSize,
MaxBackups: conf.GetConf().Kitex.LogMaxBackups,
MaxAge: conf.GetConf().Kitex.LogMaxAge,
}),
FlushInterval: time.Minute,
}
klog.SetOutput(asyncWriter)
server.RegisterShutdownHook(func() {
asyncWriter.Sync()
})
return
}
+26
View File
@@ -0,0 +1,26 @@
# *** Project
## introduce
- Use the [Kitex](https://github.com/cloudwego/kitex/) framework
- Generating the base code for unit tests.
- Provides basic config functions
- Provides the most basic MVC code hierarchy.
## Directory structure
| catalog | introduce |
| ---- | ---- |
| conf | Configuration files |
| main.go | Startup file |
| handler.go | Used for request processing return of response. |
| kitex_gen | kitex generated code |
| biz/service | The actual business logic. |
| biz/dal | Logic for operating the storage layer |
## How to run
```shell
sh build.sh
sh output/bootstrap.sh
```
+4
View File
@@ -0,0 +1,4 @@
#! /usr/bin/env bash
CURDIR=$(cd $(dirname $0); pwd)
echo "$CURDIR/bin/user"
exec "$CURDIR/bin/user"