forked from maxence-charriere/go-app
/
handler.go
147 lines (119 loc) · 3.44 KB
/
handler.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
// +build !wasm
package app
import (
"net/http"
"strings"
"sync"
apphttp "github.com/maxence-charriere/app/internal/http"
)
// ProgressiveAppConfig represents the configuration used to describe a
// progressive app.
type ProgressiveAppConfig struct {
// Enforces landscape mode.
LanscapeMode bool
// Provides a short human-readable name for the application. This is
// intended for when there is insufficient space to display the full name of
// the web application, like device homescreens.
//
// Default is the app name where space are replaces by '-'.
ShortName string
// Defines the navigation scope of this website's context. This restricts
// what web pages can be viewed while the manifest is applied. If the user
// navigates outside the scope, it returns to a normal web page inside a
// browser tab/window.
//
// Default is "/".
Scope string
// The URL that loads when a user launches the application (e.g. when added
// to home screen), typically the index.
// Default is "/".
StartURL string
// Defines the default theme color for an application. This sometimes
// affects how the OS displays the site (e.g., on Android's task switcher,
// the theme color surrounds the site).
ThemeColor string
}
// Handler is a http handler that serves UI components created with this
// package.
type Handler struct {
http.Handler
// The app author.
Author string
// The app description.
Description string
// The app keywords.
Keywords []string
// The text displayed while loading a page.
LoadingLabel string
// The app name.
Name string
// The progressive app mode configuration.
ProgressiveApp ProgressiveAppConfig
// The he path of the web directory. Default is web.
WebDir string
once sync.Once
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.once.Do(h.init)
h.Handler.ServeHTTP(w, r)
}
func (h *Handler) init() {
webDir := h.WebDir
if webDir == "" {
webDir = "web"
}
files := apphttp.FileHandler(webDir)
files = apphttp.GzipHandler(files, webDir)
files = apphttp.CacheHandler(files, webDir)
themeColor := h.ProgressiveApp.ThemeColor
if themeColor == "" {
themeColor = "#21252b"
}
var pages http.Handler = &apphttp.PageHandler{
Author: h.Author,
Description: h.Description,
Keywords: h.Keywords,
LoadingLabel: h.LoadingLabel,
Name: h.Name,
ThemeColor: themeColor,
WebDir: webDir,
}
pages = apphttp.CacheHandler(pages, webDir)
var manifest http.Handler = &apphttp.ManifestHandler{
BackgroundColor: themeColor,
Name: h.Name,
Orientation: orientation(h.ProgressiveApp.LanscapeMode),
ShortName: shortName(h.Name, h.ProgressiveApp.ShortName),
Scope: entryPoint(h.ProgressiveApp.Scope),
StartURL: entryPoint(h.ProgressiveApp.StartURL),
ThemeColor: themeColor,
}
manifest = apphttp.CacheHandler(manifest, webDir)
h.Handler = &apphttp.RouteHandler{
Files: files,
Manifest: manifest,
Pages: pages,
WebDir: webDir,
}
}
func shortName(name, shortName string) string {
if shortName != "" {
return shortName
}
shortName = strings.Replace(name, " ", "", -1)
shortName = strings.Replace(shortName, "\n", "", -1)
shortName = strings.Replace(shortName, "\t", "", -1)
return shortName
}
func orientation(landscapeMode bool) string {
if landscapeMode {
return "landscape"
}
return "any"
}
func entryPoint(entryPoint string) string {
if entryPoint == "" {
return "/"
}
return entryPoint
}