Beispiel #1
0
func (s *Server) config(o *ServerOption) {
	o.init()

	var (
		errors []error
		logErr = func(err error) {
			if err != nil {
				errors = append(errors, err)
			}
		}
	)
	s.log = o.Logger
	s.codec = o.Codec
	s.headers = o.Headers
	s.checker = ws.HeaderChecker(o.WebSocketChecker).HandshakeCheck

	logErr(s.components.Init(s))

	s.log.Info(log.M{"msg": "Execute registered init before routes funcs "})
	for _, f := range s.OnLoadRoutes() {
		logErr(f(s))
	}

	s.log.Info(log.M{"msg": "Init Handlers and Filters"})
	logErr(s.Router.Init(s))

	s.log.Info(log.M{"msg": "Execute registered finial init funcs"})
	for _, f := range s.OnStart() {
		logErr(f(s))
	}

	if len(errors) != 0 {
		s.log.Fatal(log.M{"msg": "Server start failed.", "error": errors})
	}
	s.log.Info(log.M{"msg": "server start", "addr": o.ListenAddr})
	runtime.GC()
}
Beispiel #2
0
// all log message before server start will use standard log package
func (s *Server) config(o *ServerOption) {
	o.init()

	s.Log = o.Logger
	s.log = s.Log.Prefix("[Server]")
	var (
		log    = s.log.Infoln
		errors []error
		logErr = func(err error) {
			if err != nil {
				errors = append(errors, err)
			}
		}
	)

	s.checker = websocket.HeaderChecker(o.WebSocketChecker).HandshakeCheck

	if len(s.ResMaster.Resources) == 0 {
		log("Use default resource:", resource.RES_JSON)
		s.ResMaster.DefUse(resource.RES_JSON, resource.JSON{})
	} else {
		log("Resource types:", s.ResMaster.Types, " Default:", s.ResMaster.Default)
	}

	s.processNotAcceptable = o.ProcessNotAcceptable
	log("Process non-acceptable request:", s.processNotAcceptable)

	log("VarCountPerRoute:", o.PathVarCount)
	pathVarCount = o.PathVarCount
	log("FilterCountPerRoute:", o.FilterCount)
	filterCount = o.FilterCount

	s.componentManager.initHook = func(name string) {
		switch name {
		case _GLOBAL_COMPONENT:
			log("Init global components")
		case _ANONYMOUS_COMPONENT:
			log("Init anonymous components")
		default:
			log("  " + name)
		}
	}
	logErr(s.componentManager.Init(s))

	log("Execute registered init before routes funcs ")
	for _, f := range s.beforeRoutes(nil) {
		logErr(f(s))
	}

	log("Init root filters")
	logErr(s.RootFilters.Init(s))

	log("Init Handlers and Filters")
	logErr(s.Router.Init(s))

	log("Execute registered finial init funcs")
	for _, f := range s.finalInits(nil) {
		logErr(f(s))
	}

	if len(errors) != 0 {
		s.Log.Fatalln(errors)
	}

	// destroy temporary data store
	tmpDestroy()
	log("Server Start: ", o.ListenAddr)

	runtime.GC()
}