generated from Computroniks/template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (66 loc) · 1.99 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
// SPDX-FileCopyrightText: 2023 Matthew Nickson <[email protected]>
// SPDX-License-Identifier: MIT
package main
import (
"log"
"strconv"
"github.com/Computroniks/asset-tags/handler"
"github.com/Computroniks/asset-tags/router"
"github.com/Computroniks/asset-tags/store/mysql"
"github.com/Computroniks/asset-tags/templates"
"github.com/Computroniks/asset-tags/util"
"github.com/joho/godotenv"
)
func init() {
log.Println("Fetching environment variables")
var err error
err = godotenv.Load()
if err != nil {
log.Println("Failed to load .env file")
}
util.BindAddr = util.Getenv(util.BindAddrEnv, util.DefaultBindAddr)
util.DatabaseAddr = util.Mustgetenv(util.DatabaseAddrEnv)
util.DatabaseUsr = util.Mustgetenv(util.DatabaseUsrEnv)
util.DatabasePwd = util.Mustgetenv(util.DatabasePwdEnv)
util.DatabaseName = util.Mustgetenv(util.DatabaseNameEnv)
util.DatabaseTimeout = util.Getenv(util.DatabaseTimeoutEnv, util.DefaultDBTimeout)
util.BasePath = util.Getenv(util.BasePathEnv, util.DefaultBasePath)
util.TagLength, err = strconv.Atoi(
util.Getenv(
util.TagLengthEnv,
strconv.Itoa(util.DefaultTagLength),
),
)
if err != nil {
log.Println("Failed to read tag length:", err)
log.Println("Using default tag length of", util.DefaultTagLength)
util.TagLength = util.DefaultTagLength
}
log.Println("Initialising templates")
util.Views = *templates.New()
}
func main() {
var err error
util.DB, err = mysql.New(
util.DatabaseAddr,
util.DatabaseUsr,
util.DatabasePwd,
util.DatabaseName,
util.DatabaseTimeout,
)
if err != nil {
log.Fatalln(err)
}
defer util.DB.Close()
app := router.New(util.BasePath)
app.GET("/", handler.Index)
app.GET("/settings", handler.Settings)
app.GET("/api/tag", handler.GetTag)
app.POST("/api/tag", handler.IncrementTag)
app.GET("/api/prefix", handler.GetPrefixes)
app.POST("/api/prefix", handler.AddPrefix)
app.Error(400, handler.HTTP400)
app.Error(404, handler.HTTP404)
app.Error(500, handler.HTTP500)
app.Start(util.BindAddr)
}