gogfSwagger

package module
v0.0.0-...-36a5ac6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 9, 2019 License: MIT Imports: 7 Imported by: 0

README

gogf-swagger

gogf utility to automatically generate RESTful API documentation with Swagger 2.0.

Port from https://github.com/swaggo/gin-swagger

Usage

Start using it
  1. Add comments to your API source code, See Declarative Comments Format.
  2. Download Swag for Go by using:
$ go get -u github.com/swaggo/swag/cmd/swag
  1. Run the Swag in your Go project root folder which contains main.go file, Swag will parse comments and generate required files(docs folder and docs/doc.go).
$ swag init

4.Download gogf-swagger by using:

$ go get github.com/zhwei820/gogf-swagger

And import following in your code:

import "github.com/zhwei820/gogf-swagger" // gogf-swagger middleware
import "github.com/zhwei820/gogf-swagger/swaggerFiles" // swagger embed files

Canonical example:
package main

import (
	"github.com/gogf/gf/g"
	"github.com/gogf/gf/g/net/ghttp"
	"github.com/zhwei820/gogf-swagger"
	"github.com/zhwei820/gogf-swagger/swaggerFiles"

    _ "github.com/zhwei820/gogf-swagger/example/docs"

)

//
// @Summary hello接口
// @Description hello接口
// @Tags hello
// @Success 200 {string} string	"ok"
// @router / [get]
func hello(r *ghttp.Request) {
	r.Response.Writeln("哈喽世界!")
}


// @title Swagger Example API
// @version 1.0
// @description This is a hello server .
// @termsOfService http://hello.io/terms/

// @contact.name hello
// @contact.url http://www.hello.io
// @contact.email hello@hello.io

// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html

// @host
// @BasePath /
func main() {
	s := g.Server()
	s.BindHandler("/", hello)

	url := gogfSwagger.URL("http://localhost:8199/swagger/doc.json") //The url pointing to API definition
	s.BindHandler("/swagger/*any", gogfSwagger.WrapHandler(swaggerFiles.Handler, url))

	s.SetPort(8199)
	s.Run()
}
  1. Run it, and browser to http://localhost:8199/swagger/index.html, you can see Swagger 2.0 Api documents.

swagger_index.html

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CustomWrapHandler

func CustomWrapHandler(config *Config, h *webdav.Handler) func(r *ghttp.Request)

CustomWrapHandler wraps `http.Handler` into `func(r *ghttp.Request)`

func DisablingCustomWrapHandler

func DisablingCustomWrapHandler(config *Config, h *webdav.Handler, envName string) func(r *ghttp.Request)

DisablingCustomWrapHandler turn handler off if specified environment variable passed

func DisablingWrapHandler

func DisablingWrapHandler(h *webdav.Handler, envName string) func(r *ghttp.Request)

DisablingWrapHandler turn handler off if specified environment variable passed

func URL

func URL(url string) func(c *Config)

URL presents the url pointing to API definition (normally swagger.json or swagger.yaml).

func WrapHandler

func WrapHandler(h *webdav.Handler, confs ...func(c *Config)) func(r *ghttp.Request)

WrapHandler wraps `http.Handler` into `func(r *ghttp.Request)`.

Types

type Config

type Config struct {
	//The url pointing to API definition (normally swagger.json or swagger.yaml). Default is `doc.json`.
	URL string
}

Config stores ginSwagger configuration variables.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL