Skip to content

Commit a9e89e8

Browse files
committed
cleanup, HTTP and WS engine events organized,
- godoc update - added deprecated warn msg, so user will notice updates - couple of refactors
1 parent 7129873 commit a9e89e8

20 files changed

+344
-672
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
_obj
88
_test
99
vendor/*/
10+
logs
1011

1112
# test
1213
coverage.out

aah.go

+20-103
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func newApp() *app {
6060
mu: new(sync.Mutex),
6161
}
6262

63-
aahApp.he = &httpEngine{
63+
aahApp.he = &HTTPEngine{
6464
a: aahApp,
6565
ctxPool: new(sync.Pool),
6666
registry: &ainsp.TargetRegistry{
@@ -115,7 +115,7 @@ type app struct {
115115

116116
cfg *config.Config
117117
tlsCfg *tls.Config
118-
he *httpEngine
118+
he *HTTPEngine
119119
wse *ws.Engine
120120
server *http.Server
121121
redirectServer *http.Server
@@ -206,7 +206,7 @@ func (a *app) Init(importPath string) error {
206206
}
207207
}
208208
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 {
210210
return err
211211
}
212212
}
@@ -330,45 +330,23 @@ func (a *app) SetTLSConfig(tlsCfg *tls.Config) {
330330
}
331331

332332
func (a *app) AddController(c interface{}, methods []*ainsp.Method) {
333-
a.he.registry.Add(c, methods)
333+
a.HTTPEngine().registry.Add(c, methods)
334334
}
335335

336336
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)
360338
}
361339

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
366342
}
367343

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")
371348
}
349+
return a.wse
372350
}
373351

374352
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
@@ -384,8 +362,12 @@ func (a *app) logsDir() string {
384362
}
385363

386364
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")
389371
}
390372

391373
func (a *app) initPath() (err error) {
@@ -596,73 +578,8 @@ func (a *app) aahRecover() {
596578
func (a *app) ServeHTTP(w http.ResponseWriter, r *http.Request) {
597579
defer a.aahRecover()
598580
if isWebSocket(r) {
599-
a.handleWebSocket(w, r)
581+
a.wse.Handle(w, r)
600582
} else {
601-
a.handleHTTP(w, r)
583+
a.he.Handle(w, r)
602584
}
603585
}
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-
}

aah_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ func (s *testSiteController) JSONP(callback string) {
457457
}
458458

459459
func (s *testSiteController) SecureJSON() {
460-
s.Reply().SecureJSON(sample{
460+
s.Reply().JSONSecure(sample{
461461
Username: "myuser_name",
462462
ProductName: "JSONP product",
463463
ProductID: 190398398,

access_log_test.go

-31
This file was deleted.

context.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type Context struct {
4848
Res ahttp.ResponseWriter
4949

5050
a *app
51-
e *httpEngine
51+
e *HTTPEngine
5252
controller *ainsp.Target
5353
action *ainsp.Method
5454
actionrv reflect.Value
@@ -148,12 +148,12 @@ func (ctx *Context) Session() *session.Session {
148148
// next middleware, next interceptor or action based on context it being used.
149149
// Contexts:
150150
// 1) If it's called in the middleware, then middleware chain stops;
151-
// framework starts processing response.
151+
// framework starts processing response.
152152
// 2) If it's called in Before interceptor then Before<Action> interceptor,
153-
// mapped <Action>, After<Action> interceptor and After interceptor will not
154-
// execute; framework starts processing response.
153+
// mapped <Action>, After<Action> interceptor and After interceptor will not
154+
// execute; framework starts processing response.
155155
// 3) If it's called in Mapped <Action> then After<Action> interceptor and
156-
// After interceptor will not execute; framework starts processing response.
156+
// After interceptor will not execute; framework starts processing response.
157157
func (ctx *Context) Abort() {
158158
ctx.abort = true
159159
}
@@ -169,7 +169,7 @@ func (ctx *Context) IsStaticRoute() bool {
169169
// SetURL method is to set the request URL to change the behaviour of request
170170
// routing. Ideal for URL rewrting. URL can be relative or absolute URL.
171171
//
172-
// Note: This method only takes effect on `OnRequest` server event.
172+
// Note: This method only takes effect on `OnRequest` HTTP server event.
173173
func (ctx *Context) SetURL(pathURL string) {
174174
if !ctx.decorated {
175175
return
@@ -199,7 +199,7 @@ func (ctx *Context) SetURL(pathURL string) {
199199
// SetMethod method is to set the request `Method` to change the behaviour
200200
// of request routing. Ideal for URL rewrting.
201201
//
202-
// Note: This method only takes effect on `OnRequest` server event.
202+
// Note: This method only takes effect on `OnRequest` HTTP server event.
203203
func (ctx *Context) SetMethod(method string) {
204204
if !ctx.decorated {
205205
return

0 commit comments

Comments
 (0)