@@ -60,7 +60,7 @@ func newApp() *app {
60
60
mu : new (sync.Mutex ),
61
61
}
62
62
63
- aahApp .he = & httpEngine {
63
+ aahApp .he = & HTTPEngine {
64
64
a : aahApp ,
65
65
ctxPool : new (sync.Pool ),
66
66
registry : & ainsp.TargetRegistry {
@@ -115,7 +115,7 @@ type app struct {
115
115
116
116
cfg * config.Config
117
117
tlsCfg * tls.Config
118
- he * httpEngine
118
+ he * HTTPEngine
119
119
wse * ws.Engine
120
120
server * http.Server
121
121
redirectServer * http.Server
@@ -206,7 +206,7 @@ func (a *app) Init(importPath string) error {
206
206
}
207
207
}
208
208
if a .IsWebSocketEnabled () {
209
- if a .wse , err = ws .New (a .cfg , a .logger ); err != nil {
209
+ if a .wse , err = ws .New (a .cfg , a .logger , a . router ); err != nil {
210
210
return err
211
211
}
212
212
}
@@ -330,45 +330,23 @@ func (a *app) SetTLSConfig(tlsCfg *tls.Config) {
330
330
}
331
331
332
332
func (a * app ) AddController (c interface {}, methods []* ainsp.Method ) {
333
- a .he .registry .Add (c , methods )
333
+ a .HTTPEngine () .registry .Add (c , methods )
334
334
}
335
335
336
336
func (a * app ) AddWebSocket (w interface {}, methods []* ainsp.Method ) {
337
- if a .wse == nil {
338
- a .Log ().Warn ("It seems you have implemented WebSockets, However not enabled it, refer to https://aahframework.org/websocket.html" )
339
- return
340
- }
341
- a .wse .AddWebSocket (w , methods )
342
- }
343
-
344
- func (a * app ) OnWSPreConnect (ecf ws.EventCallbackFunc ) {
345
- if a .wse != nil {
346
- a .wse .OnPreConnect (ecf )
347
- }
348
- }
349
-
350
- func (a * app ) OnWSPostConnect (ecf ws.EventCallbackFunc ) {
351
- if a .wse != nil {
352
- a .wse .OnPostConnect (ecf )
353
- }
354
- }
355
-
356
- func (a * app ) OnWSPostDisconnect (ecf ws.EventCallbackFunc ) {
357
- if a .wse != nil {
358
- a .wse .OnPostDisconnect (ecf )
359
- }
337
+ a .WSEngine ().AddWebSocket (w , methods )
360
338
}
361
339
362
- func (a * app ) OnWSError (ecf ws.EventCallbackFunc ) {
363
- if a .wse != nil {
364
- a .wse .OnError (ecf )
365
- }
340
+ func (a * app ) HTTPEngine () * HTTPEngine {
341
+ return a .he
366
342
}
367
343
368
- func (a * app ) SetWSAuthCallback (ac ws.AuthCallbackFunc ) {
369
- if a .wse != nil {
370
- a .wse .SetAuthCallback (ac )
344
+ func (a * app ) WSEngine () * ws.Engine {
345
+ if a .wse == nil {
346
+ a .Log ().Warn ("It seems WebSocket is not enabled, set 'server.websocket.enable' to true." +
347
+ " Refer to https://docs.aahframework.org/websocket.html" )
371
348
}
349
+ return a .wse
372
350
}
373
351
374
352
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
@@ -384,8 +362,12 @@ func (a *app) logsDir() string {
384
362
}
385
363
386
364
func (a * app ) showDeprecatedMsg (msg string , v ... interface {}) {
387
- a .Log ().Warnf ("DEPRECATED: " + msg , v ... )
388
- a .Log ().Warn ("Deprecated elements are planned to be remove in major release v1.0.0" )
365
+ warnf := log .Warnf
366
+ if a .Log () != nil {
367
+ warnf = a .Log ().Warnf
368
+ }
369
+ warnf ("DEPRECATED: " + msg , v ... )
370
+ warnf ("Deprecated elements are planned to be remove in major release v1.0.0" )
389
371
}
390
372
391
373
func (a * app ) initPath () (err error ) {
@@ -596,73 +578,8 @@ func (a *app) aahRecover() {
596
578
func (a * app ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
597
579
defer a .aahRecover ()
598
580
if isWebSocket (r ) {
599
- a .handleWebSocket (w , r )
581
+ a .wse . Handle (w , r )
600
582
} else {
601
- a .handleHTTP (w , r )
583
+ a .he . Handle (w , r )
602
584
}
603
585
}
604
-
605
- func (a * app ) handleHTTP (w http.ResponseWriter , r * http.Request ) {
606
- ctx := a .he .ctxPool .Get ().(* Context )
607
- ctx .Req , ctx .Res = ahttp .AcquireRequest (r ), ahttp .AcquireResponseWriter (w )
608
- ctx .Set (reqStartTimeKey , time .Now ())
609
- defer a .he .releaseContext (ctx )
610
-
611
- // Record access log
612
- if a .accessLogEnabled {
613
- defer a .accessLog .Log (ctx )
614
- }
615
-
616
- // Recovery handling
617
- defer a .he .handleRecovery (ctx )
618
-
619
- if a .requestIDEnabled {
620
- ctx .setRequestID ()
621
- }
622
-
623
- // 'OnRequest' server extension point
624
- a .he .publishOnRequestEvent (ctx )
625
-
626
- // Middlewares, interceptors, targeted controller
627
- if len (a .he .mwChain ) == 0 {
628
- ctx .Log ().Error ("'init.go' file introduced in release v0.10; please check your 'app-base-dir/app' " +
629
- "and then add to your version control" )
630
- ctx .Reply ().Error (& Error {
631
- Reason : ErrGeneric ,
632
- Code : http .StatusInternalServerError ,
633
- Message : http .StatusText (http .StatusInternalServerError ),
634
- })
635
- } else {
636
- a .he .mwChain [0 ].Next (ctx )
637
- }
638
-
639
- a .he .writeReply (ctx )
640
- }
641
-
642
- func (a * app ) handleWebSocket (w http.ResponseWriter , r * http.Request ) {
643
- domain := a .Router ().Lookup (ahttp .IdentifyHost (r ))
644
- if domain == nil {
645
- a .wse .Log ().Errorf ("WS: domain not found: %s" , ahttp .IdentifyHost (r ))
646
- a .wse .ReplyError (w , http .StatusNotFound )
647
- return
648
- }
649
-
650
- r .Method = "WS" // for route lookup
651
- route , pathParams , _ := domain .Lookup (r )
652
- if route == nil {
653
- a .wse .Log ().Errorf ("WS: route not found: %s" , r .URL .Path )
654
- a .wse .ReplyError (w , http .StatusNotFound )
655
- return
656
- }
657
-
658
- ctx , err := a .wse .Connect (w , r , route , pathParams )
659
- if err != nil {
660
- if err == ws .ErrWebSocketNotFound {
661
- a .wse .Log ().Errorf ("WS: route not found: %s" , r .URL .Path )
662
- a .wse .ReplyError (w , http .StatusNotFound )
663
- }
664
- return
665
- }
666
-
667
- a .wse .CallAction (ctx )
668
- }
0 commit comments