/
defaulthandler.go
89 lines (71 loc) · 2.87 KB
/
defaulthandler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package example
import (
erresponse "github.com/kklab-com/goth-erresponse"
"runtime/pprof"
"time"
"github.com/kklab-com/gone-core/channel"
"github.com/kklab-com/gone-http/http"
"github.com/kklab-com/gone-httpstatus"
buf "github.com/kklab-com/goth-bytebuf"
)
type DefaultTask struct {
http.DefaultHTTPHandlerTask
}
func (l *DefaultTask) Get(ctx channel.HandlerContext, req *http.Request, resp *http.Response, params map[string]any) http.ErrorResponse {
resp.SetStatusCode(httpstatus.OK)
resp.TextResponse(buf.NewByteBuf([]byte("feeling good")))
return nil
}
func (l *DefaultTask) Post(ctx channel.HandlerContext, req *http.Request, resp *http.Response, params map[string]any) http.ErrorResponse {
return l.Get(ctx, req, resp, params)
}
type DefaultHomeTask struct {
http.DefaultHTTPHandlerTask
}
func (l *DefaultHomeTask) Get(ctx channel.HandlerContext, req *http.Request, resp *http.Response, params map[string]any) http.ErrorResponse {
resp.SetStatusCode(httpstatus.OK)
resp.TextResponse(buf.NewByteBuf([]byte(req.RequestURI())))
go func() {
<-time.After(time.Millisecond * 100)
if ctx.Channel().IsActive() {
ctx.Channel().Disconnect()
}
}()
return nil
}
var longMsg = "{\"msg\":\"UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 11)210(node:44) UnhandledPromiseRejectionWarning: FetchError: Caught error after test environment was torn down\"}"
type LongTask struct {
http.DefaultHTTPHandlerTask
}
func (l *LongTask) Get(ctx channel.HandlerContext, req *http.Request, resp *http.Response, params map[string]any) http.ErrorResponse {
resp.SetStatusCode(httpstatus.OK)
resp.TextResponse(buf.NewByteBufString(longMsg + req.FormValue("v")))
return nil
}
type CloseTask struct {
http.DefaultHTTPHandlerTask
}
func (l *CloseTask) Get(ctx channel.HandlerContext, req *http.Request, resp *http.Response, params map[string]any) http.ErrorResponse {
resp.SetStatusCode(httpstatus.OK)
resp.TextResponse(buf.NewByteBuf([]byte(req.RequestURI())))
go func() {
<-time.After(time.Second)
ctx.Channel().Parent().Close()
}()
return nil
}
type Routine struct {
http.DefaultHTTPHandlerTask
}
func (a *Routine) Index(ctx channel.HandlerContext, req *http.Request, resp *http.Response, params map[string]any) http.ErrorResponse {
buffer := buf.EmptyByteBuf()
pprof.Lookup("goroutine").WriteTo(buffer, 1)
resp.TextResponse(buffer)
return nil
}
type Acceptance400 struct {
http.DispatchAcceptance
}
func (a *Acceptance400) Do(req *http.Request, resp *http.Response, params map[string]any) error {
return erresponse.InvalidRequest
}