-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add spa fileserver to the web service (#3109)
* add spa fileserver to the web service
- Loading branch information
Showing
5 changed files
with
107 additions
and
60 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Enhancement: Re-Enabling web cache control | ||
|
||
We've re-enable browser caching headers (`Expires` and `Last-Modified`) for the web service, this was disabled due to a problem in the fileserver used before. | ||
Since we're now using our own fileserver implementation this works again and is enabled by default. | ||
|
||
https://github.com/owncloud/ocis/pull/3109 |
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,6 @@ | ||
Enhancement: Add SPA conform fileserver for web | ||
|
||
We've added an SPA conform fileserver to the web service. | ||
It enables web to use vue's history mode and behaves like nginx try_files. | ||
|
||
https://github.com/owncloud/ocis/pull/3109 |
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,80 @@ | ||
package assets | ||
|
||
import ( | ||
"bytes" | ||
"golang.org/x/net/html" | ||
"io" | ||
"mime" | ||
"net/http" | ||
"path" | ||
"path/filepath" | ||
) | ||
|
||
type fileServer struct { | ||
root http.FileSystem | ||
} | ||
|
||
func FileServer(root http.FileSystem) http.Handler { | ||
return &fileServer{root} | ||
} | ||
|
||
func (f *fileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
upath := path.Clean(path.Join("/", r.URL.Path)) | ||
r.URL.Path = upath | ||
|
||
asset, err := f.root.Open(upath) | ||
if err != nil { | ||
r.URL.Path = "/index.html" | ||
f.ServeHTTP(w, r) | ||
return | ||
} | ||
defer asset.Close() | ||
|
||
s, _ := asset.Stat() | ||
if s.IsDir() { | ||
r.URL.Path = "/index.html" | ||
f.ServeHTTP(w, r) | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", mime.TypeByExtension(filepath.Ext(s.Name()))) | ||
|
||
buf := new(bytes.Buffer) | ||
|
||
switch s.Name() { | ||
case "index.html", "oidc-callback.html", "oidc-silent-redirect.html": | ||
_ = withBase(buf, asset, "/") | ||
default: | ||
_, _ = buf.ReadFrom(asset) | ||
} | ||
|
||
_, _ = w.Write(buf.Bytes()) | ||
} | ||
|
||
func withBase(w io.Writer, r io.Reader, base string) error { | ||
doc, _ := html.Parse(r) | ||
var parse func(*html.Node) | ||
parse = func(n *html.Node) { | ||
if n.Type == html.ElementNode && n.Data == "head" { | ||
n.InsertBefore(&html.Node{ | ||
Type: html.ElementNode, | ||
Data: "base", | ||
Attr: []html.Attribute{ | ||
{ | ||
Key: "href", | ||
Val: base, | ||
}, | ||
}, | ||
}, n.FirstChild) | ||
|
||
return | ||
} | ||
|
||
for c := n.FirstChild; c != nil; c = c.NextSibling { | ||
parse(c) | ||
} | ||
} | ||
parse(doc) | ||
|
||
return html.Render(w, doc) | ||
} |
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