github.com/wangyougui/gf/v2@v2.6.5/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/wangyougui/gf. 6 7 package gins 8 9 import ( 10 "context" 11 "fmt" 12 13 "github.com/wangyougui/gf/v2/internal/consts" 14 "github.com/wangyougui/gf/v2/internal/instance" 15 "github.com/wangyougui/gf/v2/internal/intlog" 16 "github.com/wangyougui/gf/v2/net/ghttp" 17 "github.com/wangyougui/gf/v2/util/gconv" 18 "github.com/wangyougui/gf/v2/util/gutil" 19 ) 20 21 // Server returns an instance of http server with specified name. 22 // Note that it panics if any error occurs duration instance creating. 23 func Server(name ...interface{}) *ghttp.Server { 24 var ( 25 err error 26 ctx = context.Background() 27 instanceName = ghttp.DefaultServerName 28 instanceKey = fmt.Sprintf("%s.%v", frameCoreComponentNameServer, name) 29 ) 30 if len(name) > 0 && name[0] != "" { 31 instanceName = gconv.String(name[0]) 32 } 33 return instance.GetOrSetFuncLock(instanceKey, func() interface{} { 34 server := ghttp.GetServer(instanceName) 35 if Config().Available(ctx) { 36 // Server initialization from configuration. 37 var ( 38 configMap map[string]interface{} 39 serverConfigMap map[string]interface{} 40 serverLoggerConfigMap map[string]interface{} 41 configNodeName string 42 ) 43 if configMap, err = Config().Data(ctx); err != nil { 44 intlog.Errorf(ctx, `retrieve config data map failed: %+v`, err) 45 } 46 // Find possible server configuration item by possible names. 47 if len(configMap) > 0 { 48 if v, _ := gutil.MapPossibleItemByKey(configMap, consts.ConfigNodeNameServer); v != "" { 49 configNodeName = v 50 } 51 if configNodeName == "" { 52 if v, _ := gutil.MapPossibleItemByKey(configMap, consts.ConfigNodeNameServerSecondary); v != "" { 53 configNodeName = v 54 } 55 } 56 } 57 // Automatically retrieve configuration by instance name. 58 serverConfigMap = Config().MustGet( 59 ctx, 60 fmt.Sprintf(`%s.%s`, configNodeName, instanceName), 61 ).Map() 62 if len(serverConfigMap) == 0 { 63 serverConfigMap = Config().MustGet(ctx, configNodeName).Map() 64 } 65 if len(serverConfigMap) > 0 { 66 if err = server.SetConfigWithMap(serverConfigMap); err != nil { 67 panic(err) 68 } 69 } else { 70 // The configuration is not necessary, so it just prints internal logs. 71 intlog.Printf( 72 ctx, 73 `missing configuration from configuration component for HTTP server "%s"`, 74 instanceName, 75 ) 76 } 77 // Server logger configuration checks. 78 serverLoggerConfigMap = Config().MustGet( 79 ctx, 80 fmt.Sprintf(`%s.%s.%s`, configNodeName, instanceName, consts.ConfigNodeNameLogger), 81 ).Map() 82 if len(serverLoggerConfigMap) == 0 && len(serverConfigMap) > 0 { 83 serverLoggerConfigMap = gconv.Map(serverConfigMap[consts.ConfigNodeNameLogger]) 84 } 85 if len(serverLoggerConfigMap) > 0 { 86 if err = server.Logger().SetConfigWithMap(serverLoggerConfigMap); err != nil { 87 panic(err) 88 } 89 } 90 } 91 // The server name is necessary. It sets a default server name is it is not configured. 92 if server.GetName() == "" || server.GetName() == ghttp.DefaultServerName { 93 server.SetName(instanceName) 94 } 95 // As it might use template feature, 96 // it initializes the view instance as well. 97 _ = getViewInstance() 98 return server 99 }).(*ghttp.Server) 100 }