forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modrevision.go
80 lines (71 loc) · 2.29 KB
/
modrevision.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
package modrevision
import (
"fmt"
"strings"
"time"
"github.com/kataras/iris/v12/context"
)
func init() {
context.SetHandlerName("iris/middleware/modrevision.*", "iris.modrevision")
}
// Options holds the necessary values to render the server name, environment and build information.
// See the `New` package-level function.
type Options struct {
// The ServerName, e.g. Iris Server.
ServerName string
// The Environment, e.g. development.
Env string
// The Developer, e.g. kataras.
Developer string
// True to display the build time as unix (seconds).
UnixTime bool
// A non nil time location value to customize the display of the build time.
TimeLocation *time.Location
}
// New returns an Iris Handler which renders
// the server name (env), build information (if available)
// and an OK message. The handler displays simple debug information such as build commit id and time.
// It does NOT render information about the Go language itself or any operating system confgiuration
// for security reasons.
//
// Example Code:
//
// app.Get("/health", modrevision.New(modrevision.Options{
// ServerName: "Iris Server",
// Env: "development",
// Developer: "kataras",
// TimeLocation: time.FixedZone("Greece/Athens", 7200),
// }))
func New(opts Options) context.Handler {
buildTime, buildRevision := context.BuildTime, context.BuildRevision
if opts.UnixTime {
if t, err := time.Parse(time.RFC3339, buildTime); err == nil {
buildTime = fmt.Sprintf("%d", t.Unix())
}
} else if opts.TimeLocation != nil {
if t, err := time.Parse(time.RFC3339, buildTime); err == nil {
buildTime = t.In(opts.TimeLocation).String()
}
}
var buildInfo string
if buildInfo = opts.ServerName; buildInfo != "" {
if env := opts.Env; env != "" {
buildInfo += fmt.Sprintf(" (%s)", env)
}
}
if buildRevision != "" && buildTime != "" {
buildTitle := ">>>> build"
tab := strings.Repeat(" ", len(buildTitle))
buildInfo += fmt.Sprintf("\n\n%s\n%[2]srevision %[3]s\n%[2]sbuildtime %[4]s\n%[2]sdeveloper %[5]s",
buildTitle, tab, buildRevision, buildTime, opts.Developer)
}
contents := []byte(buildInfo)
if len(contents) > 0 {
contents = append(contents, []byte("\n\nOK")...)
} else {
contents = []byte("OK")
}
return func(ctx *context.Context) {
ctx.Write(contents)
}
}