support SWAGGER=1 and add swagger doc for api (#16)

This commit is contained in:
wuxiaohui94 2021-01-26 23:48:11 -06:00 committed by GitHub
parent fa13339e97
commit 0e9ed80203
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 92 additions and 2 deletions

View File

@ -20,6 +20,12 @@ endif
IMAGE_TAG := $(if $(IMAGE_TAG),$(IMAGE_TAG),latest)
BUILD_TAGS ?=
ifeq ($(SWAGGER),1)
BUILD_TAGS += swagger_server
endif
PACKAGE_LIST := go list ./... | grep -vE "chaos-daemon/test|pkg/ptrace|zz_generated|vendor"
PACKAGE_DIRECTORIES := $(PACKAGE_LIST) | sed 's|github.com/chaos-mesh/chaosd/||'
@ -31,7 +37,7 @@ $(GOBIN)/goimports:
build: binary
binary: chaosd
binary: swagger_spec chaosd
taily-build:
if [ "$(shell docker ps --filter=name=$@ -q)" = "" ]; then \
@ -53,7 +59,13 @@ image-binary: image-build-base
endif
chaosd:
$(CGOENV) go build -ldflags '$(LDFLAGS)' -o bin/chaosd ./cmd/chaosd/main.go
$(CGOENV) go build -ldflags '$(LDFLAGS)' -tags "${BUILD_TAGS}" -o bin/chaosd ./cmd/chaosd/main.go
swagger_spec:
ifeq ($(SWAGGER),1)
hack/generate_swagger_spec.sh
endif
image-build-base:
DOCKER_BUILDKIT=0 docker build --ulimit nofile=65536:65536 -t pingcap/chaos-build-base ${DOCKER_BUILD_ARGS} images/build-base

View File

@ -14,6 +14,9 @@
package main
import (
_ "github.com/alecthomas/template"
_ "github.com/swaggo/swag"
"github.com/chaos-mesh/chaosd/cmd/chaosd/ctl"
)

2
go.mod
View File

@ -1,6 +1,7 @@
module github.com/chaos-mesh/chaosd
require (
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
github.com/chaos-mesh/chaos-mesh v0.9.1-0.20201225074538-d531882d632a
github.com/containerd/containerd v1.2.3
github.com/docker/docker v0.7.3-0.20190327010347-be7ac8be2ae0
@ -20,6 +21,7 @@ require (
github.com/stretchr/testify v1.5.1 // indirect
github.com/swaggo/files v0.0.0-20190704085106-630677cd5c14
github.com/swaggo/gin-swagger v1.2.0
github.com/swaggo/swag v1.6.7
go.uber.org/fx v1.13.1
go.uber.org/zap v1.15.0
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect

1
go.sum
View File

@ -54,6 +54,7 @@ github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 h1:fLjPD/aNc3UIO
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/agnivade/levenshtein v1.0.1/go.mod h1:CURSv5d9Uaml+FovSIICkLbAUZ9S4RqaHDIsdSBg7lM=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=

37
hack/generate_swagger_spec.sh Executable file
View File

@ -0,0 +1,37 @@
#!/usr/bin/env bash
# Copyright 2020 Chaos Mesh Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.
# This script generates API client from the swagger annotation in the Golang server code.
set -euo pipefail
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
PROJECT_DIR="$(dirname "$DIR")"
# See https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module
cd $PROJECT_DIR
export GOBIN=$PROJECT_DIR/bin
export PATH=$GOBIN:$PATH
echo "+ Install swagger tools"
go install github.com/swaggo/swag/cmd/swag
echo "+ Clean up go mod"
go mod tidy
echo "+ Generate swagger spec"
swag init -g cmd/chaosd/main.go

View File

@ -83,6 +83,15 @@ func handler(s *httpServer) {
}
}
// @Summary Create process attack.
// @Description Create process attack.
// @Tags attack
// @Produce json
// @Param request body core.ProcessCommand true "Request body"
// @Success 200 {object} utils.Response
// @Failure 400 {object} utils.APIError
// @Failure 500 {object} utils.APIError
// @Router /api/attack/process [post]
func (s *httpServer) createProcessAttack(c *gin.Context) {
attack := &core.ProcessCommand{}
if err := c.ShouldBindJSON(attack); err != nil {
@ -99,6 +108,15 @@ func (s *httpServer) createProcessAttack(c *gin.Context) {
c.JSON(http.StatusOK, utils.AttackSuccessResponse(uid))
}
// @Summary Create network attack.
// @Description Create network attack.
// @Tags attack
// @Produce json
// @Param request body core.NetworkCommand true "Request body"
// @Success 200 {object} utils.Response
// @Failure 400 {object} utils.APIError
// @Failure 500 {object} utils.APIError
// @Router /api/attack/network [post]
func (s *httpServer) createNetworkAttack(c *gin.Context) {
attack := &core.NetworkCommand{}
if err := c.ShouldBindJSON(attack); err != nil {
@ -115,6 +133,15 @@ func (s *httpServer) createNetworkAttack(c *gin.Context) {
c.JSON(http.StatusOK, utils.AttackSuccessResponse(uid))
}
// @Summary Create stress attack.
// @Description Create stress attack.
// @Tags attack
// @Produce json
// @Param request body core.StressCommand true "Request body"
// @Success 200 {object} utils.Response
// @Failure 400 {object} utils.APIError
// @Failure 500 {object} utils.APIError
// @Router /api/attack/stress [post]
func (s *httpServer) createStressAttack(c *gin.Context) {
attack := &core.StressCommand{}
if err := c.ShouldBindJSON(attack); err != nil {
@ -131,6 +158,14 @@ func (s *httpServer) createStressAttack(c *gin.Context) {
c.JSON(http.StatusOK, utils.AttackSuccessResponse(uid))
}
// @Summary Create recover attack.
// @Description Create recover attack.
// @Tags attack
// @Produce json
// @Param uid path string true "uid"
// @Success 200 {object} utils.Response
// @Failure 500 {object} utils.APIError
// @Router /api/attack/{uid} [delete]
func (s *httpServer) recoverAttack(c *gin.Context) {
uid := c.Param("uid")
err := utils.RecoverExp(s.exp, s.chaos, uid)