Files
threshold/internal/servers/controller/index_response_writer.go
T
Jeff MitchellandRandall Morey 8da4effd5e Add mechanism for bundling UI assets. (#153)
* Add mechanism for bundling UI assets.

The scripts/uigen.sh script will check out the UI repo, build assets,
create a static assets file, and clean up.

The dev Makefile target has been modified to run this script if no
assets file is found. There is also a build-ui target that will
unilaterally update (the dev target only builds if the file isn't found,
but doesn't update).

The handler has been updated to add the default org ID as a header if
the request path is "/".

Co-authored-by: Randall Morey <randall@randallmorey.com>
2020-06-30 11:26:22 -04:00

52 lines
1.0 KiB
Go

package controller
import (
"bytes"
"net/http"
"strings"
)
const magicValue = `{{DEFAULT_ORG_ID}}`
type indexResponseWriter struct {
statusCode int
header http.Header
body *bytes.Buffer
defaultOrgId string
}
// newindexResponseWriter returns an initialized indexResponseWriter
func newIndexResponseWriter(defaultOrgId string) *indexResponseWriter {
return &indexResponseWriter{
header: make(http.Header),
body: new(bytes.Buffer),
defaultOrgId: defaultOrgId,
}
}
func (i *indexResponseWriter) Header() http.Header {
return i.header
}
func (i *indexResponseWriter) Write(buf []byte) (int, error) {
return i.body.Write(buf)
}
func (i *indexResponseWriter) WriteHeader(code int) {
i.statusCode = code
}
func (i *indexResponseWriter) writeToWriter(w http.ResponseWriter) {
for k, v := range i.header {
for _, h := range v {
w.Header().Add(k, h)
}
}
w.WriteHeader(i.statusCode)
w.Write(
[]byte(
strings.Replace(i.body.String(), magicValue, i.defaultOrgId, 1),
),
)
}