gins_server.go 3.3 KB

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