forked from globocom/secDevLabs
/
server.go
143 lines (117 loc) · 3.33 KB
/
server.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
package main
import (
"errors"
"fmt"
"html/template"
"io"
"log"
"os"
"time"
"github.com/globocom/secDevLabs/owasp-top10-2017-apps/a1/copy-n-paste/app/handlers"
"github.com/globocom/secDevLabs/owasp-top10-2017-apps/a1/copy-n-paste/app/util"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/spf13/viper"
)
// TemplateRegistry defines the template registry struct
// Ref: https://medium.freecodecamp.org/how-to-setup-a-nested-html-template-in-the-go-echo-web-framework-670f16244bb4
type TemplateRegistry struct {
templates map[string]*template.Template
}
// Render implement e.Renderer interface
// Ref: https://medium.freecodecamp.org/how-to-setup-a-nested-html-template-in-the-go-echo-web-framework-670f16244bb4
func (t *TemplateRegistry) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
tmpl, ok := t.templates[name]
if !ok {
err := errors.New("Template not found -> " + name)
return err
}
return tmpl.ExecuteTemplate(w, "base.html", data)
}
func main() {
err := loadViper()
if err != nil {
log.Fatal(err)
}
if err := checkAPIrequirements(); err != nil {
fmt.Println("[x] Error starting API:")
fmt.Println("[x]", err)
os.Exit(1)
}
echoInstance := echo.New()
echoInstance.HideBanner = true
// Instantiate a template registry with an array of template set
// Ref: https://medium.freecodecamp.org/how-to-setup-a-nested-html-template-in-the-go-echo-web-framework-670f16244bb4
templates := make(map[string]*template.Template)
templates["form.html"] = template.Must(template.ParseFiles("app/views/form.html", "app/views/base.html"))
echoInstance.Renderer = &TemplateRegistry{
templates: templates,
}
echoInstance.Use(middleware.Logger())
echoInstance.Use(middleware.Recover())
echoInstance.Use(middleware.RequestID())
echoInstance.GET("/", handlers.PageLogin)
echoInstance.POST("/login", handlers.Login)
echoInstance.POST("/register", handlers.Register)
echoInstance.GET("/healthcheck", handlers.HealthCheck)
echoInstance.Logger.Fatal(echoInstance.Start(":10001"))
}
func checkAPIrequirements() error {
if err := checkEnvVars(); err != nil {
return err
}
fmt.Println("[*] Environment Variables: OK!")
if err := initDB(); err != nil {
return err
}
fmt.Println("[*] MySQL: Init DB OK!")
return nil
}
func loadViper() error {
viper.SetConfigName("config")
viper.AddConfigPath("./app/")
err := viper.ReadInConfig()
return err
}
func initDB() error {
timeout := time.After(1 * time.Minute)
retryTick := time.Tick(15 * time.Second)
fmt.Println("[*] Initiating DB...")
for {
select {
case <-timeout:
return errors.New("Error InitDB: timed out")
case <-retryTick:
err := util.InitDatabase()
if err != nil {
fmt.Println("Error InitDB: not ready yet")
} else {
return nil
}
}
}
}
func checkEnvVars() error {
var envIsSet bool
var allEnvIsSet bool
var errorString string
envVars := []string{
"MYSQL_USER",
"MYSQL_PASSWORD",
"MYSQL_DATABASE",
}
env := make(map[string]string)
allEnvIsSet = true
for i := 0; i < len(envVars); i++ {
env[envVars[i]], envIsSet = os.LookupEnv(envVars[i])
if !envIsSet {
errorString = errorString + envVars[i] + " "
allEnvIsSet = false
}
}
if allEnvIsSet == false {
finalError := fmt.Sprintf("check environment variables: %s", errorString)
return errors.New(finalError)
}
return nil
}