Use golang to make a restful api server
- golang
- restful
- api
- gin
- swagger
- nginx
- Viper (Go configuration with fangs)
- gorm (Go ORM lib)
- JWT
- HTTPS
-X/--request [GET|POST|PUT|DELETE|…] 指定请求的 HTTP 方法
-H/--header 指定请求的 HTTP Header
-d/--data 指定请求的 HTTP 消息体(Body)
-v/--verbose 输出详细的返回信息
-u/--user 指定账号、密码
-b/--cookie 读取 cookie...
curl -v -XPOST -H "Content-Type: application/json" http://127.0.0.1:8080/user -d'{"username":"admin","password":"admin1234"}'...
Build
> cd $GOPATH/src
> git clone https://github.com/lexkong/vendor
> cd go-api-server
> go build -v .
> ./go-api-server
- v1 实战:启动一个最简单的RESTful API服务器
> curl -XGET http://127.0.0.1:8080/sd/health
> curl -XGET http://127.0.0.1:8080/sd/disk
> curl -XGET http://127.0.0.1:8080/sd/cpu
> curl -XGET http://127.0.0.1:8080/sd/ram
- v2 实战:配置文件读取
- v3 实战:记录和管理API日志
- v4 实战:初始化Mysql数据库并建立连接
- v5 实战:自定义业务错误信息
> curl -XPOST -H "Content-Type: application/json" http://127.0.0.1:8080/v1/user
> curl -XPOST -H "Content-Type: application/json" http://127.0.0.1:8080/v1/user -d'{"username":"admin"}'
> curl -XPOST -H "Content-Type: application/json" http://127.0.0.1:8080/v1/user -d'{"password":"admin"}'
> curl -XPOST -H "Content-Type: application/json" http://127.0.0.1:8080/v1/user -d'{"username":"admin","password":"admin"}'
- v6 实战:读取和返回HTTP请求
> curl -XPOST -H "Content-Type: application/json" http://127.0.0.1:8080/v1/user/admin2?desc=test -d'{"username":"admin","password":"admin"}'
- v7 实战:用户业务逻辑处理(业务处理)
> curl -XPOST -H "Content-Type: application/json" http://127.0.0.1:8080/v1/user -d'{"username":"kong","password":"kong123"}'
> curl -XGET -H "Content-Type: application/json" http://127.0.0.1:8080/v1/user -d'{"offset": 0, "limit": 20}'
> curl -XGET -H "Content-Type: application/json" http://127.0.0.1:8080/v1/user/kong
> curl -XPUT -H "Content-Type: application/json" http://127.0.0.1:8080/v1/user/2 -d'{"username":"kong","password":"kongmodify"}'
> curl -XDELETE -H "Content-Type: application/json" http://127.0.0.1:8080/v1/user/2
- v8 实战:HTTP调用添加自定义处理逻辑
> curl -v -XGET -H "Content-Type: application/json" http://127.0.0.1:8080/v1/user
- v9 实战:API身份验证
> curl -XGET -H "Content-Type: application/json" http://127.0.0.1:8080/v1/user
> curl -XPOST -H "Content-Type: application/json" http://127.0.0.1:8080/login -d'{"username":"admin","password":"admin"}'
> curl -XGET -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MzA4NjI2NjQsImlkIjoxLCJuYmYiOjE1MzA4NjI2NjQsInVzZXJuYW1lIjoiYWRtaW4ifQ.zgBNiBHi2WufuolA2iTcmHzjyf30Hi5cSb00FFyml1Q" -H "Content-Type: application/json" http://127.0.0.1:8080/v1/user
- v10 进阶:用HTTPS加密API请求
> openssl req -new -nodes -x509 -out conf/server.crt -keyout conf/server.key -days 3650 -subj "/C=DE/ST=NRW/L=Earth/O=Random Company/OU=IT/CN=127.0.0.1/emailAddress=xxxxx@xxx.xxx"
> curl -XGET -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MjgwMTY5MjIsImlkIjowLCJuYmYiOjE1MjgwMTY5MjIsInVzZXJuYW1lIjoiYWRtaW4ifQ.LjxrK9DuAwAzUD8-9v43NzWBN7HXsSLfebw92DKd1JQ" -H "Content-Type: application/json" https://127.0.0.1:8081/v1/user
> curl -XGET -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MjgwMTY5MjIsImlkIjowLCJuYmYiOjE1MjgwMTY5MjIsInVzZXJuYW1lIjoiYWRtaW4ifQ.LjxrK9DuAwAzUD8-9v43NzWBN7HXsSLfebw92DKd1JQ" -H "Content-Type: application/json" https://127.0.0.1:8081/v1/user --cacert conf/server.crt
- v11 进阶:用Makefile管理API项目
> make
> make gotool
> make clean
> make ca
> make help
- v12 进阶:给API命令增加版本功能
> make
> ./go-api-server -v
- v13 进阶:给API增加启动脚本
> ./admin.sh -h
> ./admin.sh status
> ./admin.sh start
> ./admin.sh status
> ./admin.sh restart
> ./admin.sh status
> ./admin.sh stop
> ./admin.sh status
- v14 进阶:基于Nginx的API部署方案
- v15 进阶:go test测试你的代码
> cd util
> go test
> go test -v
> go test -v -count 2
> go test -test.bench=".*"
> go test -bench=".*" -cpuprofile=cpu.profile
> go tool pprof util.test cpu.profile
> top
> go tool pprof util.test cpu.profile
> svg
> go test -coverprofile=cover.out
> go tool cover -func=cover.out
- v16 进阶:API性能分析
> go tool pprof http://127.0.0.1:8080/debug/pprof/profile
> http://127.0.0.1:8080/debug/pprof/
- v17 进阶:生成Swagger在线文档
> swag init
> make
> ./go-api-server
> http://127.0.0.1:8080/swagger/index.html