Skip to content

Commit a0f62b5

Browse files
committed
feat: add http rest service function, update README.md, add gin logic includes middlewares, add admin http rest service example
1 parent 9837c61 commit a0f62b5

File tree

21 files changed

+688
-20
lines changed

21 files changed

+688
-20
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ Microservice framework implemented based on Golang.
2828
│ └─user
2929
│ └─v1
3030
├─app 具体服务相关的实现
31-
│ ├─shop
32-
│ │ └─admin
33-
│ │ ├─config
34-
│ │ └─controller
31+
│ ├─shop 举例 shop http 服务 同级的还可以创建其他文件夹
32+
│ │ └─admin shop 下的 admin 侧 http 服务 同级的还可以创建其他文件夹
33+
│ │ ├─config 配置文件
34+
│ │ └─controller 表示层
3535
│ ├─pkg 服务共通的包
3636
│ │ ├─code 服务的错误码
3737
│ │ ├─options
3838
│ │ └─translator
3939
│ │ └─gin
40-
│ └─user 举例 user 服务
41-
│ ├─client 用于本地测试 user 服务的客户端 rpc 服务
40+
│ └─user 举例 user gRPC 服务,可以同时建立 http 服务,参考 shop
41+
│ ├─client 用于本地测试 user rpc 服务的客户端
4242
│ └─srv
4343
│ ├─config 服务的配置项,Log error 等子服务的相关逻辑全部注册到配置中
4444
│ ├─controller 表示层

app/shop/admin/app.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package admin
2+
3+
import (
4+
"github.com/CoderI421/gframework/app/pkg/options"
5+
"github.com/CoderI421/gframework/app/shop/admin/config"
6+
gapp "github.com/CoderI421/gframework/gmicro/app"
7+
"github.com/CoderI421/gframework/gmicro/registry"
8+
"github.com/CoderI421/gframework/gmicro/registry/consul"
9+
"github.com/CoderI421/gframework/pkg/app"
10+
"github.com/CoderI421/gframework/pkg/log"
11+
"github.com/hashicorp/consul/api"
12+
)
13+
14+
func NewApp(basename string) *app.App {
15+
cfg := config.New()
16+
appl := app.NewApp("admin",
17+
"shop",
18+
app.WithOptions(cfg),
19+
app.WithRunFunc(run(cfg)),
20+
//不读配置 使用命令行参数时使用
21+
//app.WithNoConfig(),
22+
)
23+
return appl
24+
}
25+
func NewRegistrar(registry *options.RegistryOptions) registry.Registrar {
26+
c := api.DefaultConfig()
27+
c.Address = registry.Address
28+
c.Scheme = registry.Scheme
29+
cli, err := api.NewClient(c)
30+
if err != nil {
31+
panic(err)
32+
}
33+
r := consul.New(cli, consul.WithHealthCheck(true))
34+
return r
35+
}
36+
func NewUserApp(cfg *config.Config) (*gapp.App, error) {
37+
//初始化log
38+
log.Init(cfg.Log)
39+
defer log.Flush()
40+
//服务注册
41+
register := NewRegistrar(cfg.Registry)
42+
//生成http服务
43+
restServer, err := NewUserHTTPServer(cfg)
44+
if err != nil {
45+
return nil, err
46+
}
47+
return gapp.New(
48+
gapp.WithName(cfg.Server.Name),
49+
gapp.WithRestServer(restServer),
50+
gapp.WithRegistrar(register),
51+
), nil
52+
53+
}
54+
55+
// controller(参数校验) ->service(具体的业务逻辑)->(数据库的接口)
56+
func run(cfg *config.Config) app.RunFunc {
57+
return func(baseName string) error {
58+
userApp, err := NewUserApp(cfg)
59+
if err != nil {
60+
return err
61+
}
62+
//启动
63+
if err := userApp.Run(); err != nil {
64+
log.Errorf("run user app error: %s", err)
65+
}
66+
return nil
67+
}
68+
}

app/shop/admin/config/config.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package config
2+
3+
import (
4+
"github.com/CoderI421/gframework/app/pkg/options"
5+
"github.com/CoderI421/gframework/pkg/app"
6+
cliflag "github.com/CoderI421/gframework/pkg/common/cli/flag"
7+
"github.com/CoderI421/gframework/pkg/log"
8+
)
9+
10+
var _ app.CliOptions = &Config{}
11+
12+
func New() *Config {
13+
return &Config{
14+
Log: log.NewOptions(),
15+
Server: options.NewServerOptions(),
16+
Registry: options.NewRegistryOptions(),
17+
}
18+
}
19+
20+
type Config struct {
21+
Log *log.Options `json:"log" mapstructure:"log"`
22+
// 服务发现
23+
Server *options.ServerOptions `json:"server" mapstructure:"server"`
24+
// 注册中心
25+
Registry *options.RegistryOptions `json:"registry" mapstructure:"registry"`
26+
}
27+
28+
// Flags implements app.CliOptions interface.Add flags to the specified FlagSet object.
29+
func (c *Config) Flags() (fss cliflag.NamedFlagSets) {
30+
// fss.FlagSet("logs") -> 创建一个FlagSet对象,命名为logs,做为专属的 logs 传递给 Config.Log
31+
c.Log.AddFlags(fss.FlagSet("logs"))
32+
c.Server.AddFlags(fss.FlagSet("server"))
33+
c.Registry.AddFlags(fss.FlagSet("registry"))
34+
return fss
35+
}
36+
37+
// Validate 将配置中的所有校验子逻辑 注册到当前实例的校验中
38+
func (c *Config) Validate() (errors []error) {
39+
// 将 Log 中的校验,注册到 user 服务的,校验逻辑中
40+
errors = append(errors, c.Log.Validate()...)
41+
errors = append(errors, c.Server.Validate()...)
42+
errors = append(errors, c.Registry.Validate()...)
43+
return
44+
}

app/shop/admin/controller/list.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package controller
2+
3+
import (
4+
"github.com/CoderI421/gframework/pkg/log"
5+
6+
"github.com/gin-gonic/gin"
7+
)
8+
9+
func (us *userServer) List(ctx *gin.Context) {
10+
log.Info("GetUserList is called")
11+
}

app/shop/admin/controller/user.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package controller
2+
3+
type userServer struct {
4+
}
5+
6+
func NewUserController() *userServer {
7+
return &userServer{}
8+
}

app/shop/admin/http.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package admin
2+
3+
import (
4+
"github.com/CoderI421/gframework/app/shop/admin/config"
5+
"github.com/CoderI421/gframework/gmicro/server/restserver"
6+
)
7+
8+
// NewUserHTTPServer 创建一个http server
9+
func NewUserHTTPServer(conf *config.Config) (*restserver.Server, error) {
10+
uRestServer := restserver.NewServer(
11+
restserver.WithPort(conf.Server.HttpPort),
12+
restserver.WithEnableProfiling(true),
13+
restserver.WithMiddlewares(conf.Server.Middlewares),
14+
)
15+
16+
// 配置好路由
17+
initRouter(uRestServer)
18+
return uRestServer, nil
19+
}

app/shop/admin/router.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package admin
2+
3+
import (
4+
"github.com/CoderI421/gframework/app/shop/admin/controller"
5+
"github.com/CoderI421/gframework/gmicro/server/restserver"
6+
)
7+
8+
func initRouter(g *restserver.Server) {
9+
v1 := g.Group("/v1")
10+
ugroup := v1.Group("/user")
11+
ucontroller := controller.NewUserController()
12+
ugroup.GET("list", ucontroller.List)
13+
}

app/user/srv/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (c *Config) Flags() (fss cliflag.NamedFlagSets) {
3636

3737
// Validate 将配置中的所有校验子逻辑 注册到当前实例的校验中
3838
func (c *Config) Validate() (errors []error) {
39-
// 将 Log 中的校验,注册到 user 服务的,校验逻辑中
39+
// 将 Log 中的校验,注册到 admin 服务的,校验逻辑中
4040
errors = append(errors, c.Log.Validate()...)
4141
errors = append(errors, c.Server.Validate()...)
4242
errors = append(errors, c.Registry.Validate()...)

cmd/shop/admin/admin.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"math/rand"
5+
"os"
6+
"runtime"
7+
"time"
8+
9+
"github.com/CoderI421/gframework/app/shop/admin"
10+
)
11+
12+
func main() {
13+
randSrc := rand.NewSource(time.Now().UnixNano())
14+
rand.New(randSrc)
15+
16+
if len(os.Getenv("GOMAXPROCS")) == 0 {
17+
runtime.GOMAXPROCS(runtime.NumCPU())
18+
}
19+
admin.NewApp("admin-server").Run()
20+
}

configs/admin/admin.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
log:
2+
name: shop-admin #logger的名称
3+
development: true # 是否开启开发模式,开发模式下会打印调试信息,默认 false
4+
level: debug # 日志级别,优先级从低到高依次为:debug, info, warn, error, dpanic, panic, fatal。
5+
format: console # 支持的日志输出格式,目前支持console和json两种。console其实就是text格式。
6+
enable-color: true # 是否开启颜色输出,true:是,false:否
7+
disable-caller: false # 是否开启 caller,如果开启会在日志中显示调用日志所在的文件、函数和行号
8+
disable-stacktrace: false # 是否再panic及以上级别禁止打印堆栈信息
9+
output-paths: logs/shop-admin.log,stdout # 支持输出到多个输出,逗号分开。支持输出到标准输出(stdout)和文件。
10+
error-output-paths: logs/shop-admin.error.log # zap内部(非业务)错误日志输出路径,多个输出,逗号分开
11+
12+
registry:
13+
address: 127.0.0.1:8500
14+
scheme: http
15+
16+
server:
17+
name: shop-admin #服务名,注册到consul使用的名称
18+
host: "127.0.0.1" #host, 默认使用 0.0.0.0
19+
port: 8016 # grpc 安全模式的端口号,默认 8081
20+
http-port: 8050
21+
healthz: true # 是否开启健康检查,如果开启会安装 /healthz 路由,默认 true
22+
enable-metrics: true # 开启 metrics, router: /metrics
23+
profiling: true # 开启性能分析, 可以通过 <host>:<port>/debug/pprof/地址查看程序栈、线程等系统信息,默认值为 true
24+
middlewares: # 中间件
25+
- recovery # panic 恢复
26+
- cors # 跨域

0 commit comments

Comments
 (0)