server.go 4.4 KB
Newer Older
Y
Your Name 已提交
1 2 3 4
package server

import (
	"errors"
Y
Your Name 已提交
5 6 7 8
	"fmt"
	"net/http"
	"runtime/debug"

Y
Your Name 已提交
9
	//"github.com/eolinker/goku-api-gateway/common/endless"
Y
Your Name 已提交
10 11 12 13 14 15 16 17

	"github.com/eolinker/goku-api-gateway/goku-service/application"

	"github.com/eolinker/goku-api-gateway/node/routerRule"

	redis_manager "github.com/eolinker/goku-api-gateway/common/redis-manager"
	"github.com/eolinker/goku-api-gateway/node"

Y
Your Name 已提交
18 19 20 21
	"github.com/eolinker/goku-api-gateway/diting"
	"github.com/eolinker/goku-api-gateway/module"
	"github.com/eolinker/goku-api-gateway/node/admin"
	"github.com/eolinker/goku-api-gateway/node/monitor"
Y
Your Name 已提交
22
	entity "github.com/eolinker/goku-api-gateway/server/entity/console-entity"
Y
Your Name 已提交
23 24 25 26 27 28 29 30 31 32

	"github.com/eolinker/goku-api-gateway/config"
	log "github.com/eolinker/goku-api-gateway/goku-log"
	"github.com/eolinker/goku-api-gateway/node/console"
	"github.com/eolinker/goku-api-gateway/node/gateway"
	"github.com/eolinker/goku-api-gateway/node/router/httprouter"
)

//Server server
type Server struct {
Y
Your Name 已提交
33 34
	//port    int
	//console *console.Console
Y
Your Name 已提交
35
	router http.Handler
Y
Your Name 已提交
36 37 38
}

//NewServer newServer
Y
Your Name 已提交
39
func NewServer() *Server {
Y
Your Name 已提交
40
	return &Server{
Y
Your Name 已提交
41 42
		//port:    port,
		//console: nil,
Y
Your Name 已提交
43
		router: nil,
Y
Your Name 已提交
44 45 46 47 48 49 50 51 52
	}
}

//SetRouter setRouter
func (s *Server) SetRouter(r http.Handler) error {
	s.router = r
	return nil
}

Y
Your Name 已提交
53 54 55
//ServerWidthConsole 开启节点监听服务
func (s *Server) ServerWidthConsole(console console.ConfigConsole) error {
	if console == nil {
Y
Your Name 已提交
56 57 58
		return errors.New("can not start server widthout router and console")
	}

Y
Your Name 已提交
59
	if console != nil {
Y
Your Name 已提交
60

Y
Your Name 已提交
61
		conf, err := console.RegisterToConsole()
Y
Your Name 已提交
62 63 64 65
		if err != nil {
			return err
		}

Y
Your Name 已提交
66 67
		console.AddListen(s.FlushRouter)
		console.AddListen(s.FlushModule)
Y
Your Name 已提交
68 69 70 71 72 73 74
		console.AddListen(s.FlushRedisConfig)
		console.AddListen(s.FlushRouterRule)
		console.AddListen(s.FlushGatewayBasicConfig)

		console.Listen()

		return s.ServerWidthConfig(conf)
Y
Your Name 已提交
75
	}
Y
Your Name 已提交
76
	return errors.New("can not start server widthout router and console")
Y
Your Name 已提交
77 78
}

