-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.go
75 lines (63 loc) · 1.41 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
package main
import (
"crypto/sha256"
"encoding/hex"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
router.LoadHTMLGlob("templates/*")
router.GET("/", func(c *gin.Context) {
sha := getCommitSha()
color := getColor(sha)
textColor := getTextColor(color)
environment := getEnvironment()
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"sha": sha,
"color": color,
"textColor": textColor,
"environment": environment,
})
})
router.GET("/favicon.ico", func(c *gin.Context) {
c.File("./static/favicon.ico")
})
router.Run()
}
func getCommitSha() string {
content, err := ioutil.ReadFile("sha.txt")
if err != nil {
log.Fatal(err)
}
return strings.TrimSpace(string(content))
}
func getColor(sha string) string {
h := sha256.New()
h.Write([]byte(sha))
hash := hex.EncodeToString(h.Sum(nil))
return "#" + hash[:6]
}
func getTextColor(backgroundColor string) string {
r, _ := strconv.ParseInt(backgroundColor[1:3], 16, 64)
g, _ := strconv.ParseInt(backgroundColor[3:5], 16, 64)
b, _ := strconv.ParseInt(backgroundColor[5:7], 16, 64)
brightness := (r*299 + g*587 + b*114) / 1000
if brightness > 155 {
return "#000000"
} else {
return "#FFFFFF"
}
}
func getEnvironment() string {
environment := os.Getenv("ENVIRONMENT")
if environment == "" {
environment = "development"
}
return environment
}