Skip to content

Commit

Permalink
Merge branch 'release/1.1' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
jcalvarezj committed Mar 21, 2021
2 parents e67945c + 8e4b99f commit 881e172
Show file tree
Hide file tree
Showing 11 changed files with 586 additions and 22 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Jikko-Golang

This small project is an HTTP web server on port 8080 that exposes endpoints for the purpose of exercise.
This small project is an HTTP web server on port 8080 that exposes endpoints for the purpose of exercise. Includes API documentation with [Swag](https://github.com/swaggo/swag) exposed on **/swagger**

## Previous requirements
## Requirements

A MySQL database server should be running locally with a **jikkodb** database, which should have the **user** table and a **jikkouser** user identified by **jikkopass**.
It is possible to import the **jikkodb.sql** script on the **jikkodb** database to create and fill the **user** table.
Expand All @@ -13,7 +13,11 @@ Run `go build` to compile, and then `./jikko-golang` to run the server

## Endpoints

### /arrays/
### /swagger

Presents API documentation for the other endpoints

### /arrays

This endpoint serves POST requests with the following JSON body:

Expand All @@ -36,7 +40,7 @@ If there is any missing or incorrect data, it will return a 400 status code with

> **Incorrect or missing data in request body**
### /users/
### /users

This endpoint serves either GET/POST requests, responding with an HTML page that displays a form to search user by id, and lists all the users in the database below

Expand Down
11 changes: 10 additions & 1 deletion arrays/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@ import (
"io/ioutil"
)

// ArraysHandler is the handler function for the /arrays resource POST request
// ArraysHandler godoc
// @Summary Serves the /arrays resource POST requests
// @Description Serves the /arrays resource POST requests by receiving a JSON object with an unsorted numbers array and responds with a JSON object with that array and its sorted version
// @Param unsorted body arrays.ArraysJSON true "The unsorted array of numbers"
// @Accept json
// @Produce json
// @Success 200 {object} string
// @Failure 400 {object} string
// @Failure 405 {object} string
// @Router /arrays [post]
func ArraysHandler(writer http.ResponseWriter, request *http.Request) {
var jsonData ArraysJSON
contents, err := ioutil.ReadAll(request.Body)
Expand Down
185 changes: 185 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// This file was generated by swaggo/swag

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}}",
"contact": {},
"version": "{{.Version}}"
},
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/arrays": {
"post": {
"description": "Serves the /arrays resource POST requests by receiving a JSON object with an unsorted numbers array and responds with a JSON object with that array and its sorted version",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"summary": "Serves the /arrays resource POST requests",
"parameters": [
{
"description": "The unsorted array of numbers",
"name": "unsorted",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/arrays.ArraysJSON"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
},
"400": {
"description": "Bad Request",
"schema": {
"type": "string"
}
},
"405": {
"description": "Method Not Allowed",
"schema": {
"type": "string"
}
}
}
}
},
"/users": {
"get": {
"description": "Renders the index.html template for the /users resource, showing all users and a form to query a user",
"produces": [
"text/html"
],
"summary": "Serves the /users resource",
"operationId": "get-all-users",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
}
}
}
},
"/users/{id}": {
"get": {
"description": "Renders the user.html template for the /users/{id} resource, showing the user by specified path variable {id}",
"produces": [
"text/html"
],
"summary": "Serves the /users/{id} resource,",
"operationId": "get-user-by-id",
"parameters": [
{
"type": "integer",
"description": "User ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
},
"500": {
"description": ""
}
}
}
}
},
"definitions": {
"arrays.ArraysJSON": {
"type": "object",
"properties": {
"sorted": {
"type": "array",
"items": {
"type": "integer"
}
},
"unsorted": {
"type": "array",
"items": {
"type": "integer"
}
}
}
}
}
}`

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: "localhost:8080",
BasePath: "/",
Schemes: []string{},
Title: "Jikko-Golang REST API",
Description: "This is an example Web server that sorts arrays and displays user info",
}

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{})
}
123 changes: 123 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
{
"swagger": "2.0",
"info": {
"description": "This is an example Web server that sorts arrays and displays user info",
"title": "Jikko-Golang REST API",
"contact": {},
"version": "1.0"
},
"host": "localhost:8080",
"basePath": "/",
"paths": {
"/arrays": {
"post": {
"description": "Serves the /arrays resource POST requests by receiving a JSON object with an unsorted numbers array and responds with a JSON object with that array and its sorted version",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"summary": "Serves the /arrays resource POST requests",
"parameters": [
{
"description": "The unsorted array of numbers",
"name": "unsorted",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/arrays.ArraysJSON"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
},
"400": {
"description": "Bad Request",
"schema": {
"type": "string"
}
},
"405": {
"description": "Method Not Allowed",
"schema": {
"type": "string"
}
}
}
}
},
"/users": {
"get": {
"description": "Renders the index.html template for the /users resource, showing all users and a form to query a user",
"produces": [
"text/html"
],
"summary": "Serves the /users resource",
"operationId": "get-all-users",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
}
}
}
},
"/users/{id}": {
"get": {
"description": "Renders the user.html template for the /users/{id} resource, showing the user by specified path variable {id}",
"produces": [
"text/html"
],
"summary": "Serves the /users/{id} resource,",
"operationId": "get-user-by-id",
"parameters": [
{
"type": "integer",
"description": "User ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
},
"500": {
"description": ""
}
}
}
}
},
"definitions": {
"arrays.ArraysJSON": {
"type": "object",
"properties": {
"sorted": {
"type": "array",
"items": {
"type": "integer"
}
},
"unsorted": {
"type": "array",
"items": {
"type": "integer"
}
}
}
}
}
}
Loading

0 comments on commit 881e172

Please sign in to comment.