forked from leeprovoost/go-rest-api-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
100 lines (91 loc) · 2.82 KB
/
helpers.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
package main
import (
"encoding/json"
"io/ioutil"
"regexp"
"strings"
"time"
"github.com/palantir/stacktrace"
"github.com/unrolled/render"
)
// AppContext holds application configuration data
type AppContext struct {
Render *render.Render
Version string
Env string
Port string
DB DataStorer
}
// Healthcheck will store information about its name and version
type Healthcheck struct {
AppName string `json:"appName"`
Version string `json:"version"`
}
// Status is a custom response object we pass around the system and send back to the customer
// 404: Not found
// 500: Internal Server Error
type Status struct {
Status string `json:"status"`
Message string `json:"message"`
}
// CreateContextForTestSetup initialises an application context struct
// for testing purposes
func CreateContextForTestSetup() AppContext {
testVersion := "0.0.0"
db := CreateMockDatabase()
ctx := AppContext{
Render: render.New(),
Version: testVersion,
Env: local,
Port: "3001",
DB: db,
}
return ctx
}
// CreateMockDatabase initialises a database for test purposes
func CreateMockDatabase() *MockDB {
list := make(map[int]User)
dt, _ := time.Parse(time.RFC3339, "1985-12-31T00:00:00Z")
list[0] = User{0, "John", "Doe", dt, "London"}
dt, _ = time.Parse(time.RFC3339, "1992-01-01T00:00:00Z")
list[1] = User{1, "Jane", "Doe", dt, "Milton Keynes"}
return &MockDB{list, 1}
}
// LoadFixturesIntoMockDatabase loads data from fixtures file into MockDB
func LoadFixturesIntoMockDatabase(fixturesFile string) (*MockDB, error) {
var jsonObject map[string][]User
file, err := ioutil.ReadFile(fixturesFile)
if err != nil {
return nil, stacktrace.Propagate(err, "error reading fixtures file")
}
err = json.Unmarshal(file, &jsonObject)
if err != nil {
return nil, stacktrace.Propagate(err, "error parsing fixtures file")
}
list := make(map[int]User)
list[0] = jsonObject["users"][0]
list[1] = jsonObject["users"][1]
return &MockDB{
UserList: list,
MaxUserID: 1,
}, nil
}
// ParseVersionFile returns the version as a string, parsing and validating a file given the path
func ParseVersionFile(versionPath string) (string, error) {
dat, err := ioutil.ReadFile(versionPath)
if err != nil {
return "", stacktrace.Propagate(err, "error reading version file")
}
version := string(dat)
version = strings.Trim(strings.Trim(version, "\n"), " ")
// regex pulled from official https://github.com/sindresorhus/semver-regex
semverRegex := `^v?(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)(?:-[\da-z\-]+(?:\.[\da-z\-]+)*)?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?$`
match, err := regexp.MatchString(semverRegex, version)
if err != nil {
return "", stacktrace.Propagate(err, "error executing regex match")
}
if !match {
return "", stacktrace.NewError("string in VERSION is not a valid version number")
}
return version, nil
}