swaggerapi

package module
v0.0.0-...-fe91021 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2024 License: MIT Imports: 15 Imported by: 0

README

SwaggerAPI

SwaggerAPI is a simple API that allows you to create SwaggerUI for the API to help project development. It is based onSwagger 2.0 Specification

By reading the grpc service in the process, swagger document data is automatically generated, and the swagger-ui service is provided to facilitate document viewing.

It is recommended to use the gkit framework, which can automatically generate swagger documents and provide swagger-ui services.

This project is a demand derivative of gkit and can also be used independently.

Installation

$ go get github.com/ml444/swaggerapi

Usage

package main

import (
    "github.com/ml444/gkit/log"
    "github.com/ml444/gkit/transport/httpx"
    "github.com/ml444/swaggerapi"
)

func main() {
	var err error
	srv := httpx.NewServer(
		httpx.Timeout(time.Duration(hcfg.Timeout)*time.Millisecond),
		httpx.Address(hcfg.HTTPAddr),
	)
	// Create a new SwaggerAPI instance
	swaggerapi.RegisterAPI(srv)
	
	// Register your API handlers
	userpb.RegisterServerWithHTTP(srv)
	
	if err = srv.Start(context.Background()); err != nil {
		log.Error(err.Error())
		return
	}
}
$ go run main.go

访问:http://localhost:5050/swagger

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewSwaggerHandler

func NewSwaggerHandler() http.Handler

func RegisterAPI

func RegisterAPI(r Router, opts ...generator.Option)

Types

type Router

type Router interface {
	HandlePrefix(prefix string, h http.Handler)
	HandleFunc(path string, h http.HandlerFunc)
}

type Server

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

Server is api meta server

func NewServer

func NewServer(opts ...generator.Option) *Server

NewServer create server instance

func (*Server) GetServiceDesc

func (s *Server) GetServiceDesc(w http.ResponseWriter, r *http.Request)

GetServiceDesc return service meta by name

func (*Server) ListServices

func (s *Server) ListServices(w http.ResponseWriter, _ *http.Request)

ListServices return all services

type ServicesReply

type ServicesReply struct {
	Services []string `json:"services,omitempty"`
	Methods  []string `json:"methods,omitempty"`
}

Directories

Path Synopsis
genopenapi
Package genopenapi provides a code generator for OpenAPI v2.
Package genopenapi provides a code generator for OpenAPI v2.

Jump to

Keyboard shortcuts

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