Skip to content

Commit

Permalink
feat: add hertz_swagger_gen example (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
EZ4Jam1n authored Sep 25, 2024
1 parent 231bdf8 commit d1e3643
Show file tree
Hide file tree
Showing 31 changed files with 29,096 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ You can enter the example for information about "How to run"
- [bizdemo/hertz_gorm_gen:](bizdemo/hertz_gorm_gen) Example of using gorm/gen & proto IDL in hertz server
- [bizdemo/hertz_jwt:](bizdemo/hertz_jwt) Example of using jwt in hertz server
- [bizdemo/hertz_session:](bizdemo/hertz_session) Example of using distributed session and csrf in hertz server
- [bizdemo/hertz_swagger_gen:](bizdemo/hertz_swagger_gen) Example of using plugin to generate swagger service in hertz server
- [bizdemo/tiktok_demo:](bizdemo/tiktok_demo) Example of simple tiktok in hertz server
- [formulago:](https://github.com/chenghonour/formulago) Production-level backend management system framework implemented using hertz and ent
- [gpress:](https://github.com/springrain/gpress) Production-grade cloud-native high-performance content platform using hertz and zorm
Expand Down
1 change: 1 addition & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- [hertz_gorm_gen:](bizdemo/hertz_gorm_gen) 在 hertz server 中使用 gorm/gen & proto IDL 的示例
- [hertz_jwt:](bizdemo/hertz_jwt) 在 hertz server 中使用 jwt 的示例
- [hertz_session:](bizdemo/hertz_session) 在 hertz server 中使用分布式 session 和 csrf 的示例
- [hertz_swagger_gen:](bizdemo/hertz_swagger_gen) 在 hertz server 中使用插件生成 swagger 服务的示例
- [tiktok_demo:](bizdemo/tiktok_demo) 在 hertz server 中实现极简版抖音服务端的示例
- [formulago:](https://github.com/chenghonour/formulago) 使用 hertz 与 ent 实现的生产级别后台管理系统框架
- [gpress:](https://github.com/springrain/gpress) 使用 hertz 与 zorm 实现的生产级别云原生高性能内容平台
Expand Down
37 changes: 37 additions & 0 deletions bizdemo/hertz_swagger_gen/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
*.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
dumped_hertz_remote_config.json

6 changes: 6 additions & 0 deletions bizdemo/hertz_swagger_gen/.hz
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Code generated by hz. DO NOT EDIT.

hz version: v0.9.1
handlerDir: ""
modelDir: biz/hertz_gen
routerDir: ""
6 changes: 6 additions & 0 deletions bizdemo/hertz_swagger_gen/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
init_user:
hz new --model_dir biz/hertz_gen -mod github.com/cloudwego/hertz-examples/bizdemo/hertz_swagger_gen -thrift-plugins=http-swagger -idl idl/user.thrift


update_user:
hz update --model_dir biz/hertz_gen -thrift-plugins=http-swagger -idl idl/user.thrift
23 changes: 23 additions & 0 deletions bizdemo/hertz_swagger_gen/biz/dal/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2024 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 "github.com/cloudwego/hertz-examples/bizdemo/hertz_swagger_gen/biz/dal/mysql"

func Init() {
mysql.Init()
}
39 changes: 39 additions & 0 deletions bizdemo/hertz_swagger_gen/biz/dal/mysql/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2024 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 (
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)

var dsn = "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True&loc=Local"

var DB *gorm.DB

func Init() {
var err error
DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
SkipDefaultTransaction: true,
PrepareStmt: true,
Logger: logger.Default.LogMode(logger.Info),
})
if err != nil {
panic(err)
}
}
48 changes: 48 additions & 0 deletions bizdemo/hertz_swagger_gen/biz/dal/mysql/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2024 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 "github.com/cloudwego/hertz-examples/bizdemo/hertz_swagger_gen/biz/model"

func CreateUser(users []*model.User) error {
return DB.Create(users).Error
}

func DeleteUser(userId int64) error {
return DB.Where("id = ?", userId).Delete(&model.User{}).Error
}

func UpdateUser(user *model.User) error {
return DB.Updates(user).Error
}

func QueryUser(keyword *string, page, pageSize int64) ([]*model.User, int64, error) {
db := DB.Model(model.User{})
if keyword != nil && len(*keyword) != 0 {
db = db.Where(DB.Or("name like ?", "%"+*keyword+"%").
Or("introduce like ?", "%"+*keyword+"%"))
}
var total int64
if err := db.Count(&total).Error; err != nil {
return nil, 0, err
}
var res []*model.User
if err := db.Limit(int(pageSize)).Offset(int(pageSize * (page - 1))).Find(&res).Error; err != nil {
return nil, 0, err
}
return res, total, nil
}
18 changes: 18 additions & 0 deletions bizdemo/hertz_swagger_gen/biz/handler/ping.go

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

104 changes: 104 additions & 0 deletions bizdemo/hertz_swagger_gen/biz/handler/user/user_service.go

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

Loading

0 comments on commit d1e3643

Please sign in to comment.