feat: add rapidoc
This commit is contained in:
parent
b248550c3b
commit
e4f760f4c2
1 changed files with 29 additions and 1 deletions
30
main.go
30
main.go
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
_ "embed"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
@ -13,6 +14,22 @@ import (
|
||||||
"github.com/jackc/pgx/v5"
|
"github.com/jackc/pgx/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//go:embed openapi.yaml
|
||||||
|
var openapiFile []byte
|
||||||
|
|
||||||
|
var rapidocHtml = `
|
||||||
|
<!doctype html> <!-- Important: must specify -->
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8"> <!-- Important: rapi-doc uses utf8 characters -->
|
||||||
|
<script type="module" src="https://unpkg.com/rapidoc/dist/rapidoc-min.js"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<rapi-doc spec-url="/docs/openapi.yaml" render-style="view"> </rapi-doc>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// This is rude but at least we can configure the database URL securely.
|
// This is rude but at least we can configure the database URL securely.
|
||||||
psqlUrl := os.Getenv("DATABASE_URL")
|
psqlUrl := os.Getenv("DATABASE_URL")
|
||||||
|
@ -40,8 +57,19 @@ func main() {
|
||||||
log.Fatalf("failed te create OpenAPI server: %v", err)
|
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")
|
log.Println("Listening on :8080")
|
||||||
if err = http.ListenAndServe(":8080", srv); err != nil {
|
if err = http.ListenAndServe(":8080", mux); err != nil {
|
||||||
log.Fatal(err)
|
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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue