-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
43 lines (34 loc) · 909 Bytes
/
main.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
package crud
import (
"database/sql"
)
// Controller is the main component that gets and saves objects in the database and generates CRUD HTTP handler
// that can be attached to an HTTP server.
type Controller struct {
orm ORM
tagName string
passFunc func(string) string
}
type ControllerConfig struct {
TagName string
PasswordGenerator func(string) string
ORM ORM
}
type ContextValue string
// NewController returns new Controller object
func NewController(dbConn *sql.DB, tblPrefix string, cfg *ControllerConfig) *Controller {
c := &Controller{}
c.tagName = "crud"
if cfg != nil && cfg.TagName != "" {
c.tagName = cfg.TagName
}
if cfg != nil && cfg.PasswordGenerator != nil {
c.passFunc = cfg.PasswordGenerator
}
if cfg != nil && cfg.ORM != nil {
c.orm = cfg.ORM
} else {
c.orm = newWrappedStruct2db(dbConn, tblPrefix, c.tagName)
}
return c
}