Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Or 1101 titond backend add swagger and ci for checking if the swagger doc is updated #10

1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ run: $(TARGET)
$(TARGET): build

check:
swag init -g cmd/titond/main.go
CGO_ENABLED=0 GOOS=linux go build -o ./build/bin/titond ./cmd/titond/main.go
./build/bin/titond check-swagger

Expand Down
21 changes: 18 additions & 3 deletions cmd/titond/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func checkSwagger(ctx *cli.Context) error {
fmt.Printf("%-6s %-25s %s\n", route.Method, route.Path, route.Handler)
}
fmt.Println("Total APIs: ", numAPIs)
fmt.Println("Total APIs excluding swagger api: ", (numAPIs - 1))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So kind! 😍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you ^^


jsonData, err := ioutil.ReadFile("./docs/swagger.json")
if err != nil {
Expand All @@ -127,12 +128,26 @@ func checkSwagger(ctx *cli.Context) error {
}
switch v := paths.(type) {
case map[string]interface{}:
fmt.Println("num api in swagger", len(v))
if len(v) != numAPIs-1 {
return errors.New("the docs file was not updated")
numOfApisInDocs := countNumOfAPIsInDocs(v)
if numOfApisInDocs != numAPIs-1 {
return errors.New("missing swagger description for api")
}
default:
return errors.New("")
}
return nil
}

func countNumOfAPIsInDocs(paths map[string]interface{}) int {
counter := 0
for path, metadata := range paths {
fmt.Println(" [Path]:", path)
switch v := metadata.(type) {
case map[string]interface{}:
counter += len(v)
default:
}
}
fmt.Println("Api in docs ", counter)
return counter
}
14 changes: 10 additions & 4 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const docTemplate = `{
"get": {
"description": "Get Component By Type",
"summary": "GetComponentByType",
"operationId": "get-component-by-type",
"parameters": [
{
"type": "string",
Expand Down Expand Up @@ -59,6 +60,7 @@ const docTemplate = `{
"application/json"
],
"summary": "CreateComponent",
"operationId": "create-component",
"parameters": [
{
"description": "Component data to create",
Expand Down Expand Up @@ -88,8 +90,9 @@ const docTemplate = `{
},
"/api/components/{id}": {
"get": {
"description": "Get Component By Type",
"summary": "GetComponentByType",
"description": "Get Component By ID",
"summary": "GetComponentByID",
"operationId": "get-component-by-id",
"parameters": [
{
"type": "integer",
Expand All @@ -115,8 +118,9 @@ const docTemplate = `{
}
},
"delete": {
"description": "Get Component By Type",
"summary": "GetComponentByType",
"description": "Delete Component By Id",
"summary": "DeleteComponentById",
"operationId": "delete-component-by-id",
"parameters": [
{
"type": "integer",
Expand Down Expand Up @@ -149,6 +153,7 @@ const docTemplate = `{
"application/json"
],
"summary": "CreateNetwork",
"operationId": "create-network",
"responses": {
"200": {
"description": "OK",
Expand All @@ -169,6 +174,7 @@ const docTemplate = `{
"application/json"
],
"summary": "DeleteNetwork",
"operationId": "delete-network",
"parameters": [
{
"type": "integer",
Expand Down
14 changes: 10 additions & 4 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"get": {
"description": "Get Component By Type",
"summary": "GetComponentByType",
"operationId": "get-component-by-type",
"parameters": [
{
"type": "string",
Expand Down Expand Up @@ -52,6 +53,7 @@
"application/json"
],
"summary": "CreateComponent",
"operationId": "create-component",
"parameters": [
{
"description": "Component data to create",
Expand Down Expand Up @@ -81,8 +83,9 @@
},
"/api/components/{id}": {
"get": {
"description": "Get Component By Type",
"summary": "GetComponentByType",
"description": "Get Component By ID",
"summary": "GetComponentByID",
"operationId": "get-component-by-id",
"parameters": [
{
"type": "integer",
Expand All @@ -108,8 +111,9 @@
}
},
"delete": {
"description": "Get Component By Type",
"summary": "GetComponentByType",
"description": "Delete Component By Id",
"summary": "DeleteComponentById",
"operationId": "delete-component-by-id",
"parameters": [
{
"type": "integer",
Expand Down Expand Up @@ -142,6 +146,7 @@
"application/json"
],
"summary": "CreateNetwork",
"operationId": "create-network",
"responses": {
"200": {
"description": "OK",
Expand All @@ -162,6 +167,7 @@
"application/json"
],
"summary": "DeleteNetwork",
"operationId": "delete-network",
"parameters": [
{
"type": "integer",
Expand Down
14 changes: 10 additions & 4 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ paths:
/api/components:
get:
description: Get Component By Type
operationId: get-component-by-type
parameters:
- description: Component type (e.g., l2geth)
in: query
Expand All @@ -71,6 +72,7 @@ paths:
consumes:
- application/json
description: Create a new component
operationId: create-component
parameters:
- description: Component data to create
in: body
Expand All @@ -92,7 +94,8 @@ paths:
summary: CreateComponent
/api/components/{id}:
delete:
description: Get Component By Type
description: Delete Component By Id
operationId: delete-component-by-id
parameters:
- description: Component ID
in: path
Expand All @@ -108,9 +111,10 @@ paths:
description: Bad Request
"500":
description: Internal Server Error
summary: GetComponentByType
summary: DeleteComponentById
get:
description: Get Component By Type
description: Get Component By ID
operationId: get-component-by-id
parameters:
- description: Component ID
in: path
Expand All @@ -126,10 +130,11 @@ paths:
description: Bad Request
"500":
description: Internal Server Error
summary: GetComponentByType
summary: GetComponentByID
/api/networks:
post:
description: Create a new network
operationId: create-network
produces:
- application/json
responses:
Expand All @@ -143,6 +148,7 @@ paths:
/api/networks/{id}:
delete:
description: Delete a network by id
operationId: delete-network
parameters:
- description: Network ID
in: path
Expand Down
14 changes: 10 additions & 4 deletions pkg/http/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

// @Summary CreateNetwork
// @Description Create a new network
// @ID create-network
// @Produce json
// @Success 200 {object} model.Network
// @Failure 500
Expand All @@ -27,6 +28,7 @@ func (s *HTTPServer) CreateNetwork(c *gin.Context) {

// @Summary DeleteNetwork
// @Description Delete a network by id
// @ID delete-network
// @Produce json
// @Param id path int true "Network ID"
// @Success 200 {object} object
Expand All @@ -51,6 +53,7 @@ func (s *HTTPServer) DeleteNetwork(c *gin.Context) {

// @Summary CreateComponent
// @Description Create a new component
// @ID create-component
// @Accept json
// @Produce json
// @Param input body object true "Component data to create"
Expand All @@ -74,6 +77,7 @@ func (s *HTTPServer) CreateComponent(c *gin.Context) {

// @Summary GetComponentByType
// @Description Get Component By Type
// @ID get-component-by-type
// @Param type query string true "Component type (e.g., l2geth)"
// @Param network_id query integer true "Network ID"// @Produce json
// @Success 200 {object} object
Expand All @@ -94,8 +98,9 @@ func (s *HTTPServer) GetComponentByType(c *gin.Context) {
}
}

// @Summary GetComponentByType
// @Description Get Component By Type
// @Summary GetComponentByID
// @Description Get Component By ID
// @ID get-component-by-id
// @Param id path int true "Component ID"
// @Success 200 {object} object
// @Failure 400
Expand All @@ -115,8 +120,9 @@ func (s *HTTPServer) GetComponentById(c *gin.Context) {
}
}

// @Summary GetComponentByType
// @Description Get Component By Type
// @Summary DeleteComponentById
// @Description Delete Component By Id
// @ID delete-component-by-id
// @Param id path int true "Component ID"
// @Success 200 {object} object
// @Failure 400
Expand Down