-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #199 from smacker/dont_commit_assets
Don't commit assets
- Loading branch information
Showing
7 changed files
with
170 additions
and
481 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,3 +63,6 @@ typings/ | |
Makefile.main | ||
.ci | ||
bin | ||
|
||
# static included into go binary on release | ||
/server/asset/asset.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// +build with_static | ||
|
||
package server | ||
|
||
import ( | ||
"bytes" | ||
"encoding/base64" | ||
"encoding/json" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/bblfsh/web/server/asset" | ||
) | ||
|
||
const ( | ||
staticDirName = "static" | ||
indexFileName = "/index.html" | ||
|
||
serverValuesPlaceholder = "window.REPLACE_BY_SERVER" | ||
footerPlaceholder = `<div class="invisible-footer"></div>` | ||
) | ||
|
||
// Static contains handlers to serve static using esc | ||
type Static struct { | ||
fs http.FileSystem | ||
options options | ||
footerHTML []byte | ||
} | ||
|
||
// NewStatic creates new Static | ||
func NewStatic(dir, serverURL string, footerHTML string) *Static { | ||
var footerBytes []byte | ||
if footerHTML != "" { | ||
// skip incorrect base64 | ||
footerBytes, _ = base64.StdEncoding.DecodeString(footerHTML) | ||
} | ||
|
||
return &Static{ | ||
fs: asset.Dir(false, dir), | ||
options: options{ | ||
ServerURL: serverURL, | ||
}, | ||
footerHTML: footerBytes, | ||
} | ||
} | ||
|
||
// struct which will be marshalled and exposed to frontend | ||
type options struct { | ||
ServerURL string `json:"SERVER_URL"` | ||
} | ||
|
||
// ServeHTTP serves any static file from static directory or fallbacks on index.hml | ||
func (s *Static) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
_, err := s.fs.Open(r.URL.Path) | ||
if err != nil { | ||
if strings.HasPrefix(r.URL.Path, staticDirName) { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
|
||
s.serveIndexHTML(nil)(w, r) | ||
return | ||
} | ||
|
||
http.FileServer(s.fs).ServeHTTP(w, r) | ||
} | ||
|
||
// serveIndexHTML serves index.html file | ||
func (s *Static) serveIndexHTML(initialState interface{}) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
f, err := s.fs.Open(indexFileName) | ||
if err != nil { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
|
||
b, err := ioutil.ReadAll(f) | ||
if err != nil { | ||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
options := s.options | ||
bData, err := json.Marshal(options) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
b = bytes.Replace(b, []byte(serverValuesPlaceholder), bData, 1) | ||
b = bytes.Replace(b, []byte(footerPlaceholder), s.footerHTML, 1) | ||
|
||
w.Header().Add("Cache-Control", "no-cache, no-store, must-revalidate") | ||
|
||
info, err := f.Stat() | ||
if err != nil { | ||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) | ||
} | ||
|
||
http.ServeContent(w, r, info.Name(), info.ModTime(), bytes.NewReader(b)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// +build !with_static | ||
|
||
package server | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
// Static without the 'with_static' tag contains a placeholder handler | ||
// that returns 'not implemented' | ||
type Static struct{} | ||
|
||
// NewStatic creates new Static | ||
func NewStatic(dir, serverURL string, footerHTML string) *Static { | ||
return &Static{} | ||
} | ||
|
||
// ServeHTTP serves any static file from static directory or fallbacks on index.hml | ||
func (s *Static) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
http.Error(w, | ||
"Frontend assets are only available when using 'make build' or 'make serve'", | ||
http.StatusNotImplemented) | ||
|
||
return | ||
} |