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

import (
	"errors"
Y
Your Name 已提交
5 6 7 8
	"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 已提交
9 10 11 12 13 14 15 16 17 18 19 20
	"net/http"

	"github.com/eolinker/goku-api-gateway/common/endless"
	"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 已提交
21 22
	//port    int
	//console *console.Console
Y
Your Name 已提交
23 24 25 26
	router  http.Handler
}

//NewServer newServer
Y
Your Name 已提交
27
func NewServer() *Server {
Y
Your Name 已提交
28
	return &Server{
Y
Your Name 已提交
29 30
		//port:    port,
		//console: nil,
Y
Your Name 已提交
31 32 33 34 35 36 37 38 39 40 41 42
		router:  nil,
	}
}

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


//Server server
Y
Your Name 已提交
43 44
func (s *Server) ServerWidthConsole(console *console.Console ) error {
	if  console == nil {
Y
Your Name 已提交
45 46 47
		return errors.New("can not start server widthout router and console")
	}

Y
Your Name 已提交
48
	if console != nil {
Y
Your Name 已提交
49

Y
Your Name 已提交
50
		conf, err := console.GetConfig()
Y
Your Name 已提交
51 52 53 54
		if err != nil {
			return err
		}

Y
Your Name 已提交
55 56 57 58 59 60 61 62
		console.AddListen(s.FlushRouter)
		console.AddListen(s.FlushModule)
		return 	s.ServerWidthConfig(conf)
	}
	return  errors.New("can not start server widthout router and console")
}

func (s *Server)ServerWidthConfig(conf *config.GokuConfig)error  {
Y
Your Name 已提交
63

Y
Your Name 已提交
64 65
	if conf == nil{
		return errors.New("can not start server width out config")
Y
Your Name 已提交
66 67
	}

Y
Your Name 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

	r, err := gateway.Parse(conf, httprouter.Factory())
	if err != nil {
		log.Panic("parse config error:", err)
	}
	e := s.SetRouter(r)
	if e != nil {
		return e
	}
	// 初始化监控模块
	monitor.Init(conf.Cluster,conf.Instance)

	s.FlushModule(conf)

	if conf.BindAddress == ""{
		log.Panic("invalid bind address")
	}
	//if conf.AdminAddress == ""{
	//	log.Panic("invalid admin address")
	//}
	// 启用管理接口
	if conf.AdminAddress != ""{
		StartAdmin(conf.AdminAddress)
	}

	return endless.ListenAndServe(conf.BindAddress, s)
Y
Your Name 已提交
94
}
Y
Your Name 已提交
95 96
//FlushRouter flushConfig
func (s *Server) FlushRouter(config *config.GokuConfig) {
Y
Your Name 已提交
97 98 99 100 101 102 103


		r, err := gateway.Parse(config, httprouter.Factory())
		if err != nil {
			log.Error("parse config error:", err)
			return
		}
Y
Your Name 已提交
104 105 106 107 108 109 110
		_=s.SetRouter(r)
}
//FlushRouter flushConfig
func (s *Server) FlushModule(conf *config.GokuConfig) {
	SetLog(conf.Log)
	SetAccessLog(conf.AccessLog)
	module.Refresh(nil)
Y
Your Name 已提交
111

Y
Your Name 已提交
112 113 114 115
	//demo:= map[string]string{
	//	"diting.prometheus":"",
	//}
	diting.Refresh(conf.MonitorModules)
Y
Your Name 已提交
116

Y
Your Name 已提交
117
	admin.Refresh()
Y
Your Name 已提交
118

Y
Your Name 已提交
119
}
Y
Your Name 已提交
120 121 122 123 124 125 126 127 128 129
func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {

	if s.router == nil {
		w.WriteHeader(404)
		return
	}

	s.router.ServeHTTP(w, req)

}