-
Notifications
You must be signed in to change notification settings - Fork 3
/
app_tmpl.go
119 lines (99 loc) · 3.32 KB
/
app_tmpl.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
package main
import (
"strings"
"text/template"
"aahframework.org/essentials.v0"
)
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// appTmplData and its methods
//______________________________________________________________________________
const (
typeWeb = "web"
typeAPI = "api"
typeWebSocket = "websocket"
storeCookie = "cookie"
storeFile = "file"
aahTmplExt = ".atmpl"
authForm = "form"
authBasic = "basic"
authOAuth2 = "oauth2"
authGeneric = "generic"
authNone = "none"
basicFileRealm = "file-realm"
)
// appTmplData struct holds inputs collected from user for new aah creation
type appTmplData struct {
Name string
Type string
ImportPath string
BaseDir string
ViewEngine string
ViewFileExt string
AuthScheme string
BasicAuthMode string
PasswordEncoderAlgo string
SessionStore string
SessionFileStorePath string
BasicAuthFileRealmPath string
CORSEnable bool
TmplDelimLeft string
TmplDelimRight string
SubTypes []string
}
func (a *appTmplData) IsWebApp() bool {
return a.Type == typeWeb
}
func (a *appTmplData) IsAPIApp() bool {
return a.Type == typeAPI
}
func (a *appTmplData) IsWebSocketApp() bool {
return a.Type == typeWebSocket
}
func (a *appTmplData) DomainNameKey() string {
return strings.Replace(strings.Replace(a.Name, " ", "_", -1), "-", "_", -1)
}
func (a *appTmplData) IsAuthSchemeForWeb() bool {
return a.Type == typeWeb && (a.AuthScheme == authForm || a.AuthScheme == authBasic)
}
func (a *appTmplData) IsAuthSchemeForAPI() bool {
return a.Type == typeAPI && (a.AuthScheme == authGeneric || a.AuthScheme == authBasic)
}
func (a *appTmplData) IsSecurityEnabled() bool {
return !ess.IsStrEmpty(a.AuthScheme)
}
func (a *appTmplData) IsSubTypeAPI() bool {
return a.checkSubType(typeAPI)
}
func (a *appTmplData) IsSubTypeWebSocket() bool {
return a.checkSubType(typeWebSocket)
}
func (a *appTmplData) IsSessionConfigRequired() bool {
return a.AuthScheme == authForm || a.AuthScheme == authOAuth2 || a.AuthScheme == authBasic
}
func (a *appTmplData) IsAuth(name string) bool {
return strings.Contains(a.AuthScheme, name)
}
func (a *appTmplData) checkSubType(t string) bool {
for _, v := range a.SubTypes {
if v == t {
return true
}
}
return false
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Template funcs
//______________________________________________________________________________
var vreplace = strings.NewReplacer("_auth", "", "auth_", "")
var appTemplateFuncs = template.FuncMap{
"securerandomstring": func(length int) string {
return ess.SecureRandomString(length)
},
"variablename": func(v string) string {
return toLowerCamelCase(vreplace.Replace(v))
},
"isauth": func(args map[string]interface{}, name string) bool {
app := args["App"].(*appTmplData)
return app.IsAuth(name)
},
}