-
Notifications
You must be signed in to change notification settings - Fork 8
/
simpleserver.go
85 lines (75 loc) · 1.85 KB
/
simpleserver.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
package httpsimple
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"strings"
"time"
"github.com/apex/gateway"
"github.com/buaazp/fasthttprouter"
"github.com/grokify/simplego/net/anyhttp"
"github.com/valyala/fasthttp"
)
const (
EngineAwsLambda = "awslambda"
EngineNetHttp = "nethttp"
EngineFastHttp = "fasthttp"
)
type SimpleServer interface {
PortInt() int
HttpEngine() string
Router() http.Handler
RouterFast() *fasthttprouter.Router
}
func Serve(svc SimpleServer) {
engine := strings.ToLower(strings.TrimSpace(svc.HttpEngine()))
if len(engine) == 0 {
engine = EngineNetHttp
}
switch engine {
case EngineNetHttp:
log.Fatal(
http.ListenAndServe(
portAddress(svc.PortInt()),
svc.Router()))
case EngineAwsLambda:
log.Fatal(
gateway.ListenAndServe(
portAddress(svc.PortInt()),
svc.Router()))
case EngineFastHttp:
router := svc.RouterFast()
if router == nil {
log.Fatal("E_NO_FASTROUTER_FOR_ENGINE_FASTHTTP")
}
log.Fatal(
fasthttp.ListenAndServe(
portAddress(svc.PortInt()),
router.Handler))
default:
log.Fatal(fmt.Sprintf("E_ENGINE_NOT_FOUND [%s]", engine))
}
}
func portAddress(port int) string { return ":" + strconv.Itoa(port) }
type TestResponse struct {
Command string `json:"command"`
RequestURL string `json:"requestURL"`
Time time.Time `json:"time"`
}
func HandleTestFastHTTP(ctx *fasthttp.RequestCtx) {
HandleTestAnyEngine(anyhttp.NewResReqFastHttp(ctx))
}
func HandleTestNetHTTP(res http.ResponseWriter, req *http.Request) {
HandleTestAnyEngine(anyhttp.NewResReqNetHttp(res, req))
}
func HandleTestAnyEngine(aRes anyhttp.Response, aReq anyhttp.Request) {
test := TestResponse{
Command: "pong",
RequestURL: string(aReq.RequestURI()),
Time: time.Now().UTC()}
bytes, _ := json.Marshal(test)
aRes.SetStatusCode(http.StatusOK)
aRes.SetBodyBytes(bytes)
}