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: hex #129

Merged
merged 2 commits into from
Jan 5, 2024
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
35 changes: 35 additions & 0 deletions 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 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 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 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

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 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

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)
}
}
39 changes: 39 additions & 0 deletions hex/biz/dal/redis/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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

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 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 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 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 hex/biz/router/register.go

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

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

import (
"context"
"fmt"

example "cwgo/example/hex/kitex_gen/hello/example"

"github.com/cloudwego/kitex/pkg/klog"
)

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.
resp = new(example.HelloResp)
resp.RespBody = fmt.Sprintf("[KITEX] hello, %s", request.Name)
klog.Infof("[KITEX] hello, %s", request.Name)
return
}
Loading