From e4f760f4c248ad07a25e3333c72db7f37f79ed81 Mon Sep 17 00:00:00 2001 From: Bastien Riviere Date: Mon, 29 Jan 2024 22:23:15 +0100 Subject: [PATCH] feat: add rapidoc --- main.go | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index ee36d60..e8aa5ea 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "context" + _ "embed" "log" "net/http" "os" @@ -13,6 +14,22 @@ import ( "github.com/jackc/pgx/v5" ) +//go:embed openapi.yaml +var openapiFile []byte + +var rapidocHtml = ` + + + + + + + + + + +` + func main() { // This is rude but at least we can configure the database URL securely. psqlUrl := os.Getenv("DATABASE_URL") @@ -40,8 +57,19 @@ func main() { log.Fatalf("failed te create OpenAPI server: %v", err) } + mux := http.NewServeMux() + mux.Handle("/", srv) + mux.Handle("/docs", serveContent([]byte(rapidocHtml), "text/html")) + mux.Handle("/docs/openapi.yaml", serveContent(openapiFile, "application/yaml")) log.Println("Listening on :8080") - if err = http.ListenAndServe(":8080", srv); err != nil { + if err = http.ListenAndServe(":8080", mux); err != nil { log.Fatal(err) } } + +func serveContent(content []byte, contentType string) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Content-Type", contentType) + w.Write(content) + }) +}