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

[WIP] Swagger for the API #180

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions cmd/gaia/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,27 @@ package main
import (
"os"

_ "github.com/gaia-pipeline/gaia/docs"
"github.com/gaia-pipeline/gaia/server"
)

// @title Gaia API
// @version 1.0
// @description This is the API that the Gaia Admin UI uses.
// @termsOfService https://github.com/gaia-pipeline/gaia/blob/master/LICENSE

// @contact.name API Support
// @contact.url https://github.com/gaia-pipeline/gaia

// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html

// @securityDefinitions.apiKey ApiKeyAuth
// @in header
// @name Authorization

// @host gaia-pipeline.io
// @BasePath /api/v1
func main() {
// Start the server.
if err := server.Start(); err != nil {
Expand Down
202 changes: 202 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// This file was generated by swaggo/swag at
// 2020-02-15 14:49:51.707312 +0100 CET m=+0.611981341

package docs

import (
"bytes"
"encoding/json"
"strings"

"github.com/alecthomas/template"
"github.com/swaggo/swag"
)

var doc = `{
"schemes": {{ marshal .Schemes }},
"swagger": "2.0",
"info": {
"description": "{{.Description}}",
"title": "{{.Title}}",
"termsOfService": "https://github.com/gaia-pipeline/gaia/blob/master/LICENSE",
"contact": {
"name": "API Support",
"url": "https://github.com/gaia-pipeline/gaia"
},
"license": {
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
},
"version": "{{.Version}}"
},
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/login": {
"post": {
"description": "Returns an authenticated user.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"summary": "User Login",
"parameters": [
{
"description": "UserLogin request",
"name": "UserLoginRequest",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/gaia.User"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/gaia.User"
}
},
"400": {
"description": "Bad Request",
"schema": {
"type": "string"
}
},
"403": {
"description": "Forbidden",
"schema": {
"type": "string"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"type": "string"
}
}
}
}
},
"/users": {
"get": {
"security": [
{
"ApiKeyAuth": []
}
],
"description": "Returns a list of registered users.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"summary": "Get all users",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/gaia.User"
}
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"type": "string"
}
}
}
}
}
},
"definitions": {
"gaia.User": {
"type": "object",
"properties": {
"display_name": {
"type": "string"
},
"jwtexpiry": {
"type": "integer"
},
"lastlogin": {
"type": "string"
},
"password": {
"type": "string"
},
"tokenstring": {
"type": "string"
},
"trigger_token": {
"type": "string"
},
"username": {
"type": "string"
}
}
}
},
"securityDefinitions": {
"ApiKeyAuth": {
"type": "apiKey",
"name": "Authorization",
"in": "header"
}
}
}`

type swaggerInfo struct {
Version string
Host string
BasePath string
Schemes []string
Title string
Description string
}

// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = swaggerInfo{
Version: "1.0",
Host: "gaia-pipeline.io",
BasePath: "/api/v1",
Schemes: []string{},
Title: "Gaia API",
Description: "This is the API that the Gaia Admin UI uses.",
}

type s struct{}

func (s *s) ReadDoc() string {
sInfo := SwaggerInfo
sInfo.Description = strings.Replace(sInfo.Description, "\n", "\\n", -1)

t, err := template.New("swagger_info").Funcs(template.FuncMap{
"marshal": func(v interface{}) string {
a, _ := json.Marshal(v)
return string(a)
},
}).Parse(doc)
if err != nil {
return doc
}

var tpl bytes.Buffer
if err := t.Execute(&tpl, sInfo); err != nil {
return doc
}

return tpl.String()
}

func init() {
swag.Register(swag.Name, &s{})
}
139 changes: 139 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
{
"swagger": "2.0",
"info": {
"description": "This is the API that the Gaia Admin UI uses.",
"title": "Gaia API",
"termsOfService": "https://github.com/gaia-pipeline/gaia/blob/master/LICENSE",
"contact": {
"name": "API Support",
"url": "https://github.com/gaia-pipeline/gaia"
},
"license": {
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
},
"version": "1.0"
},
"host": "gaia-pipeline.io",
"basePath": "/api/v1",
"paths": {
"/login": {
"post": {
"description": "Returns an authenticated user.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"summary": "User Login",
"parameters": [
{
"description": "UserLogin request",
"name": "UserLoginRequest",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/gaia.User"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/gaia.User"
}
},
"400": {
"description": "Bad Request",
"schema": {
"type": "string"
}
},
"403": {
"description": "Forbidden",
"schema": {
"type": "string"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"type": "string"
}
}
}
}
},
"/users": {
"get": {
"security": [
{
"ApiKeyAuth": []
}
],
"description": "Returns a list of registered users.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"summary": "Get all users",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/gaia.User"
}
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"type": "string"
}
}
}
}
}
},
"definitions": {
"gaia.User": {
"type": "object",
"properties": {
"display_name": {
"type": "string"
},
"jwtexpiry": {
"type": "integer"
},
"lastlogin": {
"type": "string"
},
"password": {
"type": "string"
},
"tokenstring": {
"type": "string"
},
"trigger_token": {
"type": "string"
},
"username": {
"type": "string"
}
}
}
},
"securityDefinitions": {
"ApiKeyAuth": {
"type": "apiKey",
"name": "Authorization",
"in": "header"
}
}
}
Loading