beego(二)自动化生成api文档

/ beego / 没有评论 / 1500浏览

env

添加一个依赖驱动

root@pa6:/home/test/go/src/jeedev-api# go get -u -v github.com/go-sql-driver/mysql
github.com/go-sql-driver/mysql (download)
github.com/go-sql-driver/mysql

mysql数据库

## mysql数据库建库
root@pa6:/home/test/go/src# cat init.sql
#db--jeedev

-- ----------------------------
-- Table structure for `app`
-- ----------------------------
DROP TABLE IF EXISTS `app`;
CREATE TABLE `app` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `create_date` datetime NOT NULL,
  `app_code` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `app_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `publish_date` date DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `app_code` (`app_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

-- ----------------------------
-- Records of app
-- ----------------------------
INSERT INTO `app` VALUES ('1', NOW(), '100000', '神庙逃亡', '2015-08-06');
INSERT INTO `app` VALUES ('2', NOW(), '100001', '愤怒的小鸟', '2015-08-06');

导入到数据库

root@pa6:/home/test/go/src# mysql -uroot -p jeedev < init.sql
Enter password:

通过数据库创建Restful风格API项目

root@pa6:/home/test/go/src# bee api jeedev-api -conn="root:db_passwd@tcp(127.0.0.1:3306)/jeedev"
______
| ___ \
| |_/ /  ___   ___
| ___ \ / _ \ / _ \
| |_/ /|  __/|  __/
\____/  \___| \___| v1.10.0
2019/04/09 15:09:42 INFO     ▶ 0001 Creating API...
	create	 /home/test/go/src/jeedev-api
	create	 /home/test/go/src/jeedev-api/conf
	create	 /home/test/go/src/jeedev-api/controllers
	create	 /home/test/go/src/jeedev-api/tests
	create	 /home/test/go/src/jeedev-api/conf/app.conf
	create	 /home/test/go/src/jeedev-api/main.go
2019/04/09 15:09:42 INFO     ▶ 0002 Using 'mysql' as 'driver'
2019/04/09 15:09:42 INFO     ▶ 0003 Using 'root:db_passwd@tcp(127.0.0.1:3306)/jeedev' as 'conn'
2019/04/09 15:09:42 INFO     ▶ 0004 Using '' as 'tables'
2019/04/09 15:09:42 INFO     ▶ 0005 Analyzing database tables...
2019/04/09 15:09:42 INFO     ▶ 0006 Creating model files...
	create	 /home/test/go/src/jeedev-api/models/app.go
2019/04/09 15:09:42 INFO     ▶ 0007 Creating controller files...
	create	 /home/test/go/src/jeedev-api/controllers/app.go
2019/04/09 15:09:42 INFO     ▶ 0008 Creating router files...
	create	 /home/test/go/src/jeedev-api/routers/router.go
2019/04/09 15:09:42 SUCCESS  ▶ 0009 New API successfully created!
root@pa6:/home/test/go/src# cd
github.com/ init.sql    jeedev-api/ myproject/

api文档自动化

root@pa6:/home/test/go/src/jeedev-api# bee generate docs
______
| ___ \
| |_/ /  ___   ___
| ___ \ / _ \ / _ \
| |_/ /|  __/|  __/
\____/  \___| \___| v1.10.0
2019/04/09 15:09:52 SUCCESS  ▶ 0001 Docs successfully generated!

run

