Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:add kitex template #88

Merged
merged 8 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ header:

paths-ignore:
- '**/kitex_gen/**'
- 'kitex/**'

comment: on-failure
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ You can go into the related examples for information on "how to run"
- [etcd](https://github.com/kitex-contrib/config-etcd/tree/main/example) Kitex server uses etcd as an example of connecting the configuration center with governance features
- [nacos](https://github.com/kitex-contrib/config-nacos/tree/main/example) Kitex server uses nacos as an example of connecting the configuration center with governance features
- [apollo](https://github.com/kitex-contrib/config-apollo/tree/main/example) Kitex server uses apollo as an example of connecting the configuration center with governance features
- [discovery](discovery)Example of kitex server and client using service registration and discovery
- [discovery](discovery) Example of kitex server and client using service registration and discovery
- [etcd](https://github.com/kitex-contrib/registry-etcd/tree/main/example) Example of kitex server and client using etcd as service registration center
- [nacos](https://github.com/kitex-contrib/registry-nacos/tree/main/example) Kitex server and client use nacos as an example of service registration center
- [polaris](https://github.com/kitex-contrib/registry-polaris/tree/main/example) Kitex server and client use polaris as an example of service registration center
Expand Down Expand Up @@ -59,6 +59,7 @@ You can go into the related examples for information on "how to run"

## Kitex generated code
- [protobuf](kitex/protobuf) Example of using kitex and protobuf to generate server code
- [template](kitex/template) Example of using kitex custom template to generate server code

## Note

Expand Down
3 changes: 2 additions & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
- [etcd](https://github.com/kitex-contrib/config-etcd/tree/main/example) kitex 服务端使用 etcd 作为配置中心对接治理特性的示例
- [nacos](https://github.com/kitex-contrib/config-nacos/tree/main/example) kitex 服务端使用 nacos 作为配置中心对接治理特性的示例
- [apollo](https://github.com/kitex-contrib/config-apollo/tree/main/example) kitex 服务端使用 apollo 作为配置中心对接治理特性的示例
- [discovery](discovery)kitex 服务端和客户端使用服务注册与发现的示例
- [discovery](discovery) kitex 服务端和客户端使用服务注册与发现的示例
- [etcd](https://github.com/kitex-contrib/registry-etcd/tree/main/example) kitex 服务端和客户端使用 etcd 作为服务注册中心的示例
- [nacos](https://github.com/kitex-contrib/registry-nacos/tree/main/example) kitex 服务端和客户端使用 nacos 作为服务注册中心的示例
- [polaris](https://github.com/kitex-contrib/registry-polaris/tree/main/example) kitex 服务端和客户端使用 polaris 作为服务注册中心的示例
Expand Down Expand Up @@ -59,6 +59,7 @@

## Kitex 生成代码
- [protobuf](kitex/protobuf) 使用 kitex 与 protobuf 生成服务端代码的示例
- [template](kitex/template) 使用 kitex 自定义模版生成服务端代码的示例
li-jin-gou marked this conversation as resolved.
Show resolved Hide resolved

## Note

Expand Down
35 changes: 35 additions & 0 deletions kitex/template/.gitignore
Original file line number Diff line number Diff line change
@@ -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 changes: 11 additions & 0 deletions kitex/template/biz/dal/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dal

import (
"github.com/kitex/hello/biz/dal/mysql"
"github.com/kitex/hello/biz/dal/redis"
)

func Init() {
redis.Init()
mysql.Init()
}
25 changes: 25 additions & 0 deletions kitex/template/biz/dal/mysql/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package mysql

import (
"github.com/kitex/hello/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 changes: 24 additions & 0 deletions kitex/template/biz/dal/redis/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package redis

import (
"context"

"github.com/kitex/hello/conf"
"github.com/redis/go-redis/v9"
)

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 changes: 20 additions & 0 deletions kitex/template/biz/service/hello_method.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package service

import (
"context"
example "github.com/kitex/hello/kitex_gen/hello/example"
)

type HelloMethodService struct {
ctx context.Context
} // NewHelloMethodService new HelloMethodService
func NewHelloMethodService(ctx context.Context) *HelloMethodService {
return &HelloMethodService{ctx: ctx}
}

// Run create note info
func (s *HelloMethodService) Run(request *example.HelloReq) (resp *example.HelloResp, err error) {
// Finish your business logic.

return
}
21 changes: 21 additions & 0 deletions kitex/template/biz/service/hello_method_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package service

import (
"context"
example "github.com/kitex/hello/kitex_gen/hello/example"
"testing"
)

func TestHelloMethod_Run(t *testing.T) {
ctx := context.Background()
s := NewHelloMethodService(ctx)
// init req and assert value

request := &example.HelloReq{}
resp, err := s.Run(request)
t.Logf("err: %v", err)
t.Logf("resp: %v", resp)

// todo: edit your unit test

}
7 changes: 7 additions & 0 deletions kitex/template/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash
RUN_NAME="hello"
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}
113 changes: 113 additions & 0 deletions kitex/template/conf/conf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
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"`
EnablePprof bool `yaml:"enable_pprof"`
EnableGzip bool `yaml:"enable_gzip"`
EnableAccessLog bool `yaml:"enable_access_log"`
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 changes: 23 additions & 0 deletions kitex/template/conf/dev/conf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
kitex:
service: "hello"
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 changes: 23 additions & 0 deletions kitex/template/conf/online/conf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
kitex:
service: "hello"
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 changes: 23 additions & 0 deletions kitex/template/conf/test/conf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
kitex:
service: "hello"
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 changes: 15 additions & 0 deletions kitex/template/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -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
Loading
Loading