package webdemo import ( "net/http" "billing/internal/web" ) const managementBase = "/v0/management/plugins/billing" const resourceUI = "/v0/resource/plugins/billing/ui" type Input interface { ServeHTTP(http.ResponseWriter, *http.Request) } func NewServer(input Input) http.Handler { mux := http.NewServeMux() mux.HandleFunc("GET /{$}", func(response http.ResponseWriter, request *http.Request) { http.Redirect(response, request, "/ui", http.StatusTemporaryRedirect) }) mux.HandleFunc("GET /ui", asset("text/html; charset=utf-8", web.UI())) mux.HandleFunc("GET /ui-config.js", asset("text/javascript; charset=utf-8", []byte(`window.BILLING_UI_CONFIG = Object.freeze({managementBase:"/v0/management/plugins/billing",readOnlyBase:"/v0/resource/plugins/billing/ui",managementKey:"demo",lockManagementKey:true,hideManagementKey:true,demoPerspectives:true});`))) mux.HandleFunc("GET /styles/{name}", embeddedAsset("styles/")) mux.HandleFunc("GET /app/{path...}", embeddedAsset("app/")) mux.HandleFunc("GET /v0/resource/plugins/billing/ui-config.js", asset("text/javascript; charset=utf-8", []byte(`window.BILLING_UI_CONFIG = Object.freeze({managementBase:"/v0/management/plugins/billing",readOnlyBase:"/v0/resource/plugins/billing/ui",managementKey:"demo",lockManagementKey:true,hideManagementKey:true,demoPerspectives:true});`))) mux.HandleFunc("GET /v0/resource/plugins/billing/styles/{name}", embeddedAsset("styles/")) mux.HandleFunc("GET /v0/resource/plugins/billing/app/{path...}", embeddedAsset("app/")) mux.Handle(managementBase+"/", input) mux.Handle(resourceUI, input) return mux } func embeddedAsset(prefix string) http.HandlerFunc { return func(response http.ResponseWriter, request *http.Request) { name := request.PathValue("name") if path := request.PathValue("path"); path != "" { name = path } body, contentType, found := web.Asset(prefix + name) if !found { http.NotFound(response, request) return } asset(contentType, body)(response, request) } } func asset(contentType string, body []byte) http.HandlerFunc { return func(response http.ResponseWriter, _ *http.Request) { response.Header().Set("Content-Type", contentType) response.Header().Set("Cache-Control", "no-store") response.WriteHeader(http.StatusOK) _, _ = response.Write(body) } }