-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
84 lines (69 loc) · 1.84 KB
/
utils.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
package essen
import (
"os"
"regexp"
"github.com/akshitgrover/essen/jobqueue"
)
const (
//Minute constant to get numerical value of time unit
Minute = 60
//Hour constant to get numerical value of time unit
Hour = 60 * Minute
//Day constant to get numerical value of time unit
Day = 24 * Hour
//Week constant to get numerical value of time unit
Week = 7 * Day
//Month constant to get numerical value of time unit
Month = 4 * Week
)
//TemplateFunc type instance is used to pass functions in go templates
type TemplateFunc map[string]interface{}
//GetTemplateFunc returns an instance of TemplateFunc type
//
// tf := essen.GetTemplateFunc()
func GetTemplateFunc() TemplateFunc {
return make(TemplateFunc)
}
//Push method is used to add function to TemplateFunc instance
func (t TemplateFunc) Push(key string, f interface{}) {
t[key] = f
}
//CreateFileIfNotExist is used to create a file if does not exists.
func CreateFileIfNotExist(path string) (*os.File, EssenError) {
ee := EssenError{nilval: true}
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
ee.nilval = false
ee.errortype = "PathError"
ee.message = err.Error()
return f, ee
}
return f, ee
}
//CreateDirIfNotExist is used to create a directory if does not exists.
func CreateDirIfNotExist(path string) EssenError {
ee := EssenError{nilval: true}
err := os.MkdirAll(path, 0777)
if err != nil {
ee.nilval = false
ee.errortype = "PathError"
ee.message = err.Error()
}
return ee
}
//SetConcurrencyLimit is used to set limit on concurrently running request handlers
func SetConcurrencyLimit(n int) {
jobqueue.SetConcurrency(n)
}
func matchStaticUrl(url string) string {
l := 0
mString := ""
for k := range paths.static {
matched, _ := regexp.MatchString("^"+k, url)
if matched && len(k) > l {
l = len(k)
mString = k
}
}
return mString
}