Skip to content

Commit

Permalink
Gotta do it myself.
Browse files Browse the repository at this point in the history
  • Loading branch information
Doug Luce authored and dougluce committed Dec 20, 2015
1 parent 271e987 commit 23763f7
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 35 deletions.
66 changes: 38 additions & 28 deletions lib/api.ls
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,35 @@ require! {
'prelude-ls': {map}
fs
npid
'node-restify-validation': restifyValidation
'node-restify-swagger': restifySwagger
}

swagger =
* swagger: "2.0"
info:
title: "API for kvs.io",
description: "An API for using the kvs.io key-value store.",
version: "0.1"
consumes: ["text/plain; charset=utf-8", "application/json"]
produces: ["text/plain; charset=utf-8", "application/json"]
basePath: "/"
paths: {}

swaggerOperation = (path, cmd) ->
* operationId: "get#path"
tags: [path]
summary: cmd.summary
description: cmd.description
parameters: [p for p in cmd.params when not p['x-private']]
responses:
default:
description: "Invalid request."
schema:
"$ref": "#/definitions/Error"
200:
description: "Successful request."
schema:
"$ref": "#/definitions/Weather"

errors =
'bucket already exists': [restify.InternalServerError, "cannot create bucket."]
'not found': [restify.NotFoundError, "Entry not found."]
Expand Down Expand Up @@ -72,14 +97,10 @@ makeroutes = (server, logger) ->
docparams = {}
for parm in cm.params
docparams[parm.name] = parm
server.get do
url: "/#commandname#params"
swagger:
description: 'descripton'
notes: 'My hello call notes'
nickname: 'sayHelloCall'
validation: docparams
, handler
server.get "/#commandname#params" handler
swagger.paths[commandname] =
* get: swaggerOperation commandname, cm
post: swaggerOperation commandname, cm
server.post "/#commandname" handler

web_proxy = (req, res, next) ->
Expand All @@ -95,35 +116,22 @@ web_proxy = (req, res, next) ->
request.get options .pipe res
next!

swaggerJson = (req, res) ->
res.send JSON.stringify swagger

export init = (server, logobj) ->
logger = logobj
swagger.host = server.name
server.use setHeader
server.use restify.bodyParser!
server.use restify.CORS!
server.use restify.fullResponse!
server.use restifyValidation.validationPlugin errorsAsArray: false
restifySwagger.configure server, do
info:
contact: '[email protected]'
description: 'Description text'
license: 'MIT'
licenseUrl: 'http://opensource.org/licenses/MIT'
termsOfServiceUrl: 'http://opensource.org/licenses/MIT'
title: 'Node Restify Swagger Demo'
apiDescriptions:
get: 'GET-Api Resources'
post: 'POST-Api Resources'

server.get /^(|\/|\/index.html|\/w.*)$/ web_proxy
commands.init!
makeroutes server, logger
restifySwagger.loadRestifyRoutes!
server.get "/swagger.json" swaggerJson

server.get /^\/docs\/?.*/ restify.serveStatic directory: './swagger-ui'
server.get '/docs', (req, res, next) ->
res.header 'Location' '/docs/index.html'
res.send 302
next false
req, res, route, err <- server.on 'uncaughtException'
throw err

Expand Down Expand Up @@ -189,6 +197,7 @@ export standalone = ->
init server, logger

<- server.listen if is_prod then 80 else 8080

cli.start_upgrader server # Allow upgrades to CLI
logger.info '%s listening at %s', server.name, server.url

Expand All @@ -207,3 +216,4 @@ export standalone = ->

if !module.parent # Run stand-alone
standalone!

35 changes: 28 additions & 7 deletions lib/commands.ls
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ newbucket.params =
'x-private': true
newbucket.success = 201
newbucket.returnformatter = (w, bucket) -> w "Your new bucket is #bucket"
newbucket.doc = """
newbucket.summary = """
Create a new bucket.
"""
newbucket.description = """
Create a new bucket.
"""

Expand All @@ -118,7 +121,10 @@ listkeys.params =
required: true
...
listkeys.success = 200
listkeys.doc = """
listkeys.summary = """
List the keys in a bucket.
"""
listkeys.description = """
List the keys in a bucket.
"""

Expand All @@ -145,7 +151,10 @@ delbucket.params =
required: true
...
delbucket.success = 204
delbucket.doc = """
delbucket.summary = """
Delete a bucket.
"""
delbucket.description = """
Delete a bucket.
"""

Expand All @@ -167,7 +176,10 @@ setkey.params =
description: "The value."
required: true
setkey.success = 201
setkey.doc = """
setkey.summary = """
Set the value of a key in a bucket.
"""
setkey.description = """
Set the value of a key in a bucket.
"""

Expand Down Expand Up @@ -205,7 +217,10 @@ getkey.params =
description: "The key."
required: true
getkey.success = 200
getkey.doc = """
getkey.summary = """
Get the value of a key in a bucket.
"""
getkey.description = """
Get the value of a key in a bucket.
"""

Expand All @@ -228,7 +243,10 @@ delkey.params =
description: "The key to delete."
required: true
delkey.success = 204
delkey.doc = """
delkey.summary = """
Delete a key from a bucket.
"""
delkey.description = """
Delete a key from a bucket.
"""

Expand All @@ -255,7 +273,10 @@ findkeys.params =
description: "A substring to search for."
required: true
findkeys.success = 200
findkeys.doc = """
findkeys.summary = """
Find keys in a bucket that contain a given substring.
"""
findkeys.description = """
Find keys in a bucket that contain a given substring.
"""

Expand Down
7 changes: 7 additions & 0 deletions test/test-api.ls
Original file line number Diff line number Diff line change
Expand Up @@ -389,3 +389,10 @@ describe "API" ->
expect res.headers['content-type'], "/w content" .to.equal 'text/html'
expect data, "/w data" .to.not.be.empty
done!

describe 'swagger' ->
specify.only 'should have some swag' (done) ->
err, req, res, data <- client.get "/swagger.json"
swagger = JSON.parse data
expect swagger.swagger, 'swagger' .to.equal "2.0"
done!

0 comments on commit 23763f7

Please sign in to comment.