-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
157 lines (125 loc) · 3.39 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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/foolin/goview/supports/ginview"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
)
const (
FORTUNE = "FORTUNE"
PORT = "PORT"
PARAM_NUM_FORTUNE = "count"
DEFAULT_FORTUNE_FILE = "./fortune.txt"
DEFAULT_STATIC_DIR = "./static"
DEFAULT_PORT = 3000
DEFAULT_NUM_FORTUNE = "1"
)
func loadFortunes(path string) []string {
buff, err := ioutil.ReadFile(path)
if nil != err {
log.Fatalf("Error reading %s: %v\n", path, err)
}
lines := strings.Split(string(buff), "\n")
return lines[:len(lines)-1]
}
func checkStaticAsset(path string) {
if _, err := os.Stat(path); nil != err {
log.Fatalf("Static directory '%s' error : %s ", path, err)
}
}
func defaultFortune() string {
value, present := os.LookupEnv(FORTUNE)
if present {
return value
}
return DEFAULT_FORTUNE_FILE
}
func defaultPort() (int, error) {
value, present := os.LookupEnv(PORT)
if present {
return strconv.Atoi(value)
}
return DEFAULT_PORT, nil
}
func getFortunes(fortune []string, count int) []string {
idx := rand.Perm(len(fortune))[:count]
f := make([]string, count)
for i := 0; i < count; i++ {
f[i] = fortune[idx[i]]
}
return f
}
func mkHandler(fortunes []string) func(*gin.Context) {
return func(c *gin.Context) {
count, err := strconv.Atoi(c.DefaultQuery(PARAM_NUM_FORTUNE, DEFAULT_NUM_FORTUNE))
if nil != err {
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("%v", err)})
return
}
f := getFortunes(fortunes, count)
t, _ := time.Now().MarshalText()
c.JSON(http.StatusOK, gin.H{
"timestamp": string(t),
"fortunes": f,
})
}
}
func mkMVCHandler(fortunes []string) func(*gin.Context) {
return func(c *gin.Context) {
f := getFortunes(fortunes, 1)
c.HTML(http.StatusOK, "index", gin.H{ "fortuneText": f[0] })
}
}
func healthz(c *gin.Context) {
t, _ := time.Now().MarshalText()
c.JSON(http.StatusOK, gin.H{"timestamp": string(t)})
}
func notFound(c *gin.Context) {
if strings.Contains(c.GetHeader("Accept"), "text/html") {
c.Redirect(http.StatusPermanentRedirect, "/static/404.html")
return
}
t, _ := time.Now().MarshalText()
c.JSON(http.StatusNotFound, gin.H{
"timestamp": string(t),
"error": fmt.Sprintf("Resource not found: %s", c.Request.URL.String()),
})
}
func main() {
var fortuneFile string
var port int
var staticDir string
defPort, err := defaultPort()
if nil != err {
log.Fatalf("Error: %v", err)
}
flag.StringVar(&fortuneFile, "fortune", defaultFortune(), "Fortune file")
flag.IntVar(&port, "port", defPort, "port")
flag.StringVar(&staticDir, "static", DEFAULT_STATIC_DIR, "Static resources directory")
flag.Parse()
log.Printf("fortune file: %s, static directory: %s, port: %d", fortuneFile, staticDir, port)
checkStaticAsset(staticDir)
fortunes := loadFortunes(fortuneFile)
log.Printf("Loaded %s fortunes file\n", fortuneFile)
rand.Seed(time.Now().UnixNano())
r := gin.Default()
r.HTMLRender = ginview.Default()
r.GET("/", mkMVCHandler(fortunes))
r.GET("/api/fortune", mkHandler(fortunes))
r.GET("/healthz", healthz)
r.Use(static.Serve("/static", static.LocalFile(staticDir, true)))
r.Use(notFound)
log.Printf("Starting server on port %d\n", port)
if err := r.Run(fmt.Sprintf("0.0.0.0:%d", port)); nil != err {
log.Panicf("Cannot start server. %v\n", err)
}
}