diff --git a/main.go b/main.go index 7d44db5..58cc890 100644 --- a/main.go +++ b/main.go @@ -30,6 +30,12 @@ const LINKSMART = ` ╩═╝ ╩ ╝╚╝ ╩ ╩ ╚═╝ ╩ ╩ ╩ ╩ ╩╚═ ╩ ` +const ( + SwaggerUISchemeLess = "linksmart.github.io/swagger-ui/dist" + Spec = "https://raw.githubusercontent.com/linksmart/thing-directory/{version}/apidoc/openapi-spec.yml" + SourceCodeRepo = "https://github.com/linksmart/thing-directory" +) + var ( confPath = flag.String("conf", "conf/thing-directory.json", "Configuration file path") schemaPath = flag.String("schema", "conf/wot_td_schema.json", "WoT Thing Description schema file path") diff --git a/router.go b/router.go index 221ac1e..2c5c358 100644 --- a/router.go +++ b/router.go @@ -4,10 +4,10 @@ package main import ( "fmt" + "html/template" "io" "log" "net/http" - "net/url" "strings" "github.com/gorilla/mux" @@ -61,52 +61,49 @@ func optionsHandler(w http.ResponseWriter, _ *http.Request) { } func indexHandler(w http.ResponseWriter, _ *http.Request) { - const sourceRepo = "https://github.com/linksmart/thing-directory" - var version = "master" + version := "master" if Version != "" { version = Version } - var spec = "https://raw.githubusercontent.com/linksmart/thing-directory/" + version + "/apidoc/openapi-spec.yml" - var swaggerUIRelativeScheme = "//linksmart.github.io/swagger-ui/dist" - var swaggerUISecure = "https:" + swaggerUIRelativeScheme - // TODO: check on startup - _, err := url.ParseRequestURI(swaggerUISecure) - if err != nil { - log.Printf("ERROR: Invalid SwaggerUI URL: %s", err) - } + spec := strings.NewReplacer("{version}", version).Replace(Spec) + + swaggerUIRelativeScheme := "//" + SwaggerUISchemeLess + swaggerUISecure := "https:" + swaggerUIRelativeScheme w.Header().Set("Content-Type", "text/html") - var body string - body += `` - body += `

LinkSmart Thing Directory

` - body += fmt.Sprintf(`Version: %s

`, version) - // Source code - body += fmt.Sprintf(`

%s

`, sourceRepo, sourceRepo) - // Registration endpoint - body += `

RESTful registration endpoint: /td

` - // Swagger UI - body += fmt.Sprintf(`

API Documentation: Swagger UI

`, swaggerUISecure, spec) - // Interactive Swagger UI - body += fmt.Sprintf(`

Try it out! (experimental; requires internet connection on both server and client sides)

+ + data := struct { + Version, SourceRepo, Spec, SwaggerUIRelativeScheme, SwaggerUISecure string + }{version, SourceCodeRepo, spec, swaggerUIRelativeScheme, swaggerUISecure} + + tmpl := ` +

LinkSmart Thing Directory

+

Version: {{.Version}}

+

{{.SourceRepo}}

+

API Documentation: Swagger UI

+

Try it out! (experimental; requires internet connection on both server and client sides)

`, swaggerUIRelativeScheme) - body += `` + window.onload = function(){ + document.getElementById("swagger").href = "{{.SwaggerUIRelativeScheme}}/?url=" + window.location.toString() + "openapi-spec-proxy" + window.location.pathname; + } +` - _, err = w.Write([]byte(body)) + t, err := template.New("body").Parse(tmpl) + if err != nil { + log.Fatalf("Error parsing template: %s", err) + } + err = t.Execute(w, data) if err != nil { - log.Printf("ERROR writing HTTP response: %s", err) + log.Fatalf("Error applying template to response: %s", err) } } func apiSpecProxy(w http.ResponseWriter, req *http.Request) { - var version = "master" + version := "master" if Version != "" { version = Version } - var spec = "https://raw.githubusercontent.com/linksmart/thing-directory/" + version + "/apidoc/openapi-spec.yml" + spec := strings.NewReplacer("{version}", version).Replace(Spec) // get the spec res, err := http.Get(spec)