gins_server.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. "fmt"
  9. "github.com/gogf/gf/net/ghttp"
  10. "github.com/gogf/gf/util/gutil"
  11. )
  12. const (
  13. frameCoreComponentNameServer = "gf.core.component.server"
  14. configNodeNameServer = "server"
  15. )
  16. // Server returns an instance of http server with specified name.
  17. func Server(name ...interface{}) *ghttp.Server {
  18. instanceKey := fmt.Sprintf("%s.%v", frameCoreComponentNameServer, name)
  19. return instances.GetOrSetFuncLock(instanceKey, func() interface{} {
  20. s := ghttp.GetServer(name...)
  21. // To avoid file no found error while it's not necessary.
  22. if Config().Available() {
  23. var (
  24. serverConfigMap map[string]interface{}
  25. serverLoggerConfigMap map[string]interface{}
  26. )
  27. nodeKey, _ := gutil.MapPossibleItemByKey(Config().GetMap("."), configNodeNameServer)
  28. if nodeKey == "" {
  29. nodeKey = configNodeNameServer
  30. }
  31. // Server configuration.
  32. serverConfigMap = Config().GetMap(fmt.Sprintf(`%s.%s`, nodeKey, s.GetName()))
  33. if len(serverConfigMap) == 0 {
  34. serverConfigMap = Config().GetMap(nodeKey)
  35. }
  36. if len(serverConfigMap) > 0 {
  37. if err := s.SetConfigWithMap(serverConfigMap); err != nil {
  38. panic(err)
  39. }
  40. }
  41. // Server logger configuration.
  42. serverLoggerConfigMap = Config().GetMap(
  43. fmt.Sprintf(`%s.%s.%s`, nodeKey, s.GetName(), configNodeNameLogger),
  44. )
  45. if len(serverLoggerConfigMap) > 0 {
  46. if err := s.Logger().SetConfigWithMap(serverLoggerConfigMap); err != nil {
  47. panic(err)
  48. }
  49. }
  50. // As it might use template feature,
  51. // it initialize the view instance as well.
  52. _ = getViewInstance()
  53. }
  54. return s
  55. }).(*ghttp.Server)
  56. }