-
Notifications
You must be signed in to change notification settings - Fork 49
/
main.go
51 lines (42 loc) · 1.43 KB
/
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
44
45
46
47
48
49
50
51
package api
import (
"net/http"
"github.com/darkweak/souin/configurationtypes"
"github.com/darkweak/souin/pkg/api/debug"
"github.com/darkweak/souin/pkg/api/prometheus"
"github.com/darkweak/souin/pkg/storage/types"
"github.com/darkweak/souin/pkg/surrogate/providers"
)
// MapHandler is a map to store the available http Handlers
type MapHandler struct {
Handlers *map[string]http.HandlerFunc
}
// GenerateHandlerMap generate the MapHandler
func GenerateHandlerMap(
configuration configurationtypes.AbstractConfigurationInterface,
storers []types.Storer,
surrogateStorage providers.SurrogateInterface,
) *MapHandler {
hm := make(map[string]http.HandlerFunc)
shouldEnable := false
souinAPI := configuration.GetAPI()
basePathAPIS := souinAPI.BasePath
if basePathAPIS == "" {
basePathAPIS = "/souin-api"
}
for _, endpoint := range Initialize(configuration, storers, surrogateStorage) {
if endpoint.IsEnabled() {
shouldEnable = true
hm[basePathAPIS+endpoint.GetBasePath()] = endpoint.HandleRequest
}
}
if shouldEnable {
return &MapHandler{Handlers: &hm}
}
return nil
}
// Initialize contains all apis that should be enabled
func Initialize(c configurationtypes.AbstractConfigurationInterface, storers []types.Storer, surrogateStorage providers.SurrogateInterface) []EndpointInterface {
return []EndpointInterface{initializeSouin(c, storers,
surrogateStorage), debug.InitializeDebug(c), prometheus.InitializePrometheus(c)}
}