gins_server.go 3.4 KB

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