root@pa6:/home/test/go/src/jeedev-api# bee run -gendoc=true -downdoc=true
______
| ___ \
| |_/ /  ___   ___
| ___ \ / _ \ / _ \
| |_/ /|  __/|  __/
\____/  \___| \___| v1.10.0
2019/04/09 15:49:20 INFO     ▶ 0001 Using 'jeedev-api' as 'appname'
2019/04/09 15:49:20 INFO     ▶ 0002 Downloading 'https://github.com/beego/swagger/archive/v3.zip' to 'swagger.zip'...
2019/04/09 15:49:36 SUCCESS  ▶ 0003 477861 bytes downloaded!
2019/04/09 15:49:36 INFO     ▶ 0004 Unzipping 'swagger.zip'...
2019/04/09 15:49:36 SUCCESS  ▶ 0005 Done! Deleting 'swagger.zip'...
2019/04/09 15:49:36 INFO     ▶ 0006 Initializing watcher...
2019/04/09 15:49:36 INFO     ▶ 0007 Generating the docs...
2019/04/09 15:49:36 SUCCESS  ▶ 0008 Docs generated!
2019/04/09 15:49:37 SUCCESS  ▶ 0009 Built Successfully!
2019/04/09 15:49:37 INFO     ▶ 0010 Restarting 'jeedev-api'...
2019/04/09 15:49:37 SUCCESS  ▶ 0011 './jeedev-api' is running...
2019/04/09 15:49:37.909 [I] [router.go:270]  /home/test/go/src/jeedev-api/controllers no changed
2019/04/09 15:49:37.913 [I] [asm_amd64.s:1337]  http server Running on http://:8080
2019/04/09 15:49:42.446 [D] [server.go:2774]  |  125.34.210.43| 404 |    258.179µs| nomatch| GET      /docs
2019/04/09 15:49:57.792 [D] [server.go:2774]  |  125.34.210.43| 302 |     68.802µs|   match| GET      /swagger
2019/04/09 15:49:57.803 [D] [server.go:2774]  |  125.34.210.43| 200 |    135.697µs|   match| GET      /swagger/
2019/04/09 15:49:57.825 [D] [server.go:2774]  |  125.34.210.43| 200 |    260.378µs|   match| GET      /swagger/swagger-ui.css
2019/04/09 15:49:59.352 [D] [server.go:2774]  |  125.34.210.43| 200 |  1.51623649s|   match| GET      /swagger/swagger-ui-standalone-preset.js
2019/04/09 15:50:07.364 [D] [server.go:2774]  |  125.34.210.43| 200 | 9.536941352s|   match| GET      /swagger/swagger-ui-bundle.js
2019/04/09 15:50:09.547 [D] [server.go:2774]  |  125.34.210.43| 200 |    203.581µs|   match| GET      /swagger/swagger.json
2019/04/09 15:50:09.933 [D] [server.go:2774]  |  125.34.210.43| 200 |    157.279µs|   match| GET      /swagger/favicon-32x32.png
2019/04/09 15:55:54.475 [D] [server.go:2774]  |   39.96.216.86| 404 |    208.953µs| nomatch| GET      /
2019/04/09 15:56:05.504 [D] [server.go:2774]  |  125.34.210.43| 304 |     72.922µs|   match| GET      /swagger/swagger.json
2019/04/09 15:56:11.094 [D] [server.go:2774]  |  125.34.210.43| 404 |    213.837µs| nomatch| GET      /swagger/233
2019/04/09 15:56:16.325 [D] [server.go:2774]  |  125.34.210.43| 304 |     64.429µs|   match| GET      /swagger/
2019/04/09 15:56:20.688 [D] [server.go:2774]  |  125.34.210.43| 302 |     46.595µs|   match| GET      /swagger
2019/04/09 15:56:20.738 [D] [server.go:2774]  |  125.34.210.43| 304 |    104.736µs|   match| GET      /swagger/swagger-ui-bundle.js
2019/04/09 15:56:20.739 [D] [server.go:2774]  |  125.34.210.43| 304 |     13.153µs|   match| GET      /swagger/swagger-ui.css
2019/04/09 15:56:20.744 [D] [server.go:2774]  |  125.34.210.43| 304 |     51.758µs|   match| GET      /swagger/swagger-ui-standalone-preset.js
2019/04/09 15:59:16.809 [D] [server.go:2774]  |  39.96.196.196| 404 |    215.712µs| nomatch| GET      /

demo

url http://ip:8080/swaggerswagger.json

http://ip:8080/swagger