This repository was archived by the owner on Apr 19, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.go
More file actions
109 lines (91 loc) · 3.35 KB
/
Copy pathplugin.go
File metadata and controls
109 lines (91 loc) · 3.35 KB
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
package apidCRUD
import (
"strconv"
"net/http"
"github.com/apid/apid-core"
)
// ----- narrowed-down versions of apid service interfaces
// these narrowed interfaces make testing easier,
// by making it easier to hand craft a simple mock interface
// that can be used as an argument to pieces of code under test.
// handleFuncer interface provides the HandleFunc() method.
// narrowed from apid.APISerivce.
type handleFuncer interface {
HandleFunc(path string, hf http.HandlerFunc) apid.Route
}
// forModuler interface proviees the ForModule() method.
// narrowed from apid.LogService.
type forModuler interface {
ForModule(name string) apid.LogService
}
// ----- functions go below this line
// initPlugin() is called by the apid InitializePlugins().
// just calls realInitPlugin() which has been designed to simplify unit testing.
func initPlugin(services apid.Services) (apid.PluginData, error) {
return realInitPlugin(services.Config(), services.Log(), services.API())
}
// realInitPlugin() drives miscellaneous plugin-specific setup activities,
// then returns apidCRUD's pluginData.
// reads in the plugin-specific configuration data.
// sets the log variable.
// sets the db variable.
// registers the API handlers.
func realInitPlugin(gsi getStringer,
fmi forModuler,
hfi handleFuncer) (apid.PluginData, error) {
initConfig(gsi)
log = fmi.ForModule(pluginData.Name) // NOTE: non-local var
registerHandlers(hfi, apiTable)
log.Infof("in apidCRUD realInitPlugin")
var err error
db, err = initDB(dbName) // NOTE: non-local var
return pluginData, err
}
// registerHandlers() register all our handlers with the given service.
func registerHandlers(service handleFuncer, tab []apiDesc) {
ws := newApiWiring(basePath, tab)
maps := ws.GetMaps()
for path, vmap := range maps {
addPath(service, path, vmap)
}
}
// addPath() registers the given path with the given service,
// so that it will be handled indirectly by pathDispatch().
// when an API call is made on this path, the vmap argument from
// this context will be suppllied, along with the w and r arguments
// passed in by the service framework.
func addPath(service handleFuncer, path string, vmap verbMap) {
service.HandleFunc(path,
func(w http.ResponseWriter, r *http.Request) {
pathDispatch(vmap, w, mkApiHandlerArg(r, getPathParams(r)))
})
}
// getPathParams() returns a map of path params from the given http request.
func getPathParams(req *http.Request) map[string]string {
return apid.API().Vars(req)
}
// ----- configuration related functions
// getStringer is an interface that supports GetString().
// narrowed from apid.ConfigService.
type getStringer interface {
GetString(vname string) string
}
// confGet() returns the config value of the named string,
// or if there is no configured value, the given default value.
func confGet(gsi getStringer, vname string, defval string) string {
ret := gsi.GetString(vname)
if ret == "" {
return defval
}
return ret
}
// initConfig() sets up some global configuration parameters for this plugin.
func initConfig(gsi getStringer) {
aMaxRecs := strconv.Itoa(maxRecs)
// these are all global assignments!
dbDriver = confGet(gsi, "apidCRUD_db_driver", dbDriver)
dbName = confGet(gsi, "apidCRUD_db_name", dbName)
basePath = confGet(gsi, "apidCRUD_base_path", basePath)
maxRecs, _ = strconv.Atoi( // nolint
confGet(gsi, "apidCRUD_max_recs", aMaxRecs))
}