Skip to content

Commit

Permalink
add swagger
Browse files Browse the repository at this point in the history
  • Loading branch information
rvhonorato committed Nov 27, 2024
1 parent b773df8 commit e6a1af4
Show file tree
Hide file tree
Showing 8 changed files with 581 additions and 67 deletions.
28 changes: 22 additions & 6 deletions controllers/queue/queue_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,18 @@ import (
"github.com/golang/glog"
)

// UploadJob is the controller for uploading a job to the queue
// UploadJob godoc
// @Summary Upload a new job to the queue
// @Description Validates and creates a new job in the system
// @Tags Jobs
// @Accept json
// @Produce json
// @Param job body jobs.Job true "Job to be uploaded"
// @Success 201 {object} jobs.Job "Job successfully created"
// @Failure 400 {object} errors.RestErr "Bad request - validation error"
// @Failure 500 {object} errors.RestErr "Internal server error"
// @Router /api/upload [post]
func UploadJob(c *gin.Context) {

var j jobs.Job

err := c.BindJSON(&j)
Expand All @@ -40,12 +49,20 @@ func UploadJob(c *gin.Context) {
}

c.JSON(http.StatusCreated, result)

}

// GetJob is the controller for getting a job from the queue
// RetrieveJob godoc
// @Summary Retrieve a job from the queue
// @Description Fetches a job by its ID with partial content handling
// @Tags Jobs
// @Produce json
// @Param id path string true "Job ID"
// @Success 200 {object} jobs.Job "Successfully retrieved job"
// @Success 206 {object} jobs.Job "Partially completed job"
// @Failure 404 {object} errors.RestErr "Job not found"
// @Failure 500 {object} errors.RestErr "Internal server error"
// @Router /api/get/{id} [get]
func RetrieveJob(c *gin.Context) {

id := c.Param("id")
j := jobs.Job{ID: id}

Expand All @@ -66,5 +83,4 @@ func RetrieveJob(c *gin.Context) {
default:
c.JSON(http.StatusOK, result)
}

}
7 changes: 6 additions & 1 deletion controllers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@ package router
import (
queue "jobd/controllers/queue"

// Import your local docs package
_ "jobd/docs"

"github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)

func SetupRouter() *gin.Engine {

gin.ForceConsoleColor()

r := gin.Default()
r.POST("/api/upload", queue.UploadJob)
r.GET("/api/get/:id", queue.RetrieveJob)

r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
return r
}
178 changes: 178 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
// Package docs Code generated by swaggo/swag. DO NOT EDIT
package docs

import "github.com/swaggo/swag"

const docTemplate = `{
"schemes": {{ marshal .Schemes }},
"swagger": "2.0",
"info": {
"description": "{{escape .Description}}",
"title": "{{.Title}}",
"contact": {},
"version": "{{.Version}}"
},
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/api/get/{id}": {
"get": {
"description": "Fetches a job by its ID with partial content handling",
"produces": [
"application/json"
],
"tags": [
"Jobs"
],
"summary": "Retrieve a job from the queue",
"parameters": [
{
"type": "string",
"description": "Job ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "Successfully retrieved job",
"schema": {
"$ref": "#/definitions/jobs.Job"
}
},
"206": {
"description": "Partially completed job",
"schema": {
"$ref": "#/definitions/jobs.Job"
}
},
"404": {
"description": "Job not found",
"schema": {
"$ref": "#/definitions/errors.RestErr"
}
},
"500": {
"description": "Internal server error",
"schema": {
"$ref": "#/definitions/errors.RestErr"
}
}
}
}
},
"/api/upload": {
"post": {
"description": "Validates and creates a new job in the system",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Jobs"
],
"summary": "Upload a new job to the queue",
"parameters": [
{
"description": "Job to be uploaded",
"name": "job",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/jobs.Job"
}
}
],
"responses": {
"201": {
"description": "Job successfully created",
"schema": {
"$ref": "#/definitions/jobs.Job"
}
},
"400": {
"description": "Bad request - validation error",
"schema": {
"$ref": "#/definitions/errors.RestErr"
}
},
"500": {
"description": "Internal server error",
"schema": {
"$ref": "#/definitions/errors.RestErr"
}
}
}
}
}
},
"definitions": {
"errors.RestErr": {
"type": "object",
"properties": {
"error": {
"type": "string"
},
"message": {
"type": "string"
},
"status": {
"type": "integer"
}
}
},
"jobs.Job": {
"type": "object",
"properties": {
"ID": {
"type": "string"
},
"Input": {
"type": "string"
},
"Slurml": {
"type": "boolean"
},
"lastUpdated": {
"type": "string"
},
"message": {
"type": "string"
},
"output": {
"type": "string"
},
"path": {
"type": "string"
},
"slurmID": {
"type": "integer"
},
"status": {
"type": "string"
}
}
}
}
}`

// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = &swag.Spec{
Version: "",
Host: "",
BasePath: "",
Schemes: []string{},
Title: "",
Description: "",
InfoInstanceName: "swagger",
SwaggerTemplate: docTemplate,
LeftDelim: "{{",
RightDelim: "}}",
}

func init() {
swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo)
}
Loading

0 comments on commit e6a1af4

Please sign in to comment.