This commit is contained in:
+46
-17
@@ -20,15 +20,27 @@ const (
|
||||
var staticFiles embed.FS
|
||||
|
||||
type gateway struct {
|
||||
upstream *url.URL
|
||||
client *http.Client
|
||||
static http.Handler
|
||||
core *url.URL
|
||||
continuity *url.URL
|
||||
client *http.Client
|
||||
static http.Handler
|
||||
}
|
||||
|
||||
func newHandler(upstream string, client *http.Client) (http.Handler, error) {
|
||||
parsed, err := url.Parse(upstream)
|
||||
if err != nil || parsed.Scheme == "" || parsed.Host == "" {
|
||||
return nil, fmt.Errorf("invalid core runtime URL")
|
||||
type routeTarget uint8
|
||||
|
||||
const (
|
||||
targetCore routeTarget = iota
|
||||
targetContinuity
|
||||
)
|
||||
|
||||
func newHandler(coreURL, continuityURL string, client *http.Client) (http.Handler, error) {
|
||||
core, err := parseRuntimeURL(coreURL, "Core")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
continuity, err := parseRuntimeURL(continuityURL, "Continuity")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
assets, err := fs.Sub(staticFiles, "static")
|
||||
if err != nil {
|
||||
@@ -37,7 +49,15 @@ func newHandler(upstream string, client *http.Client) (http.Handler, error) {
|
||||
if client == nil {
|
||||
client = http.DefaultClient
|
||||
}
|
||||
return securityHeaders(&gateway{upstream: parsed, client: client, static: http.FileServer(http.FS(assets))}), nil
|
||||
return securityHeaders(&gateway{core: core, continuity: continuity, client: client, static: http.FileServer(http.FS(assets))}), nil
|
||||
}
|
||||
|
||||
func parseRuntimeURL(rawURL, name string) (*url.URL, error) {
|
||||
parsed, err := url.Parse(rawURL)
|
||||
if err != nil || parsed.Scheme == "" || parsed.Host == "" {
|
||||
return nil, fmt.Errorf("invalid %s Runtime URL", name)
|
||||
}
|
||||
return parsed, nil
|
||||
}
|
||||
|
||||
func (g *gateway) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -61,7 +81,7 @@ func (g *gateway) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (g *gateway) forwardAPI(w http.ResponseWriter, r *http.Request) {
|
||||
upstreamPath, allowed := creatorRoute(r.Method, r.URL.Path)
|
||||
routeTarget, upstreamPath, allowed := creatorRoute(r.Method, r.URL.Path)
|
||||
if !allowed {
|
||||
http.Error(w, "creator route denied", http.StatusNotFound)
|
||||
return
|
||||
@@ -75,7 +95,11 @@ func (g *gateway) forwardAPI(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "request too large", http.StatusRequestEntityTooLarge)
|
||||
return
|
||||
}
|
||||
target := *g.upstream
|
||||
upstream := g.core
|
||||
if routeTarget == targetContinuity {
|
||||
upstream = g.continuity
|
||||
}
|
||||
target := *upstream
|
||||
target.Path = upstreamPath
|
||||
target.RawQuery = r.URL.RawQuery
|
||||
request, err := http.NewRequestWithContext(r.Context(), r.Method, target.String(), strings.NewReader(string(body)))
|
||||
@@ -98,26 +122,31 @@ func (g *gateway) forwardAPI(w http.ResponseWriter, r *http.Request) {
|
||||
_, _ = io.Copy(w, io.LimitReader(response.Body, maxResponseBytes))
|
||||
}
|
||||
|
||||
func creatorRoute(method, path string) (string, bool) {
|
||||
func creatorRoute(method, path string) (routeTarget, string, bool) {
|
||||
switch {
|
||||
case method == http.MethodPost && path == "/api/session":
|
||||
return "/v1/creator/sessions", true
|
||||
return targetCore, "/v1/creator/sessions", true
|
||||
case method == http.MethodGet && path == "/api/session/current":
|
||||
return "/v1/creator/sessions/current", true
|
||||
return targetCore, "/v1/creator/sessions/current", true
|
||||
case method == http.MethodGet && path == "/api/focus":
|
||||
return "/v1/creator/focus", true
|
||||
return targetCore, "/v1/creator/focus", true
|
||||
case method == http.MethodGet && strings.HasPrefix(path, "/api/tasks/"):
|
||||
id := strings.TrimPrefix(path, "/api/tasks/")
|
||||
if validID(id) {
|
||||
return "/v1/creator/tasks/" + id, true
|
||||
return targetCore, "/v1/creator/tasks/" + id, true
|
||||
}
|
||||
case method == http.MethodGet && strings.HasPrefix(path, "/api/capabilities/") && strings.HasSuffix(path, "/health"):
|
||||
id := strings.TrimSuffix(strings.TrimPrefix(path, "/api/capabilities/"), "/health")
|
||||
if validID(id) {
|
||||
return "/v1/creator/capabilities/" + id + "/health", true
|
||||
return targetCore, "/v1/creator/capabilities/" + id + "/health", true
|
||||
}
|
||||
case method == http.MethodGet && strings.HasPrefix(path, "/api/prospective-items/"):
|
||||
id := strings.TrimPrefix(path, "/api/prospective-items/")
|
||||
if validID(id) {
|
||||
return targetContinuity, "/v1/creator/prospective-items/" + id, true
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
return targetCore, "", false
|
||||
}
|
||||
|
||||
func validID(value string) bool {
|
||||
|
||||
Reference in New Issue
Block a user