generated from wuhan005/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
133 lines (108 loc) · 3.32 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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2022 E99p1ant. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"io"
"log"
"mime"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/flamego/flamego"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
defaultFormat = "text/html"
)
func init() {
prometheus.MustRegister(requestCount)
prometheus.MustRegister(requestDuration)
}
func main() {
f := flamego.Classic()
flamego.SetEnv(flamego.EnvTypeProd)
f.Any("/", func(ctx flamego.Context) {
start := time.Now()
namespace := ctx.Request().Header.Get(Namespace)
serviceName := ctx.Request().Header.Get(ServiceName)
defaultExts, err := mime.ExtensionsByType(defaultFormat)
if err != nil || len(defaultExts) == 0 {
panic("couldn't get file extension for default format")
}
defaultExt := defaultExts[0]
ext := defaultExt
format := ctx.Request().Header.Get(FormatHeader)
if format == "" {
format = defaultFormat
log.Printf("format not specified. Using %v", format)
}
cext, err := mime.ExtensionsByType(format)
if err != nil {
log.Printf("unexpected error reading media type extension: %v. Using %v", err, ext)
format = defaultFormat
} else if len(cext) == 0 {
log.Printf("couldn't get media type extension. Using %v", ext)
} else {
ext = cext[0]
}
ctx.ResponseWriter().Header().Set(ContentType, format)
errCode := ctx.Request().Header.Get(CodeHeader)
code, err := strconv.Atoi(errCode)
if err != nil {
code = http.StatusNotFound
log.Printf("unexpected error reading return code: %v. Using %v", err, code)
}
ctx.ResponseWriter().WriteHeader(code)
if !strings.HasPrefix(ext, ".") {
ext = "." + ext
}
// special case for compatibility
if ext == ".htm" {
ext = ".html"
}
path := "/www"
statusCode := strconv.Itoa(code)
files := []string{
filepath.Join(path, namespace, serviceName, fmt.Sprintf("%v%v", code, ext)),
filepath.Join(path, namespace, serviceName, fmt.Sprintf("%cxx%v", statusCode[0], ext)),
filepath.Join(path, namespace, fmt.Sprintf("%v%v", code, ext)),
filepath.Join(path, namespace, fmt.Sprintf("%cxx%v", statusCode[0], ext)),
filepath.Join(path, fmt.Sprintf("%v%v", code, ext)),
filepath.Join(path, fmt.Sprintf("%cxx%v", statusCode[0], ext)),
}
f, err := tryToOpenFile(files)
if err != nil {
log.Printf("unexpected error opening file: %v", err)
http.NotFound(ctx.ResponseWriter(), ctx.Request().Request)
return
}
defer f.Close()
log.Printf("serving custom error response for code %v and format %v", code, format)
_, _ = io.Copy(ctx.ResponseWriter(), f)
duration := time.Since(start).Seconds()
proto := strconv.Itoa(ctx.Request().ProtoMajor)
proto = fmt.Sprintf("%s.%s", proto, strconv.Itoa(ctx.Request().ProtoMinor))
requestCount.WithLabelValues(proto).Inc()
requestDuration.WithLabelValues(proto).Observe(duration)
})
f.Any("/metrics", promhttp.Handler())
f.Any("/healthz", func() {})
f.Run(8080)
}
func tryToOpenFile(paths []string) (*os.File, error) {
for _, path := range paths {
f, err := os.Open(path)
if err != nil {
log.Printf("unexpected error opening file: %v", err)
continue
}
return f, nil
}
return nil, os.ErrNotExist
}