swagger

package module
v0.0.0-...-79bfb9d Latest Latest
Warning

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

Go to latest
Published: May 5, 2023 License: MIT Imports: 9 Imported by: 0

README

Swagger

Wool handler to automatically generate RESTful API documentation with Swagger 2.0

Usage

Start using it
  1. Add comments to your API source code, See Declarative Comments Format.
  2. Download Swag for Go by using:
go install github.com/swaggo/swag/cmd/swag@latest
  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
  1. Download swagger by using:
go get github.com/gowool/swagger

And import following in your code:

import "github.com/gowool/swagger" // wool swagger handler
Canonical example:
package main

import (
    "context"
    "github.com/gowool/swagger"
    _ "github.com/gowool/swagger/example/docs"
    "github.com/gowool/wool"
	"golang.org/x/exp/slog"
    "os"
)

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

// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email support@swagger.io

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

// @host petstore.swagger.io
// @BasePath /api/v1
func main() {
	logger := slog.New(slog.HandlerOptions{Level: slog.LevelDebug}.NewJSONHandler(os.Stdout))
    w := wool.New(logger.WithGroup("wool"))
    swg := swagger.New(&swagger.Config{})
    w.GET("/swagger/...", swg.Handler)
    
    srv := wool.NewServer(&wool.ServerConfig{
        Address: ":1323",
    }, logger.WithGroup("server"))
    if err := srv.StartC(context.Background(), w); err != nil {
		srv.Log.Error("server error", err)
        os.Exit(1)
    }
}
  1. Run it, and browser to http://localhost:1323/swagger/index.html, you can see Swagger 2.0 Api documents.

License

Distributed under MIT License, please see license file within the code for more details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// The url pointing to API definition (normally swagger.json or swagger.yaml).
	URL                  string `mapstructure:"url"`
	DocExpansion         string `mapstructure:"doc_expansion"`
	DomID                string `mapstructure:"dom_id"`
	InstanceName         string `mapstructure:"instance_name"`
	DeepLinking          *bool  `mapstructure:"deep_linking"`
	PersistAuthorization bool   `mapstructure:"persist_authorization"`
	SyntaxHighlight      *bool  `mapstructure:"syntax_highlight"`

	// The information for OAuth2 integration, if any.
	OAuth *OAuthConfig `mapstructure:"oauth"`
}

func (*Config) Init

func (cfg *Config) Init()

type OAuthConfig

type OAuthConfig struct {
	// The ID of the client sent to the OAuth2 IAM provider.
	ClientId string `mapstructure:"client_id"`

	// The OAuth2 realm that the client should operate in. If not applicable, use empty string.
	Realm string `mapstructure:"realm"`

	// The name to display for the application in the authentication popup.
	AppName string `mapstructure:"app_name"`
}

OAuthConfig stores configuration for Config UI OAuth2 integration. See https://swagger.io/docs/open-source-tools/swagger-ui/usage/oauth2/ for further details.

type Swagger

type Swagger struct {
	// contains filtered or unexported fields
}

func New

func New(cfg *Config) *Swagger

func NewWithTemplate

func NewWithTemplate(cfg *Config, index *template.Template) *Swagger

func (*Swagger) Handler

func (s *Swagger) Handler(c wool.Ctx) error

Directories

Path Synopsis
docs
Package docs GENERATED BY SWAG; DO NOT EDIT This file was generated by swaggo/swag
Package docs GENERATED BY SWAG; DO NOT EDIT This file was generated by swaggo/swag

Jump to

Keyboard shortcuts

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