diff --git a/docs/api/restapi.md b/docs/api/restapi.md new file mode 100644 index 0000000..5ea2137 --- /dev/null +++ b/docs/api/restapi.md @@ -0,0 +1,118 @@ +# ranna main API +The ranna main REST API. + +## Version: 1.0 + +### /exec + +#### POST +##### Summary + +Get Spec Map + +##### Description + +Returns the available spec map. + +##### Parameters + +| Name | Located in | Description | Required | Schema | +| ---- | ---------- | ----------- | -------- | ---- | +| payload | body | The execution payload | Yes | [models.ExecutionRequest](#modelsexecutionrequest) | + +##### Responses + +| Code | Description | Schema | +| ---- | ----------- | ------ | +| 200 | OK | [models.ExecutionResponse](#modelsexecutionresponse) | +| 400 | Bad Request | [models.ErrorModel](#modelserrormodel) | +| 500 | Internal Server Error | [models.ErrorModel](#modelserrormodel) | + +### /info + +#### GET +##### Summary + +Get System Info + +##### Description + +Returns general system and version information. + +##### Responses + +| Code | Description | Schema | +| ---- | ----------- | ------ | +| 200 | OK | [models.ExecutionResponse](#modelsexecutionresponse) | +| 500 | Internal Server Error | [models.ErrorModel](#modelserrormodel) | + +### /spec + +#### GET +##### Summary + +Get Spec Map + +##### Description + +Returns the available spec map. + +##### Responses + +| Code | Description | Schema | +| ---- | ----------- | ------ | +| 200 | OK | [models.SpecMap](#modelsspecmap) | + +### Models + +#### models.ErrorModel + +| Name | Type | Description | Required | +| ---- | ---- | ----------- | -------- | +| code | integer | | No | +| context | string | | No | +| error | string | | No | + +#### models.ExecutionRequest + +| Name | Type | Description | Required | +| ---- | ---- | ----------- | -------- | +| arguments | [ string ] | | No | +| code | string | | No | +| environment | object | | No | +| inline_expression | boolean | | No | +| language | string | | No | + +#### models.ExecutionResponse + +| Name | Type | Description | Required | +| ---- | ---- | ----------- | -------- | +| exectimems | integer | | No | +| stderr | string | | No | +| stdout | string | | No | + +#### models.InlineSpec + +| Name | Type | Description | Required | +| ---- | ---- | ----------- | -------- | +| import_regex | string | | No | +| template | string | | No | + +#### models.Spec + +| Name | Type | Description | Required | +| ---- | ---- | ----------- | -------- | +| cmd | string | | No | +| entrypoint | string | | No | +| example | string | | No | +| filename | string | | No | +| image | string | | No | +| inline | [models.InlineSpec](#modelsinlinespec) | | No | +| registry | string | | No | +| use | string | | No | + +#### models.SpecMap + +| Name | Type | Description | Required | +| ---- | ---- | ----------- | -------- | +| models.SpecMap | object | | | diff --git a/docs/api/swagger.json b/docs/api/swagger.json new file mode 100644 index 0000000..5f3a595 --- /dev/null +++ b/docs/api/swagger.json @@ -0,0 +1,197 @@ +{ + "swagger": "2.0", + "info": { + "description": "The ranna main REST API.", + "title": "ranna main API", + "contact": {}, + "version": "1.0" + }, + "basePath": "/v1", + "paths": { + "/exec": { + "post": { + "description": "Returns the available spec map.", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "summary": "Get Spec Map", + "parameters": [ + { + "description": "The execution payload", + "name": "payload", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/models.ExecutionRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/models.ExecutionResponse" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/models.ErrorModel" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/models.ErrorModel" + } + } + } + } + }, + "/info": { + "get": { + "description": "Returns general system and version information.", + "produces": [ + "application/json" + ], + "summary": "Get System Info", + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/models.ExecutionResponse" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/models.ErrorModel" + } + } + } + } + }, + "/spec": { + "get": { + "description": "Returns the available spec map.", + "produces": [ + "application/json" + ], + "summary": "Get Spec Map", + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/models.SpecMap" + } + } + } + } + } + }, + "definitions": { + "models.ErrorModel": { + "type": "object", + "properties": { + "code": { + "type": "integer" + }, + "context": { + "type": "string" + }, + "error": { + "type": "string" + } + } + }, + "models.ExecutionRequest": { + "type": "object", + "properties": { + "arguments": { + "type": "array", + "items": { + "type": "string" + } + }, + "code": { + "type": "string" + }, + "environment": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "inline_expression": { + "type": "boolean" + }, + "language": { + "type": "string" + } + } + }, + "models.ExecutionResponse": { + "type": "object", + "properties": { + "exectimems": { + "type": "integer" + }, + "stderr": { + "type": "string" + }, + "stdout": { + "type": "string" + } + } + }, + "models.InlineSpec": { + "type": "object", + "properties": { + "import_regex": { + "type": "string" + }, + "template": { + "type": "string" + } + } + }, + "models.Spec": { + "type": "object", + "properties": { + "cmd": { + "type": "string" + }, + "entrypoint": { + "type": "string" + }, + "example": { + "type": "string" + }, + "filename": { + "type": "string" + }, + "image": { + "type": "string" + }, + "inline": { + "$ref": "#/definitions/models.InlineSpec" + }, + "registry": { + "type": "string" + }, + "use": { + "type": "string" + } + } + }, + "models.SpecMap": { + "type": "object", + "additionalProperties": { + "$ref": "#/definitions/models.Spec" + } + } + } +} \ No newline at end of file diff --git a/docs/api/swagger.yaml b/docs/api/swagger.yaml new file mode 100644 index 0000000..b2e4060 --- /dev/null +++ b/docs/api/swagger.yaml @@ -0,0 +1,128 @@ +basePath: /v1 +definitions: + models.ErrorModel: + properties: + code: + type: integer + context: + type: string + error: + type: string + type: object + models.ExecutionRequest: + properties: + arguments: + items: + type: string + type: array + code: + type: string + environment: + additionalProperties: + type: string + type: object + inline_expression: + type: boolean + language: + type: string + type: object + models.ExecutionResponse: + properties: + exectimems: + type: integer + stderr: + type: string + stdout: + type: string + type: object + models.InlineSpec: + properties: + import_regex: + type: string + template: + type: string + type: object + models.Spec: + properties: + cmd: + type: string + entrypoint: + type: string + example: + type: string + filename: + type: string + image: + type: string + inline: + $ref: '#/definitions/models.InlineSpec' + registry: + type: string + use: + type: string + type: object + models.SpecMap: + additionalProperties: + $ref: '#/definitions/models.Spec' + type: object +info: + contact: {} + description: The ranna main REST API. + title: ranna main API + version: "1.0" +paths: + /exec: + post: + consumes: + - application/json + description: Returns the available spec map. + parameters: + - description: The execution payload + in: body + name: payload + required: true + schema: + $ref: '#/definitions/models.ExecutionRequest' + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/models.ExecutionResponse' + "400": + description: Bad Request + schema: + $ref: '#/definitions/models.ErrorModel' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/models.ErrorModel' + summary: Get Spec Map + /info: + get: + description: Returns general system and version information. + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/models.ExecutionResponse' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/models.ErrorModel' + summary: Get System Info + /spec: + get: + description: Returns the available spec map. + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/models.SpecMap' + summary: Get Spec Map +swagger: "2.0" diff --git a/internal/api/v1/routes.go b/internal/api/v1/routes.go index 6140850..1954b56 100644 --- a/internal/api/v1/routes.go +++ b/internal/api/v1/routes.go @@ -18,6 +18,10 @@ var ( errEmptyCode = fiber.NewError(fiber.StatusBadRequest, "code is empty") ) +// @title ranna main API +// @version 1.0 +// @description The ranna main REST API. +// @basepath /v1 type Router struct { spec spec.Provider cfg config.Provider @@ -43,6 +47,12 @@ func (r *Router) optionsBypass(ctx *fiber.Ctx) error { return ctx.Next() } +// @summary Get System Info +// @description Returns general system and version information. +// @produce json +// @success 200 {object} models.ExecutionResponse +// @failure 500 {object} models.ErrorModel +// @router /info [get] func (r *Router) getInfo(ctx *fiber.Ctx) (err error) { sandboxInfo, err := r.manager.GetProvider().Info() if err != nil { @@ -57,10 +67,24 @@ func (r *Router) getInfo(ctx *fiber.Ctx) (err error) { return ctx.JSON(info) } +// @summary Get Spec Map +// @description Returns the available spec map. +// @produce json +// @success 200 {object} models.SpecMap +// @router /spec [get] func (r *Router) getSpec(ctx *fiber.Ctx) (err error) { return ctx.JSON(r.spec.Spec().GetSnapshot()) } +// @summary Get Spec Map +// @description Returns the available spec map. +// @accept json +// @produce json +// @param payload body models.ExecutionRequest true "The execution payload" +// @success 200 {object} models.ExecutionResponse +// @failure 400 {object} models.ErrorModel +// @failure 500 {object} models.ErrorModel +// @router /exec [post] func (r *Router) postExec(ctx *fiber.Ctx) (err error) { req := new(models.ExecutionRequest) if err = ctx.BodyParser(req); err != nil { diff --git a/scripts/apidocs.sh b/scripts/apidocs.sh new file mode 100644 index 0000000..d015a95 --- /dev/null +++ b/scripts/apidocs.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +OUTDIR="./docs/api" + +# https://github.com/swaggo/swag +swag init \ + -g ./internal/api/v1/routes.go \ + -o $OUTDIR \ + --parseDependency --parseDepth 2 + +rm -rf $OUTDIR/docs.go + +# https://github.com/syroegkin/swagger-markdown +swagger-markdown \ + -i $OUTDIR/swagger.json \ + -o $OUTDIR/restapi.md \ No newline at end of file diff --git a/spec/spec.yaml b/spec/spec.yaml index bcdee1c..4b5b31f 100644 --- a/spec/spec.yaml +++ b/spec/spec.yaml @@ -44,6 +44,14 @@ cpp: printf("Hello world!\n"); return 0; } + inline: + import_regex: '^#include ["<][\w/.]+[">]$' + template: |- + $${IMPORTS} + int main() { + $${CODE} + return 0; + } csharp: use: 'mono'