-
Notifications
You must be signed in to change notification settings - Fork 8
/
engine.go
187 lines (161 loc) · 4.17 KB
/
engine.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package bytengine
import (
"encoding/json"
"errors"
"fmt"
)
type Config struct {
Authentication struct{ Plugin string }
ByteStore struct{ Plugin string }
FileSystem struct{ Plugin string }
StateStore struct{ Plugin string }
DataFilter struct{ Plugin string }
Parser struct{ Plugin string }
}
type ConfigData struct {
Authentication json.RawMessage
ByteStore json.RawMessage
FileSystem json.RawMessage
StateStore json.RawMessage
DataFilter json.RawMessage
Parser json.RawMessage
}
type Engine struct {
Authentication Authentication
FileSystem FileSystem
ByteStore ByteStore
StateStore StateStore
Parser Parser
}
func NewEngine() *Engine {
e := Engine{}
return &e
}
func (eng *Engine) checkUser(token string) (*User, error) {
// check token
if len(token) == 0 {
// anonymous user
return nil, nil
}
uname, err := eng.StateStore.TokenGet(token)
if err != nil {
return nil, errors.New("invalid auth token")
}
return eng.Authentication.UserInfo(uname)
}
func (eng *Engine) parseScript(script string) ([]Command, error) {
var cmds []Command
if len(script) == 0 {
return cmds, errors.New("empty script")
}
cmds, err := eng.Parser.Parse(script)
if err != nil {
return cmds, fmt.Errorf("script parse error:\n%s", err.Error())
}
if len(cmds) == 0 {
return cmds, errors.New("no command found")
}
return cmds, nil
}
func createAuthentication(plugin string, config []byte) (Authentication, error) {
return NewAuthentication(plugin, string(config))
}
func createByteStore(plugin string, config []byte) (ByteStore, error) {
return NewByteStore(plugin, string(config))
}
func createFileSystem(bstore *ByteStore, plugin string, config []byte) (FileSystem, error) {
return NewFileSystem(plugin, string(config), bstore)
}
func createStateStore(plugin string, config []byte) (StateStore, error) {
return NewStateStore(plugin, string(config))
}
func createParser(plugin string, config []byte) (Parser, error) {
return NewParser(plugin, string(config))
}
// start engine and configure plugins
func (eng *Engine) Start(b []byte) error {
// read configuration
config := Config{}
err := json.Unmarshal(b, &config)
if err != nil {
return err
}
configdata := ConfigData{}
err = json.Unmarshal(b, &configdata)
if err != nil {
return err
}
// get plugins from configuration
auth, err := createAuthentication(config.Authentication.Plugin, configdata.Authentication)
if err != nil {
return err
}
bytestore, err := createByteStore(config.ByteStore.Plugin, configdata.ByteStore)
if err != nil {
return err
}
filesystem, err := createFileSystem(&bytestore, config.FileSystem.Plugin, configdata.FileSystem)
if err != nil {
return err
}
statestore, err := createStateStore(config.StateStore.Plugin, configdata.StateStore)
if err != nil {
return err
}
parser, err := createParser(config.Parser.Plugin, configdata.Parser)
if err != nil {
return err
}
// setup engine
eng.Authentication = auth
eng.ByteStore = bytestore
eng.FileSystem = filesystem
eng.StateStore = statestore
eng.Parser = parser
return nil
}
// script is parsed into commands before execution
func (eng *Engine) ExecuteScript(token, script string) (interface{}, error) {
// check user
user, err := eng.checkUser(token)
// check anonymous login
if user == nil && err == nil {
return nil, errors.New("Authorization required")
}
if err != nil {
return nil, err
}
// parse script
cmdlist, err := eng.parseScript(script)
if err != nil {
return nil, err
}
// execute command(s)
resultset := []interface{}{}
for _, cmd := range cmdlist {
r, err := eng.execute(cmd, user)
if err != nil {
return nil, err
}
resultset = append(resultset, r)
}
if len(resultset) > 1 {
r := resultset
return &r, nil
}
return resultset[0], nil
}
// command is sent directly for execution
func (eng *Engine) ExecuteCommand(token string, cmd Command) (interface{}, error) {
user, err := eng.checkUser(token)
if err != nil {
return nil, err
}
// exec command
r, err := eng.execute(cmd, user)
return r, err
}
func (eng *Engine) CreateAdminUser(usr, pw string) error {
err := eng.Authentication.NewUser(usr, pw, true)
return err
}