2021-09-27 16:24:00 -04:00
|
|
|
//go:build ui
|
2020-07-07 19:57:54 -04:00
|
|
|
// +build ui
|
|
|
|
|
|
|
|
|
|
package controller
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2020-08-10 17:15:02 -04:00
|
|
|
"github.com/hashicorp/boundary/internal/ui"
|
2020-07-07 19:57:54 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
handleUi = handleUiWithAssets
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func handleUiWithAssets(c *Controller) http.Handler {
|
|
|
|
|
var nextHandler http.Handler
|
2021-10-14 12:13:07 -04:00
|
|
|
if c.conf.RawConfig.DevUiPassthroughDir != "" {
|
|
|
|
|
nextHandler = devUiPassthroughHandler(c.conf.RawConfig.DevUiPassthroughDir)
|
2020-07-07 19:57:54 -04:00
|
|
|
} else {
|
2021-04-20 09:45:05 -05:00
|
|
|
nextHandler = ui.Handler()
|
2020-07-07 19:57:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
r.URL.Path = "/"
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
switch r.URL.Path {
|
|
|
|
|
case "/", "/favicon.png", "/assets/styles.css":
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
r.URL.Path = "/"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fall through to the next handler
|
2020-08-17 15:38:41 -04:00
|
|
|
nextHandler.ServeHTTP(w, r)
|
2020-07-07 19:57:54 -04:00
|
|
|
})
|
|
|
|
|
}
|