-
-
Notifications
You must be signed in to change notification settings - Fork 354
/
server.go
170 lines (134 loc) · 3.62 KB
/
server.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
// +build !js
package web
import (
"bytes"
"context"
"html/template"
"net/http"
"os"
"path"
"github.com/murlokswarm/app/appjs"
"github.com/murlokswarm/app"
"github.com/murlokswarm/app/html"
)
func init() {
app.Loggers = []app.Logger{
app.NewLogger(os.Stdout, os.Stderr, true, true),
}
}
// Run satisfies the app.Driver interface.
func (d *Driver) Run(f app.Factory) error {
d.factory = f
if len(d.NotFoundURL) == 0 {
d.NotFoundURL = "web.NotFound"
}
if d.Server == nil {
d.Server = &http.Server{
Addr: ":7042",
}
}
http.Handle("/", d)
fileHandler := http.FileServer(http.Dir("resources"))
fileHandler = http.StripPrefix("/resources/", fileHandler)
fileHandler = newGzipHandler(fileHandler)
http.Handle("/resources/", fileHandler)
if d.OnServerRun != nil {
d.OnServerRun()
}
errC := make(chan error)
go func() {
err := d.Server.ListenAndServe()
if err == http.ErrServerClosed {
err = nil
}
errC <- err
}()
return <-errC
}
// ServeHTTP is the http.Handler that route wether to serve a page or a
// resource.
func (d *Driver) ServeHTTP(res http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/" || len(req.URL.Path) == 0 {
req.URL.Path = d.DefaultURL
}
if compoName := app.ComponentNameFromURL(req.URL); d.factory.Registered(compoName) {
d.handleComponent(res, req)
return
}
req.URL.Path = d.NotFoundURL
d.handleNotFound(res, req)
}
func (d *Driver) handleComponent(res http.ResponseWriter, req *http.Request) {
compo, err := d.factory.New(app.ComponentNameFromURL(req.URL))
if err != nil {
http.NotFound(res, req)
return
}
var config html.PageConfig
if page, ok := compo.(html.Page); ok {
config = page.PageConfig()
}
if len(config.CSS) == 0 {
config.CSS = app.CSSResources()
}
config.Javascripts = append(config.Javascripts, d.Resources("goapp.js"))
config.AppJS = appjs.AppJS("console.log")
page := html.NewPage(config)
res.WriteHeader(http.StatusOK)
res.Write([]byte(page))
}
func (d *Driver) handleNotFound(res http.ResponseWriter, req *http.Request) {
compo, err := d.factory.New(app.ComponentNameFromURL(req.URL))
if err != nil {
http.NotFound(res, req)
return
}
markup := html.NewMarkup(d.factory)
var root app.Tag
if root, err = markup.Mount(compo); err != nil {
http.NotFound(res, req)
return
}
var b bytes.Buffer
enc := html.NewEncoder(&b, markup, false)
if err = enc.Encode(root); err != nil {
http.NotFound(res, req)
return
}
page := html.NewPage(html.PageConfig{
Title: "not found",
DefaultComponent: template.HTML(b.String()),
})
res.WriteHeader(http.StatusNotFound)
res.Write([]byte(page))
}
// AppName satisfies the app.Driver interface.
func (d *Driver) AppName() string {
return "goapp"
}
// Resources satisfies the app.Driver interface.
func (d *Driver) Resources(p ...string) string {
resources := path.Join(p...)
resources = path.Join("resources", resources)
return resources
}
// Storage satisfies the app.Driver interface.
func (d *Driver) Storage(p ...string) string {
return ""
}
// Render satisfies the app.Driver interface.
func (d *Driver) Render(c app.Component) error {
return app.NewErrNotSupported("render")
}
// ElementByComponent satisfies the app.Driver interface.
func (d *Driver) ElementByComponent(c app.Component) (app.ElementWithComponent, error) {
return nil, app.NewErrNotSupported("element by component")
}
// CallOnUIGoroutine satisfies the app.Driver interface.
func (d *Driver) CallOnUIGoroutine(f func()) {
app.Log("CallOnUIGoroutine is not supported on server side")
}
// Close shutdown the server.
func (d *Driver) Close() error {
return d.Server.Shutdown(context.Background())
}