-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
134 lines (114 loc) · 3.05 KB
/
util.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
package main
import (
"database/sql"
"errors"
"html/template"
"net"
"net/url"
"os"
"strings"
"time"
dat "github.com/nerdynz/dat/dat"
runner "github.com/nerdynz/dat/sqlx-runner"
"github.com/jaybeecave/render"
)
func getRenderer() *render.Render {
tmplDir := os.Getenv("templates_dir")
if tmplDir == "" {
tmplDir = "./blueprints"
}
dir, _ := os.Getwd()
if strings.HasPrefix(tmplDir, "./") {
tmplDir = dir + "/" + strings.TrimPrefix(tmplDir, "./")
}
r := render.New(render.Options{
Directory: tmplDir,
Funcs: []template.FuncMap{
template.FuncMap{
"jsesc": toJS,
"nextIndex": nextIndex,
},
},
})
return r
}
func getDBConnection() *runner.DB {
//get url from ENV in the following format postgres://user:[email protected]:5432/spaceio")
dbURL := os.Getenv("DATABASE_URL")
u, err := url.Parse(dbURL)
if err != nil {
panic(err)
}
username := u.User.Username()
pass, isPassSet := u.User.Password()
if !isPassSet {
panic("no database password")
}
host, port, _ := net.SplitHostPort(u.Host)
dbName := strings.Replace(u.Path, "/", "", 1)
db, _ := sql.Open("postgres", "dbname="+dbName+" user="+username+" password="+pass+" host="+host+" port="+port+" sslmode=disable")
err = db.Ping()
if err != nil {
panic(err)
}
// ensures the database can be pinged with an exponential backoff (15 min)
runner.MustPing(db)
// set to reasonable values for production
db.SetMaxIdleConns(4)
db.SetMaxOpenConns(16)
// set this to enable interpolation
dat.EnableInterpolation = true
// set to check things like sessions closing.
// Should be disabled in production/release builds.
dat.Strict = false
// Log any query over 10ms as warnings. (optional)
runner.LogQueriesThreshold = 10 * time.Millisecond
// db connection
return runner.NewDB(db, "postgres")
}
// for storing variables when running the templates
type viewBucket struct {
Data map[string]interface{}
}
func newViewBucket() *viewBucket {
return &viewBucket{Data: map[string]interface{}{
"LTEqStr": template.HTML(`<=`),
"GTEqStr": template.HTML(`>=`),
"LTStr": template.HTML(`<`),
"GTStr": template.HTML(`>`),
"LTEq": template.HTML(`<=`),
"GTEq": template.HTML(`>=`),
"LT": template.HTML(`<`),
"GT": template.HTML(`>`),
"LEFT_BRACE": template.JS(`{`),
"RIGHT_BRACE": template.JS(`}`),
}}
}
func toJS(s string) template.JS {
return template.JS(s)
}
func nextIndex(i int) int {
return i + 1
}
func (viewBucket *viewBucket) add(key string, value interface{}) {
viewBucket.Data[key] = value
}
func (viewBucket *viewBucket) getStrSafe(key string) (string, error) {
val := viewBucket.Data[key]
if val == nil {
return "", errors.New("could not find " + key)
}
strVal, ok := val.(string)
if !ok {
return "", errors.New("could not cast " + key + " to string")
}
return strVal, nil
}
// getStr - returns a string for the provided key. Will panic if key not found
func (viewBucket *viewBucket) getStr(key string) string {
val, err := viewBucket.getStrSafe(key)
if err != nil {
panic(err)
}
return val
}