-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
4 changed files
with
58 additions
and
3 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
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,23 @@ | ||
package static | ||
|
||
import ( | ||
"bytes" | ||
"net/http" | ||
"strconv" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type responseRewriter struct { | ||
http.ResponseWriter | ||
old []byte | ||
new []byte | ||
} | ||
|
||
func (w *responseRewriter) Write(a []byte) (int, error) { | ||
b := bytes.Replace(a, w.old, w.new, 1) | ||
// status code and headers are printed out when we write data | ||
w.Header().Set("Content-Length", strconv.Itoa(len(b))) | ||
log.WithFields(log.Fields{"a": string(a), "b": string(b)}).Info() | ||
return w.ResponseWriter.Write(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 |
---|---|---|
@@ -1,16 +1,32 @@ | ||
package static | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
func ServerFiles(w http.ResponseWriter, r *http.Request) { | ||
type FilesServer struct { | ||
baseHRef string | ||
} | ||
|
||
func NewFilesServer(baseHRef string) *FilesServer { | ||
return &FilesServer{baseHRef} | ||
} | ||
|
||
func (s *FilesServer) ServerFiles(w http.ResponseWriter, r *http.Request) { | ||
|
||
// this hack allows us to server the routes (e.g. /workflows) with the index file | ||
if !strings.Contains(r.URL.Path, ".") { | ||
r.URL.Path = "index.html" | ||
} | ||
|
||
if r.URL.Path == "index.html" { | ||
// hack to prevent ServerHTTP from giving us gzipped content which we can do our search-and-replace on | ||
r.Header.Del("Accept-Encoding") | ||
w = &responseRewriter{ResponseWriter: w, old: []byte(`<base href="/">`), new: []byte(fmt.Sprintf(`<base href="%s">`, s.baseHRef))} | ||
} | ||
|
||
// in my IDE (IntelliJ) the next line is red for some reason - but this is fine | ||
ServeHTTP(w, r) | ||
} |