-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
125 lines (98 loc) · 3.05 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
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
package hardwire
import (
"fmt"
"os"
"path"
echo "github.com/labstack/echo/v4"
config "github.com/ncpa0/hardwire/configuration"
hw "github.com/ncpa0/hardwire/hw-context"
resources "github.com/ncpa0/hardwire/resources"
servestatic "github.com/ncpa0/hardwire/serve-static"
"github.com/ncpa0/hardwire/views"
)
type DynamicRequestContext = resources.DynamicRequestContext
type ResourceRequestError = resources.ResourceRequestError
type ResourceRegistry = resources.ResourceRegistry
type ActionContext = resources.ActionContext
type Configuration = config.Configuration
type CachingConfig = config.CachingConfig
type CachingPolicy = config.CachingPolicy
var ResourceReg = resources.ResourceReg
var Configure = config.Configure
var HardwireContext hw.HardwireContext = &HwContext{}
func redirectHandler(to string) echo.HandlerFunc {
return func(ctx echo.Context) error {
return ctx.Redirect(301, to)
}
}
func validateResourcesAvailable(resource []string) error {
for _, resKey := range resource {
_, found := resources.GetResource(resKey)
if !found {
return fmt.Errorf("'%s' resource doesn't have a provider registered", resKey)
}
}
return nil
}
// Builds the HTML and templates for all pages and adds the routes to the server
func UseWith(server *echo.Echo) error {
pageViewRegistry := views.GetPageViewRegistry()
dynamicFragmentViewRegistry := views.GetDynamicFragmentViewRegistry()
wd, err := os.Getwd()
if err != nil {
return err
}
err = views.LoadViews(wd)
if err != nil {
return err
}
resources.ValidateActionEndpoints()
err = pageViewRegistry.ForEach(func(view *views.PageView) error {
fmt.Printf("Adding new route: %s\n", view.GetRoutePathname())
if view.IsDynamic() {
err := validateResourcesAvailable(view.GetResourceKeys().ToSlice())
if err != nil {
return err
}
}
pathname := view.GetRoutePathname()
server.GET(pathname, createPageViewHandler(view, config.Current))
server.GET(pathname+"/", redirectHandler(pathname))
return nil
})
if err != nil {
return err
}
err = dynamicFragmentViewRegistry.ForEach(func(view *views.DynamicFragmentView) error {
if config.Current.DebugMode {
fmt.Printf("Adding new dynamic fragment under route: %s\n", view.GetRoutePathname())
}
resKey := view.ResourceKeys()[0]
err := validateResourcesAvailable([]string{resKey})
if err != nil {
return err
}
pathname := view.GetRoutePathname()
server.GET(pathname, createDynamicFragmentHandler(view, config.Current))
server.GET(pathname+"/", redirectHandler(pathname))
return nil
})
if err != nil {
return err
}
resources.MountActionEndpoints(HardwireContext, server)
if config.Current.DebugMode {
fmt.Printf(
"Serving static files at the following URL: %s from directory: %s\n",
config.Current.StaticURL, config.Current.StaticDir,
)
}
staticDir := config.Current.StaticDir
if !path.IsAbs(staticDir) {
staticDir = path.Join(wd, staticDir)
}
servestatic.Serve(server, config.Current.StaticURL, staticDir, &servestatic.Configuration{
BeforeSend: config.Current.BeforeStaticResponse,
})
return nil
}