Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Progressive Web App support #294

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ gotty: main.go server/*.go webtty/*.go backend/*.go Makefile
godep go build ${BUILD_OPTIONS}

.PHONY: asset
asset: bindata/static/js/gotty-bundle.js bindata/static/index.html bindata/static/favicon.png bindata/static/css/index.css bindata/static/css/xterm.css bindata/static/css/xterm_customize.css
asset: bindata/static/js/gotty-bundle.js bindata/static/index.html bindata/static/favicon.png bindata/static/css/index.css bindata/static/css/xterm.css bindata/static/css/xterm_customize.css bindata/static/manifest.json bindata/static/icon_192.png
go-bindata -prefix bindata -pkg server -ignore=\\.gitkeep -o server/asset.go bindata/...
gofmt -w server/asset.go

Expand All @@ -23,9 +23,15 @@ bindata/static: bindata
bindata/static/index.html: bindata/static resources/index.html
cp resources/index.html bindata/static/index.html

bindata/static/manifest.json: bindata/static resources/manifest.json
cp resources/manifest.json bindata/static/manifest.json

bindata/static/favicon.png: bindata/static resources/favicon.png
cp resources/favicon.png bindata/static/favicon.png

bindata/static/icon_192.png: bindata/static resources/icon_192.png
cp resources/icon_192.png bindata/static/icon_192.png

bindata/static/js: bindata/static
mkdir -p bindata/static/js

Expand Down
Binary file added resources/icon_192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions resources/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<html>
<head>
<title>{{ .title }}</title>
<link rel="manifest" href="manifest.json">
<link rel="icon" type="image/png" href="favicon.png">
<link rel="stylesheet" href="./css/index.css" />
<link rel="stylesheet" href="./css/xterm.css" />
Expand Down
15 changes: 15 additions & 0 deletions resources/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"short_name": "{{ .title }}",
"name": "{{ .title }}",
"start_url": "./",
"icons": [
{
"src": "./icon_192.png",
"type": "image/png",
"sizes": "192x192"
}
],
"display": "minimal-ui",
"theme_color": "#000000",
"background_color": "#000000"
}
90 changes: 61 additions & 29 deletions server/asset.go

Large diffs are not rendered by default.

47 changes: 36 additions & 11 deletions server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,40 @@ func (server *Server) processWSConn(ctx context.Context, conn *websocket.Conn) e
}

func (server *Server) handleIndex(w http.ResponseWriter, r *http.Request) {
indexVars, err := server.indexVariables(r)
if err != nil {
http.Error(w, "Internal Server Error", 500)
return
}

indexBuf := new(bytes.Buffer)
err = server.indexTemplate.Execute(indexBuf, indexVars)
if err != nil {
http.Error(w, "Internal Server Error", 500)
return
}

w.Write(indexBuf.Bytes())
}

func (server *Server) handleManifest(w http.ResponseWriter, r *http.Request) {
indexVars, err := server.indexVariables(r)
if err != nil {
http.Error(w, "Internal Server Error", 500)
return
}

indexBuf := new(bytes.Buffer)
err = server.manifestTemplate.Execute(indexBuf, indexVars)
if err != nil {
http.Error(w, "Internal Server Error", 500)
return
}

w.Write(indexBuf.Bytes())
}

func (server *Server) indexVariables(r *http.Request) (map[string]interface{}, error) {
titleVars := server.titleVariables(
[]string{"server", "master"},
map[string]map[string]interface{}{
Expand All @@ -182,22 +216,13 @@ func (server *Server) handleIndex(w http.ResponseWriter, r *http.Request) {
titleBuf := new(bytes.Buffer)
err := server.titleTemplate.Execute(titleBuf, titleVars)
if err != nil {
http.Error(w, "Internal Server Error", 500)
return
return nil, err
}

indexVars := map[string]interface{}{
"title": titleBuf.String(),
}

indexBuf := new(bytes.Buffer)
err = server.indexTemplate.Execute(indexBuf, indexVars)
if err != nil {
http.Error(w, "Internal Server Error", 500)
return
}

w.Write(indexBuf.Bytes())
return indexVars, err
}

func (server *Server) handleAuthToken(w http.ResponseWriter, r *http.Request) {
Expand Down
23 changes: 18 additions & 5 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ type Server struct {
factory Factory
options *Options

upgrader *websocket.Upgrader
indexTemplate *template.Template
titleTemplate *noesctmpl.Template
upgrader *websocket.Upgrader
indexTemplate *template.Template
titleTemplate *noesctmpl.Template
manifestTemplate *template.Template
}

// New creates a new instance of Server.
Expand All @@ -52,6 +53,15 @@ func New(factory Factory, options *Options) (*Server, error) {
panic("index template parse failed") // must be valid
}

manifestData, err := Asset("static/manifest.json")
if err != nil {
panic("manifest not found") // must be in bindata
}
manifestTemplate, err := template.New("manifest").Parse(string(manifestData))
if err != nil {
panic("manifest template parse failed") // must be valid
}

titleTemplate, err := noesctmpl.New("title").Parse(options.TitleFormat)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse window title format `%s`", options.TitleFormat)
Expand All @@ -78,8 +88,9 @@ func New(factory Factory, options *Options) (*Server, error) {
Subprotocols: webtty.Protocols,
CheckOrigin: originChekcer,
},
indexTemplate: indexTemplate,
titleTemplate: titleTemplate,
indexTemplate: indexTemplate,
titleTemplate: titleTemplate,
manifestTemplate: manifestTemplate,
}, nil
}

Expand Down Expand Up @@ -190,7 +201,9 @@ func (server *Server) setupHandlers(ctx context.Context, cancel context.CancelFu
siteMux.Handle(pathPrefix+"js/", http.StripPrefix(pathPrefix, staticFileHandler))
siteMux.Handle(pathPrefix+"favicon.png", http.StripPrefix(pathPrefix, staticFileHandler))
siteMux.Handle(pathPrefix+"css/", http.StripPrefix(pathPrefix, staticFileHandler))
siteMux.Handle(pathPrefix+"icon_192.png", http.StripPrefix(pathPrefix, staticFileHandler))

siteMux.HandleFunc(pathPrefix+"manifest.json", server.handleManifest)
siteMux.HandleFunc(pathPrefix+"auth_token.js", server.handleAuthToken)
siteMux.HandleFunc(pathPrefix+"config.js", server.handleConfig)

Expand Down