Y
Your Name 已提交
79 80
//ServerWidthConfig 处理配置
func (s *Server) ServerWidthConfig(conf *config.GokuConfig) error {
Y
Your Name 已提交
81

Y
Your Name 已提交
82
	if conf == nil {
Y
Your Name 已提交
83
		return errors.New("can not start server width out config")
Y
Your Name 已提交
84
	}
Y
Your Name 已提交
85
	s.FlushRedisConfig(conf)
Y
Your Name 已提交
86 87 88 89 90

	r, err := gateway.Parse(conf, httprouter.Factory())
	if err != nil {
		log.Panic("parse config error:", err)
	}
Y
Your Name 已提交
91 92 93
	if conf.GatewayBasicInfo != nil {
		application.SetSkipCertificate(conf.GatewayBasicInfo.SkipCertificate)
	}
Y
Your Name 已提交
94 95 96 97
	e := s.SetRouter(r)
	if e != nil {
		return e
	}
Y
Your Name 已提交
98 99
	routerRule.Load(conf.Routers)

Y
Your Name 已提交
100
	// 初始化监控模块
Y
Your Name 已提交
101
	monitor.Init(conf.Cluster, conf.Instance)
Y
Your Name 已提交
102 103 104

	s.FlushModule(conf)

Y
Your Name 已提交
105
	if conf.BindAddress == "" {
Y
Your Name 已提交
106 107
		log.Panic("invalid bind address")
	}
Y
Your Name 已提交
108

Y
Your Name 已提交
109
	// 启用管理接口
Y
Your Name 已提交
110
	if conf.AdminAddress != "" {
Y
Your Name 已提交
111 112 113
		StartAdmin(conf.AdminAddress)
	}

Y
Your Name 已提交
114 115
	//return endless.ListenAndServe(conf.BindAddress, s)
	return http.ListenAndServe(conf.BindAddress, s)
Y
Your Name 已提交
116
}
Y
Your Name 已提交
117

Y
Your Name 已提交
118 119
//FlushRouter flushConfig
func (s *Server) FlushRouter(config *config.GokuConfig) {
Y
Your Name 已提交
120 121 122 123 124 125 126
	r, err := gateway.Parse(config, httprouter.Factory())
	if err != nil {
		log.Error("parse config error:", err)
		return
	}
	_ = s.SetRouter(r)
}
Y
Your Name 已提交
127

Y
Your Name 已提交
128 129 130 131
//FlushRouterRule flushConfig
func (s *Server) FlushRouterRule(config *config.GokuConfig) {
	routerRule.Load(config.Routers)
}
Y
Your Name 已提交
132

Y
Your Name 已提交
133 134 135 136 137 138 139 140 141 142 143
//FlushGatewayBasicConfig 刷新网关基础配置
func (s *Server) FlushGatewayBasicConfig(config *config.GokuConfig) {
	if config.GatewayBasicInfo != nil {
		application.SetSkipCertificate(config.GatewayBasicInfo.SkipCertificate)
	}
}

//FlushRedisConfig 刷新redis配置
func (s *Server) FlushRedisConfig(config *config.GokuConfig) {
	if r, ok := config.ExtendsConfig["redis"]; ok {
		if r == nil {
Y
Your Name 已提交
144 145
			return
		}
Y
Your Name 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158
		rr := r.(map[string]interface{})
		rdsConfig := &entity.ClusterRedis{
			Addrs:    rr["addrs"].(string),
			DbIndex:  int(rr["dbIndex"].(float64)),
			Masters:  rr["masters"].(string),
			Mode:     rr["mode"].(string),
			Password: rr["password"].(string),
		}
		rds := redis_manager.Create(rdsConfig)
		log.Info(rdsConfig)
		redis_manager.SetDefault(rds)
		node.InitPluginUtils()
	}
Y
Your Name 已提交
159
}
Y
Your Name 已提交
160 161

//FlushModule 刷新模块配置
Y
Your Name 已提交
162 163 164 165
func (s *Server) FlushModule(conf *config.GokuConfig) {
	SetLog(conf.Log)
	SetAccessLog(conf.AccessLog)
	module.Refresh(nil)
Y
Your Name 已提交
166

Y
Your Name 已提交
167 168 169 170
	//demo:= map[string]string{
	//	"diting.prometheus":"",
	//}
	diting.Refresh(conf.MonitorModules)
Y
Your Name 已提交
171

Y
Your Name 已提交
172
	admin.Refresh()
Y
Your Name 已提交
173

Y
Your Name 已提交
174
}
Y
Your Name 已提交
175 176
func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {

Y
Your Name 已提交
177 178 179 180 181 182 183
	defer func() {
		if err := recover(); err != nil {
			fmt.Println(err)
			print(err)
			debug.PrintStack()
		}
	}()
Y
Your Name 已提交
184 185 186 187 188 189 190 191
	if s.router == nil {
		w.WriteHeader(404)
		return
	}

	s.router.ServeHTTP(w, req)

}