Golang 自动生成swagger
-
安装
go get -u github.com/swaggo/swag/cmd/swag
-
在项目下执行
swag init
,会生成docs目录。如果目录存在则会报错。 - docs目录下会生成docs.go,swagger.json和swagger.yaml,根据需求使用。
Gin 集成例
- main.go
// @title Sample Service API // @version 1.0 // @description Platform API for Sample. // @contact.name getsu // @contact.url http://www.swagger.io/support // @contact.email acrhwfy@gmail.com // @license.name Apache 2.0 // @license.url http://www.apache.org/licenses/LICENSE-2.0.html // @host sample.com // @BasePath /api // @securityDefinitions.apikey ApiKeyAuth // @in header // @name Authorization func setupRouter() *gin.Engine { r := gin.Default() r.Run() }
- Controller.go
//CreateApp create app // CreateApp godoc // @Summary create app // @Description create app // @Accept json // @Produce json // @Param app body dao.App true "create app" // @Success 200 {object} App // @Failure 400 {object} Response // @Failure 500 {object} Response // @Router /app [post] // @Security ApiKeyAuth func CreateApp(c *gin.Context) { //略 }
NodeJS 自动生成swagger
Express框架集成
- 安装
npm i express-swagger-generator --save-dev
- 代码例:
- main.js
var express = require('express'); var bodyParser = require('body-parser'); var controller = require('./controller'); const config = require('./config/config'); var app = express(); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); const expressSwagger = require('express-swagger-generator')(app); let options = { swaggerDefinition: { info: { description: 'This is a sample server', title: 'Swagger', version: '1.0.0', }, host: 'localhost:3000', basePath: '/v1', produces: [ "application/json", "application/xml" ], schemes: ['http', 'https'], securityDefinitions: { JWT: { type: 'apiKey', in: 'header', name: 'Authorization', description: "", } } }, route: { url:'/swagger', docs:'/swagger.json', //swagger文件 api }, basedir: __dirname, //app absolute path files: ['./controller/*.js'] //Path to the API handle folder }; expressSwagger(options) app.listen(config.port);
- controller/api.js
/** * api for get request * @route GET /api/run * @returns {object} 200 - An array of user info * @returns {Error} default - Unexpected error */ exports.doGet = function(req, res) { res.setHeader('Content-Type', 'application/json;charset=utf-8'); res.send({ result: true, message: 'ok' }); }; /** * api for post request * @route POST /api/run * @returns {object} 200 - An array of user info * @returns {Error} default - Unexpected error */ exports.doPost = function(req, res) { res.setHeader('Content-Type', 'application/json;charset=utf-8'); res.send({ result: true, message: 'ok' }); };
NestJS 集成
- 安装
npm i --save @nestjs/swagger
During the examination of the defined controllers, the SwaggerModule is looking for all used @Body(), @Query(), and @Param() decorators in the route handlers.
import { NestFactory } from '@nestjs/core'; import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'; import { ApplicationModule } from './app.module'; async function bootstrap() { const app = await NestFactory.create(ApplicationModule); const options = new DocumentBuilder() .setTitle('Cats example') .setDescription('The cats API description') .setVersion('1.0') .addTag('cats') .build(); const document = SwaggerModule.createDocument(app, options); SwaggerModule.setup('api', app, document); await app.listen(3001); } bootstrap();
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 数据生成工具 ZenData 发布 2.0 版本,自动生成“互联网黑话”!
- 自动生成高效DNN,适用于边缘设备的生成合成工具FermiNets
- Simulink自动生成代码
- changelog 日志自动生成插件
- 深度有趣 | 23 歌词古诗自动生成
- 自定义Mybatis自动生成代码规则
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
函数式算法设计珠玑
Richard Bird / 苏统华、孙芳媛、郝文超、徐琴 / 机械工业出版社 / 2017-4-1 / 69.00
本书采用完全崭新的方式介绍算法设计。全书由30个珠玑构成,每个珠玑单独列为一章,用于解决一个特定编程问题。这些问题的出处五花八门,有的来自游戏或拼图,有的是有趣的组合任务,还有的是散落于数据压缩及字串匹配等领域的更为熟悉的算法。每个珠玑以使用函数式编程语言Haskell对问题进行描述作为开始,每个解答均是诉诸于函数式编程法则从问题表述中计算得到。本书适用于那些喜欢学习算法设计思想的函数式编程人员、......一起来看看 《函数式算法设计珠玑》 这本书的介绍吧!