swaggest / swgui

Embedded Swagger UI for Go
https://pkg.go.dev/github.com/swaggest/swgui
Apache License 2.0
51 stars 8 forks source link

could u give a sample which can show local json api file #38

Closed shyandsy closed 1 year ago

shyandsy commented 1 year ago

the sample code like this

package main

import (
    "net/http"

    v3 "github.com/swaggest/swgui/v3"
)

func main() {
    http.Handle("/", v3.NewHandler("My API", "/sso.json", "/"))
    http.ListenAndServe(":8080", nil)
}

image

it doesnt work image

vearutop commented 1 year ago

Hi, please check the following example:

package main

import (
    "log"
    "net/http"

    swgui "github.com/swaggest/swgui/v5"
)

func main() {
    urlToSchema := "/sso.json"
    filePathToSchema := "./sso.json"

    swh := swgui.NewHandler("Foo", urlToSchema, "/")
    hh := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
        if r.URL.Path == urlToSchema {
            http.ServeFile(rw, r, filePathToSchema)
            return
        }

        swh.ServeHTTP(rw, r)
    })

    log.Println("Starting Swagger UI server at http://localhost:8082/")
    _ = http.ListenAndServe("localhost:8082", hh)
}