Skip to content

Commit

Permalink
feat(gateway): add gateway gmctl
Browse files Browse the repository at this point in the history
  • Loading branch information
huerni committed Dec 21, 2023
1 parent f4faba9 commit 0429e76
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 152 deletions.
4 changes: 2 additions & 2 deletions gmctl/generator/gw/gengwcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"path/filepath"
)

//go:embed tpl/gw/etc/cfg.tpl
//go:embed tpl/gw/etc/properties.tpl
var gwCfgTemplate string

func (g *GwGenerator) GenGwCfg(ctx DirContext, gctx *GwContext) error {

fileName := filepath.Join(ctx.GetEtc().Filename, fmt.Sprintf("%v.toml", "cfg"))
fileName := filepath.Join(ctx.GetEtc().Filename, fmt.Sprintf("%v.yml", "properties"))

return util.With("gwCfg").GoFmt(false).Parse(gwCfgTemplate).SaveTo(map[string]any{
"project": ctx.GetProjectName(),
Expand Down
18 changes: 0 additions & 18 deletions gmctl/generator/gw/gengwserver.go

This file was deleted.

5 changes: 0 additions & 5 deletions gmctl/generator/gw/gwgenator.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,5 @@ func (g *GwGenerator) Generate(gctx *GwContext) error {
return err
}

err = g.GenGwServer(dirCtx, gctx)
if err != nil {
return err
}

return nil
}
10 changes: 5 additions & 5 deletions gmctl/generator/gw/tpl/gw/cmd/main.tpl
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package main

import (
"context"
"gateway/internal/app"
"gateway/internal/config"
"github.com/huerni/gmitex/core/discovery"
"github.com/huerni/gmitex/core/gateway"
)

func main() {
discovery.EnableDiscovery()
server := core.NewAPIGatewayServer()
server.Start()
discovery.EnableDiscovery(config.GetRegisterCenter())
server := core.NewAPIGatewayServer(config.GetServerConfig())
server.Start(config.GetGatewayRouter())
}
14 changes: 0 additions & 14 deletions gmctl/generator/gw/tpl/gw/docker-compose.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,3 @@ services:
- "2379:2379"
- "2380:2380"

traefik:
image: "traefik:latest"
restart: always
container_name: {{.project}}-traefik
command:
- "--api.insecure=true"
- "--providers.docker=false"
- "--providers.etcd=true"
- "--providers.etcd.endpoints={{.project}}-etcd:2379"
- "--entrypoints.web.address=:80"
ports:
- "8090:80"
- "9090:8080"

18 changes: 0 additions & 18 deletions gmctl/generator/gw/tpl/gw/etc/cfg.tpl

This file was deleted.

20 changes: 20 additions & 0 deletions gmctl/generator/gw/tpl/gw/etc/properties.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
server:
port: 8790
contextPath: /
timeout: 10
host:
registerCenter:
refreshFrequency: 30
# eureka:
# serviceUrls: [ http://localhost:8761 ]
etcd:
prefix: test
endpoints: ["127.0.0.1:2379"]

gateway:
routers:
gmitest:
path: /api/v1/gmitest/**
serviceId: gmitest
stripPrefix: false
timeout: 30
59 changes: 0 additions & 59 deletions gmctl/generator/gw/tpl/gw/internal/app/gwserver.tpl

This file was deleted.

58 changes: 27 additions & 31 deletions gmctl/generator/gw/tpl/gw/internal/config/config.tpl
Original file line number Diff line number Diff line change
@@ -1,48 +1,44 @@
package config

import (
"github.com/huerni/gmitex/pkg/config"
"github.com/zeromicro/go-zero/core/conf"
"github.com/huerni/gmitex/core/gateway/config"
"gopkg.in/yaml.v2"
"io/ioutil"

"github.com/huerni/gmitex/core/logger"
)

type Config struct {
Prefix string `json:"prefix"`
var appConfig = &AppConfig{}

Etcd config.EtcdConf `json:"etcd,option"`
Mysql config.MysqlConf `json:"mysql,option"`
}
type AppConfig struct {
// eureka 配置信息
RegisterCenter *config.RegisterCenter `yaml:"registerCenter"`
var (
Cfg *Config
)
// 路由配置信息
GatewayRouter *config.Routers `yaml:"gateway"`
func InitConfig(filePath string) (*Config, error) {
if Cfg == nil {
Cfg = &Config{}
conf.MustLoad(filePath, Cfg)
err := FigureConf(Cfg)
if err != nil {
return nil, err
}
}
// web 服务配置信息
Server *config.ServerConfig `yaml:"server"`
}

return Cfg, nil
func GetServerConfig() *config.ServerConfig {
return appConfig.Server
}

func GetConfig() *Config {
return Cfg
func GetGatewayRouter() *config.Routers {
return appConfig.GatewayRouter
}

func FigureConf(c *Config) error {
err := c.Etcd.FigureConfig()
if err != nil {
return err
}
func GetRegisterCenter() *config.RegisterCenter {
return appConfig.RegisterCenter
}

err = c.Mysql.FigureConfig()
func init() {
logger.Info("加载路由配置信息")
data, err := ioutil.ReadFile("etc/properties.yml")
if err != nil {
return err
logger.Error("从 conf/properties.yml 配置文件中加载配置信息失败")
}

return nil
yaml.Unmarshal(data, appConfig)
logger.Info("路由配置信息解析完成")
}

0 comments on commit 0429e76

Please sign in to comment.