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: cwgo hex #79

Merged
merged 8 commits into from
Aug 23, 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
3 changes: 3 additions & 0 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ header:
- '**/*.go'
- '**/*.s'

paths-ignore:
- "example/**"

comment: on-failure
5 changes: 5 additions & 0 deletions _typos.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@

[files]
extend-exclude = ["go.sum", "check_branch_name.sh"]

[default.extend-identifiers]
# *sigh* this just isn't worth the cost of fixing
O_WRONLY = "O_WRONLY"
WRONLY = "WRONLY"
9 changes: 5 additions & 4 deletions cmd/static/server_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ func serverFlags() []cli.Flag {
&cli.StringFlag{Name: config.ServiceType, Usage: "Specify the generate type. (RPC or HTTP)", Value: config.RPC},
&cli.StringFlag{Name: config.Module, Aliases: []string{"mod"}, Usage: "Specify the Go module name to generate go.mod.", Destination: &globalArgs.ServerArgument.GoMod},
&cli.StringFlag{Name: config.IDLPath, Usage: "Specify the IDL file path. (.thrift or .proto)", Destination: &globalArgs.ServerArgument.IdlPath},
&cli.StringFlag{Name: config.OutDir, Value: ".", Aliases: []string{"o"}, Usage: "Specify the output path. Currently cwgo supports git templates, such as `--template https://github.com/***/cwgo_template.git`", Destination: &globalArgs.ServerArgument.OutDir},
&cli.StringFlag{Name: config.OutDir, Value: ".", Aliases: []string{"o"}, Usage: "Specify the output path. Currently cwgo supports git templates, such as `--template https://github.com/***/cwgo_template.git`.", Destination: &globalArgs.ServerArgument.OutDir},
&cli.StringFlag{Name: config.Template, Usage: "Specify the layout template.", Destination: &globalArgs.ServerArgument.Template},
&cli.StringFlag{Name: config.Registry, Usage: "Specify the registry, default is None"},
&cli.StringSliceFlag{Name: config.ProtoSearchPath, Aliases: []string{"I"}, Usage: "Add an IDL search path for includes. (Valid only if idl is protobuf)"},
&cli.StringSliceFlag{Name: config.Pass, Usage: "pass param to hz or kitex"},
&cli.StringFlag{Name: config.Registry, Usage: "Specify the registry, default is None."},
&cli.StringSliceFlag{Name: config.ProtoSearchPath, Aliases: []string{"I"}, Usage: "Add an IDL search path for includes."},
&cli.StringSliceFlag{Name: config.Pass, Usage: "Pass param to hz or Kitex."},
&cli.BoolFlag{Name: config.Verbose, Usage: "Turn on verbose mode."},
&cli.BoolFlag{Name: config.HexTag, Usage: "Add HTTP listen for Kitex.", Destination: &globalArgs.Hex},
welkeyever marked this conversation as resolved.
Show resolved Hide resolved
}
}
1 change: 1 addition & 0 deletions config/argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,5 @@ const (
Signable = "signable"
IndexTag = "index_tag"
TypeTag = "type_tag"
HexTag = "hex"
)
1 change: 1 addition & 0 deletions config/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type ServerArgument struct {
Template string
SliceParam *SliceParam
Verbose bool
Hex bool // add http listen for kitex

Cwd string
GoSrc string
Expand Down
35 changes: 35 additions & 0 deletions example/hex/.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
6 changes: 6 additions & 0 deletions example/hex/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
mod_init:
go mod init cwgo/example/hex
hex:
cwgo server --type RPC --idl idl/hello.thrift --service p.s.m --hex
mod_tidy:
go mod tidy
24 changes: 24 additions & 0 deletions example/hex/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# CWGO Hex Usage

## Introduce
The main power of `cwgo hex` is to allow hertz and kitex to listen on the same port and use protocol sniffing to distribute requests to kitex and hertz for processing
## Install
```
# Go 1.15 and earlier version
GO111MODULE=on GOPROXY=https://goproxy.cn/,direct go get github.com/cloudwego/cwgo@latest

# Go 1.16 and later version
GOPROXY=https://goproxy.cn/,direct go install github.com/cloudwego/cwgo@latest
```
## Usage
- init go.mod `go mod init cwgo/example/hex`
- generate code `cwgo server --type RPC --idl idl/hello.thrift --service p.s.m --hex`
- mod tidy `go mod tidy`

## Test
- `cd /cwgo/example/hex`
- start server: `go run .`
- test rpc: `go run client/main.go`
- `HelloResp({RespBody:[KITEX] hello, hex})`
- test http: `curl 127.0.0.1:8888/hello?name=hex`
- `{"RespBody":"[HERTZ] hello, hex"}`
27 changes: 27 additions & 0 deletions example/hex/biz/dal/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2023 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dal
FGYFFFF marked this conversation as resolved.
Show resolved Hide resolved

import (
"cwgo/example/hex/biz/dal/mysql"
"cwgo/example/hex/biz/dal/redis"
)

func Init() {
redis.Init()
mysql.Init()
}
41 changes: 41 additions & 0 deletions example/hex/biz/dal/mysql/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2023 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package mysql
FGYFFFF marked this conversation as resolved.
Show resolved Hide resolved

import (
"cwgo/example/hex/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)
}
}
40 changes: 40 additions & 0 deletions example/hex/biz/dal/redis/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2023 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package redis
FGYFFFF marked this conversation as resolved.
Show resolved Hide resolved

import (
"context"

"cwgo/example/hex/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)
}
}
45 changes: 45 additions & 0 deletions example/hex/biz/handler/hello/example/hello_service.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions example/hex/biz/router/hello/example/hello.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions example/hex/biz/router/hello/example/middleware.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions example/hex/biz/router/register.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading