gins_server.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright GoFrame Author(https://github.com/gogf/gf). 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 m map[string]interface{}
  24. nodeKey, _ := gutil.MapPossibleItemByKey(Config().GetMap("."), configNodeNameServer)
  25. if nodeKey == "" {
  26. nodeKey = configNodeNameServer
  27. }
  28. m = Config().GetMap(fmt.Sprintf(`%s.%s`, nodeKey, s.GetName()))
  29. if len(m) == 0 {
  30. m = Config().GetMap(nodeKey)
  31. }
  32. if len(m) > 0 {
  33. if err := s.SetConfigWithMap(m); err != nil {
  34. panic(err)
  35. }
  36. }
  37. // As it might use template feature,
  38. // it initialize the view instance as well.
  39. _ = getViewInstance()
  40. }
  41. return s
  42. }).(*ghttp.Server)
  43. }