Skip to content

Latest commit

 

History

History
94 lines (72 loc) · 1.83 KB

README.md

File metadata and controls

94 lines (72 loc) · 1.83 KB

go-zero-swagger-example

embbed swagger in go-zero server Embedded Swagger UI for go-zero

package main

import (
	_ "embed"

	"flag"
	"fmt"
	"net/http"
	"strings"

	"greet/api/internal/config"
	"greet/api/internal/handler"
	"greet/api/internal/svc"

	"github.com/swaggest/swgui/v5emb"
	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/rest"
)

//go:embed greet.json
var spec []byte

var configFile = flag.String("f", "etc/greet.yaml", "the config file")

const (
	swaggerAPI     = "/api/doc"
	SwaggerJsonAPI = "/api/greet.json"
	Title          = "title"
)

var swaggerHandle http.Handler = v5emb.New(
	Title,
	SwaggerJsonAPI,
	swaggerAPI,
)

func Notfound() http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		if strings.HasPrefix(r.URL.Path, swaggerAPI) {
			swaggerHandle.ServeHTTP(w, r)
			return
		}
		http.NotFound(w, r)
	}
}

func main() {
	flag.Parse()

	var c config.Config
	conf.MustLoad(*configFile, &c)

	server := rest.MustNewServer(c.RestConf, rest.WithNotFoundHandler(Notfound()))
	// or just like below
	// server = rest.MustNewServer(c.RestConf, rest.WithNotFoundHandler(swaggerHandle))

	defer server.Stop()
	// swagger  json file
	server.AddRoute(rest.Route{
		Method: http.MethodGet,
		Path:   SwaggerJsonAPI,
		Handler: func(w http.ResponseWriter, r *http.Request) {
			_, _ = w.Write(spec)
		},
	})

	ctx := svc.NewServiceContext(c)
	handler.RegisterHandlers(server, ctx)
	server.PrintRoutes()

	fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
	fmt.Println("doc: http://localhost:8888/api/doc")
	server.Start()
}

#Test

git clone [email protected]:dtest11/go-zero-swagger-example.git
cd go-zero-swagger-example/api
make r

you can check by http://localhost:8888/api/doc

Alt Text