github.com/gogf/gf@v1.16.9/frame/gins/gins_server.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gins 8 9 import ( 10 "fmt" 11 "github.com/gogf/gf/net/ghttp" 12 "github.com/gogf/gf/util/gutil" 13 ) 14 15 const ( 16 frameCoreComponentNameServer = "gf.core.component.server" 17 configNodeNameServer = "server" 18 ) 19 20 // Server returns an instance of http server with specified name. 21 func Server(name ...interface{}) *ghttp.Server { 22 instanceKey := fmt.Sprintf("%s.%v", frameCoreComponentNameServer, name) 23 return instances.GetOrSetFuncLock(instanceKey, func() interface{} { 24 s := ghttp.GetServer(name...) 25 // To avoid file no found error while it's not necessary. 26 if Config().Available() { 27 var ( 28 serverConfigMap map[string]interface{} 29 serverLoggerConfigMap map[string]interface{} 30 ) 31 nodeKey, _ := gutil.MapPossibleItemByKey(Config().GetMap("."), configNodeNameServer) 32 if nodeKey == "" { 33 nodeKey = configNodeNameServer 34 } 35 // Server configuration. 36 serverConfigMap = Config().GetMap(fmt.Sprintf(`%s.%s`, nodeKey, s.GetName())) 37 if len(serverConfigMap) == 0 { 38 serverConfigMap = Config().GetMap(nodeKey) 39 } 40 if len(serverConfigMap) > 0 { 41 if err := s.SetConfigWithMap(serverConfigMap); err != nil { 42 panic(err) 43 } 44 } 45 // Server logger configuration. 46 serverLoggerConfigMap = Config().GetMap( 47 fmt.Sprintf(`%s.%s.%s`, nodeKey, s.GetName(), configNodeNameLogger), 48 ) 49 if len(serverLoggerConfigMap) > 0 { 50 if err := s.Logger().SetConfigWithMap(serverLoggerConfigMap); err != nil { 51 panic(err) 52 } 53 } 54 // As it might use template feature, 55 // it initialize the view instance as well. 56 _ = getViewInstance() 57 } 58 return s 59 }).(*ghttp.Server) 60 }