application.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package application
  2. import (
  3. "context"
  4. "time"
  5. "git.ali33.ru/fcg-xvii/rest"
  6. "git.ali33.ru/fcg-xvii/rest/rest_http"
  7. "git.ali33.ru/fcg-xvii/rest/rest_websocket"
  8. )
  9. func New(appConf *AppConfig, swaggerConf *SwaggerConf, ctx context.Context) rest.IApplication {
  10. app := &Application{
  11. conf: appConf,
  12. //Sockets: NewSockets(),
  13. }
  14. if swaggerConf != nil && swaggerConf.Enabled {
  15. app.swagger = NewSwagger(swaggerConf)
  16. go app.swagger.Start(ctx)
  17. }
  18. go app.start(ctx)
  19. return app
  20. }
  21. type Application struct {
  22. *Sockets
  23. conf *AppConfig
  24. swagger *Swagger
  25. //core any
  26. ctx context.Context
  27. cancel context.CancelFunc
  28. chConnect chan rest.IStream
  29. }
  30. func (s *Application) start(ctx context.Context) (err error) {
  31. s.ctx, s.cancel = context.WithCancel(ctx)
  32. var server rest.IServer
  33. if s.conf.SSL {
  34. server = rest.NewServerTLS(s.conf.Addr, s.conf.Secret, s.conf.TLSKeyPath, s.conf.TLSCertPath)
  35. } else {
  36. server = rest.NewServer(s.conf.Addr, s.conf.Secret)
  37. }
  38. if s.conf.Http {
  39. restServ := rest_http.New(s, s.conf.Core, s.conf.GetHTTPHeaders()())
  40. restServ.Prepare(server, s.conf.GetHttpPrefix())
  41. //log.Println("http part prepared...")
  42. }
  43. if s.conf.Websocket {
  44. s.chConnect = make(chan rest.IStream)
  45. ws := rest_websocket.New(s, s.conf.Core)
  46. ws.Prepare(server, s.conf.GetWebsocketPrefix())
  47. go s.work()
  48. //log.Println("websocket engine prepared...")
  49. }
  50. if err = server.Listen(time.Second, s.ctx); err != nil {
  51. s.cancel()
  52. }
  53. return
  54. }
  55. func (s *Application) Stop() {
  56. s.cancel()
  57. }
  58. func (s *Application) Executer(r rest.IRequestIn) (rest.IExecuter, bool) {
  59. method := s.conf.GetCommandsMethod()
  60. if command, check := method(r.RCommand()); check {
  61. return command, true
  62. }
  63. return nil, false
  64. }