-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
60 lines (52 loc) · 1.13 KB
/
main.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
package main
import (
"bytes"
"log"
"os"
"os/signal"
"syscall"
"github.com/valyala/fasthttp"
)
func main() {
port := envOr("PORT", "8080")
log.Printf("Port: %v", port)
routeFavIco := []byte("/favicon.ico")
routeS := []byte("/s/")
routeURL := []byte("/url")
respGoAway := []byte("go away")
server := &fasthttp.Server{
Handler: func(ctx *fasthttp.RequestCtx) {
path := ctx.Path()
if bytes.Equal(path, routeFavIco) {
ctx.Response.Header.Set(fasthttp.HeaderCacheControl, "Cache-Control: public, max-age=31536000")
ctx.SetStatusCode(fasthttp.StatusNoContent)
return
}
if bytes.HasPrefix(path, routeS) {
ctx.SetBody(bytes.TrimPrefix(path, routeS))
return
}
if bytes.HasPrefix(path, routeURL) {
fastProxy(ctx)
return
}
ctx.SetBody(respGoAway)
},
}
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
_ = server.Shutdown()
}()
if err := server.ListenAndServe(":" + port); err != nil {
log.Fatalf("server start0 fail, %v", err)
}
}
func envOr(env, def string) string {
e := os.Getenv(env)
if e == "" {
return def
}
return e
}