|
|
|
|
@ -1,10 +1,13 @@
|
|
|
|
|
package controller
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
|
"path"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
@ -37,32 +40,135 @@ func (c *Controller) handler(props HandlerProperties) (http.Handler, error) {
|
|
|
|
|
// Create the muxer to handle the actual endpoints
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
|
|
|
|
|
if c.conf.RawConfig.PassthroughDirectory != "" {
|
|
|
|
|
// Panic may not be ideal but this is never a production call and it'll
|
|
|
|
|
// panic on startup. We could also just change the function to return
|
|
|
|
|
// an error.
|
|
|
|
|
abs, err := filepath.Abs(c.conf.RawConfig.PassthroughDirectory)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
c.logger.Warn("serving passthrough files at /", "path", abs)
|
|
|
|
|
fs := http.FileServer(http.Dir(abs))
|
|
|
|
|
prefixHandler := http.StripPrefix("/", fs)
|
|
|
|
|
mux.Handle("/", prefixHandler)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
h, err := handleGrpcGateway(c)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
mux.Handle("/v1/", h)
|
|
|
|
|
|
|
|
|
|
// TODO: enable when not in this mode, when we bundle the assets
|
|
|
|
|
if c.conf.RawConfig.PassthroughDirectory != "" {
|
|
|
|
|
mux.Handle("/", handleUi(c))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
corsWrappedHandler := wrapHandlerWithCors(mux, props)
|
|
|
|
|
commonWrappedHandler := wrapHandlerWithCommonFuncs(corsWrappedHandler, c, props)
|
|
|
|
|
|
|
|
|
|
return commonWrappedHandler, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func handleUi(c *Controller) http.Handler {
|
|
|
|
|
// TODO: Do stuff with real UI data when it's bundled. We may also have to
|
|
|
|
|
// do a similar thing with fetching index.html in advance.
|
|
|
|
|
var nextHandler http.Handler
|
|
|
|
|
var indexBytes []byte
|
|
|
|
|
var modTime time.Time
|
|
|
|
|
if c.conf.RawConfig.PassthroughDirectory != "" {
|
|
|
|
|
nextHandler, indexBytes, modTime = devPassthroughHandler(c)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
returnIndexBytes := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
_, file := filepath.Split(r.URL.Path)
|
|
|
|
|
rw := newIndexResponseWriter()
|
|
|
|
|
http.ServeContent(rw, r, file, modTime, bytes.NewReader(indexBytes))
|
|
|
|
|
for k, v := range rw.header {
|
|
|
|
|
for _, i := range v {
|
|
|
|
|
w.Header().Add(k, i)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
w.Header().Set("content-type", "text/html; charset=utf-8")
|
|
|
|
|
w.WriteHeader(rw.statusCode)
|
|
|
|
|
w.Write(rw.body.Bytes())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if r.Method != http.MethodGet {
|
|
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dotIndex := strings.LastIndex(r.URL.Path, ".")
|
|
|
|
|
switch dotIndex {
|
|
|
|
|
case -1:
|
|
|
|
|
// For all paths without an extension serve /index.html
|
|
|
|
|
returnIndexBytes(w, r)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
switch r.URL.Path {
|
|
|
|
|
case "/index.html":
|
|
|
|
|
// Because of the special handling of http.FileServer this fails
|
|
|
|
|
// in dev passthrough mode so we handle it specifically
|
|
|
|
|
returnIndexBytes(w, r)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
case "/favicon.png", "/assets/styles.css":
|
|
|
|
|
// This is purely an optimization, it'd fall through below
|
|
|
|
|
// outside of this case
|
|
|
|
|
nextHandler.ServeHTTP(w, r)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
for i := dotIndex + 1; i < len(r.URL.Path); i++ {
|
|
|
|
|
intVal := r.URL.Path[i]
|
|
|
|
|
// Current guidance from FE is if it's only alphanum after
|
|
|
|
|
// the last dot, treat it as an extension
|
|
|
|
|
if intVal < '0' ||
|
|
|
|
|
(intVal > '9' && intVal < 'A') ||
|
|
|
|
|
(intVal > 'Z' && intVal < 'a') ||
|
|
|
|
|
intVal > 'z' {
|
|
|
|
|
// Not an extension. Serve the contents of index.html
|
|
|
|
|
returnIndexBytes(w, r)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fall through to the next handler
|
|
|
|
|
nextHandler.ServeHTTP(w, r)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func devPassthroughHandler(c *Controller) (http.Handler, []byte, time.Time) {
|
|
|
|
|
// Panic may not be ideal but this is never a production call and it'll
|
|
|
|
|
// panic on startup. We could also just change the function to return
|
|
|
|
|
// an error.
|
|
|
|
|
abs, err := filepath.Abs(c.conf.RawConfig.PassthroughDirectory)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
c.logger.Warn("serving passthrough files at /", "path", abs)
|
|
|
|
|
fs := http.FileServer(http.Dir(abs))
|
|
|
|
|
prefixHandler := http.StripPrefix("/", fs)
|
|
|
|
|
|
|
|
|
|
// We need to read index.html because http.ServeFile has special handling
|
|
|
|
|
// for that file that we don't want
|
|
|
|
|
file, err := os.Open(filepath.Join(abs, "index.html"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.logger.Warn("unable to open index.html in the dev passthrough directory, if it exists")
|
|
|
|
|
return prefixHandler, nil, time.Time{}
|
|
|
|
|
}
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
|
|
fileInfo, err := file.Stat()
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.logger.Warn("unable to stat index.html in the dev passthrough directory, if it exists")
|
|
|
|
|
return prefixHandler, nil, time.Time{}
|
|
|
|
|
}
|
|
|
|
|
modTime := fileInfo.ModTime()
|
|
|
|
|
|
|
|
|
|
// Easier to just do an ioutil.ReadAll than deal with the lower level read
|
|
|
|
|
// methods, even though we're opening twice
|
|
|
|
|
indexBytes, err := ioutil.ReadFile(filepath.Join(abs, "index.html"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.logger.Warn("unable to read index.html bytes in the dev passthrough directory, if it exists")
|
|
|
|
|
return prefixHandler, nil, time.Time{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return prefixHandler, indexBytes, modTime
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func handleGrpcGateway(c *Controller) (http.Handler, error) {
|
|
|
|
|
// Register*ServiceHandlerServer methods ignore the passed in ctx. Using the baseContext now just in case this changes
|
|
|
|
|
// in the future, at which point we'll want to be using the baseContext.
|
|
|
|
|
|