app_config.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package application
  2. import (
  3. "git.ali33.ru/fcg-xvii/rest"
  4. "git.ali33.ru/fcg-xvii/rest/commands"
  5. )
  6. func HTTPHeadersCrossOrigin() func() map[string]string {
  7. return func() map[string]string {
  8. return map[string]string{
  9. "Access-Control-Allow-Origin": "*",
  10. "Access-Control-Allow-Methods": "POST, GET, OPTIONS, PUT, DELETE",
  11. "Access-Control-Allow-Headers": "Content-Type, Authorization, X-Requested-With",
  12. }
  13. }
  14. }
  15. type AppConfig struct {
  16. SSL bool
  17. Addr string
  18. Http bool
  19. Websocket bool
  20. Secret []byte
  21. HttpPrefix string
  22. WebsocketPrefix string
  23. HTTPHeaders func() map[string]string
  24. Commands func(string) (rest.IExecuter, bool)
  25. TLSKeyPath string
  26. TLSCertPath string
  27. Core any
  28. OnSocketConnect func(rest.IStream)
  29. OnSocketDisconnect func(rest.IStream)
  30. }
  31. func (s *AppConfig) GetCommandsMethod() func(string) (rest.IExecuter, bool) {
  32. if s.Commands != nil {
  33. return s.Commands
  34. }
  35. return commands.GetCommand
  36. }
  37. func (s *AppConfig) GetHttpPrefix() string {
  38. if len(s.HttpPrefix) > 0 {
  39. return s.HttpPrefix
  40. }
  41. return "/api/"
  42. }
  43. func (s *AppConfig) GetWebsocketPrefix() string {
  44. if len(s.WebsocketPrefix) > 0 {
  45. return s.WebsocketPrefix
  46. }
  47. return "/ws/"
  48. }
  49. func (s *AppConfig) GetHTTPHeaders() func() map[string]string {
  50. if s.HTTPHeaders != nil {
  51. return s.HTTPHeaders
  52. }
  53. return HTTPHeadersCrossOrigin()
  54. }