app_config.go 1.5 KB

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