-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
169 lines (143 loc) · 4.49 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"fmt"
"io"
"net/http"
"os"
"runtime"
"strconv"
"time"
"github.com/bombsimon/logrusr/v3"
"github.com/go-logr/logr"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
)
const (
defaultImageDay = time.Monday
tagFormat = "20060102"
)
var (
Version = "unreleased"
BuildDate = "now"
)
func getLogger() logr.Logger {
formatter := new(logrus.TextFormatter)
formatter.DisableTimestamp = true
logger := logrus.New()
logger.SetFormatter(formatter)
return logrusr.New(logger)
}
func main() {
log := getLogger()
log.Info("App", "Version", Version, "Build Date", BuildDate)
log.Info("Go", "Version", runtime.Version(), "OS", runtime.GOOS, "ARCH", runtime.GOARCH)
r := router(log)
srv := &http.Server{
Handler: r,
WriteTimeout: 30 * time.Second,
ReadTimeout: 30 * time.Second,
Addr: ":8080",
}
err := srv.ListenAndServe()
if err != nil {
log.Error(err, "HTTP server error")
}
}
func router(log logr.Logger) *mux.Router {
r := mux.NewRouter()
h := handler{
log: log,
imageDay: imageDayFromEnv(defaultImageDay),
}
r.HandleFunc("/window/{day:[0-6]}/{hour:2[0-3]|[01][0-9]}", h.getWindow).Methods("GET")
r.HandleFunc("/alive", h.alive).Methods("GET")
return r
}
type handler struct {
log logr.Logger
imageDay time.Weekday
}
func (t *handler) getWindow(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
day, err := strconv.Atoi(vars["day"])
if err != nil {
t.error(w, fmt.Errorf("error parsing day: %w", err), http.StatusUnprocessableEntity)
return
}
hour, err := strconv.Atoi(vars["hour"])
if err != nil {
t.error(w, fmt.Errorf("error parsing hour: %w", err), http.StatusUnprocessableEntity)
return
}
currentTime := time.Now()
tag, err := calculateTag(t.imageDay, day, hour, currentTime)
if err != nil {
t.error(w, fmt.Errorf("error calculating tag: %w", err), http.StatusUnprocessableEntity)
return
}
t.log.Info("serving",
"current_time", currentTime.Format(time.RFC3339),
"requested_day", day,
"requested_hour", hour,
"tag", tag,
)
http.Redirect(w, r, "/tag/"+tag, http.StatusFound)
}
func (t *handler) alive(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err := io.WriteString(w, `{"status":"ok"}`)
if err != nil {
t.log.Error(err, "failed to write aliveness check")
}
}
func (t *handler) error(w http.ResponseWriter, err error, code int) {
t.log.Error(err, "error handling request", "code", code)
http.Error(w, err.Error(), code)
}
func calculateTag(imageDay time.Weekday, day, hour int, currentTime time.Time) (string, error) {
//this should never get hit if the call comes from the correctly configured gorilla mux
if day > 6 || day < 0 || hour > 23 || hour < 0 {
return "", fmt.Errorf("invalid day (%d) or hour (%d)", day, hour)
}
//Let's convert the diffs to the maintenance window to time.Durations, so we can easily
//calculate if we're already past that point or not.
diffDays := 24 * time.Duration((day - int(currentTime.Weekday()))) * time.Hour
diffHours := time.Duration(hour-currentTime.Hour()) * time.Hour
windowTime := currentTime.Add(diffDays + diffHours)
if currentTime.After(windowTime) || currentTime.Equal(windowTime) {
return getCurrentTag(imageDay, currentTime), nil
}
return getPreviousTag(imageDay, currentTime), nil
}
func getCurrentTag(imageDay time.Weekday, currentTime time.Time) string {
return floorToImageDay(imageDay, currentTime).Format(tagFormat)
}
// getPreviousTag returns last week's tag according to the imageDay
func getPreviousTag(imageDay time.Weekday, currentTime time.Time) string {
// If the current weekday is before the imageDay, the floor is the weekday of the previous week.
if currentTime.Weekday() < imageDay {
return getCurrentTag(imageDay, currentTime)
}
return getCurrentTag(imageDay, currentTime.AddDate(0, 0, -7))
}
func floorToImageDay(imageDay time.Weekday, date time.Time) time.Time {
for date.Weekday() != imageDay {
date = date.AddDate(0, 0, -1)
}
return date
}
// imageDayFromEnv returns the imageDay from the environment variable FG_IMAGE_DAY.
// Values are from 0-6 where Sunday=0
func imageDayFromEnv(defaultValue time.Weekday) time.Weekday {
if str, ok := os.LookupEnv("FG_IMAGE_DAY"); ok {
if d, err := strconv.Atoi(str); err != nil {
fmt.Printf("failed to parse $%s: %v", "FG_IMAGE_DAY", err)
} else if d < 0 || d > 6 {
fmt.Printf("$%s must be between 0 and 6\n", "FG_IMAGE_DAY")
} else {
return time.Weekday(d)
}
}
return defaultValue
}