knife4g

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: May 30, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

README

Knife4g

use knife4j-front to show the api documents for gin

Usage

  1. Add comments to your API source code
  2. Download Swag for Go by using:
  3. Run swag init -ot json in your project directory
  4. Run go get github.com/hononet639/knife4g
  5. Add router to your gin project
    example:
    package main
    
    import (
        "github.com/gin-gonic/gin"
        "github.com/hononet639/knife4g"
    )
    
    func main() {
        engine := gin.Default()
        engine.GET("/doc/*any", knife4g.Handler(knife4g.Config{RelativePath: "/doc"}))
        engine.Run(":80")
    }
    
  6. Visit http://localhost/doc/index

Acknowledgement

Thanks to knife4j

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handler

func Handler(config *Config) http.Handler

Handler 返回 knife4g 文档服务 http.Handler

Types

type Config

type Config struct {
	RelativePath   string // 访问前缀,如 "/doc"
	OpenApiContent []byte // openapi.yaml的数据内容
	StaticPath     string // 静态资源路径(可选,默认 embed 的 front)
	KService       Knife4gService
}

type Knife4gService added in v0.0.3

type Knife4gService struct {
	Name           string `json:"name"`
	Url            string `json:"url"`
	SwaggerVersion string `json:"swaggerVersion"`
	Location       string `json:"location"`
}

type OpenAPI

type OpenAPI struct {
	OpenAPI    string                 `yaml:"openapi" json:"swagger"`
	Info       map[string]interface{} `yaml:"info" json:"info"`
	Paths      map[string]interface{} `yaml:"paths" json:"paths"`
	Components map[string]interface{} `yaml:"components" json:"definitions"`
	Tags       []struct {
		Name        string `yaml:"name" json:"name"`
		Description string `yaml:"description" json:"description"`
	} `yaml:"tags" json:"tags"`
}

OpenAPI文档结构

Jump to

Keyboard shortcuts

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