Examples changes (#201)

This commit is contained in:
Pedro Nasser
2016-11-01 18:15:27 -02:00
committed by C Cirello
parent 570fdea062
commit 5d9269e186
91 changed files with 967 additions and 424 deletions

View File

@@ -1,8 +1,11 @@
# Blog API Example
A simple serverless blog API
## Requirements
- Remote MongoDB instance (for example heroku)
- Running IronFunctions API
## Development
@@ -13,18 +16,19 @@
USERNAME=YOUR_DOCKER_HUB_USERNAME
# build it
docker build -t $USERNAME/functions-blog .
docker run --rm -v "$PWD":/go/src/github.com/treeder/hello -w /go/src/github.com/treeder/hello iron/go:dev go build -o function
docker build -t $USERNAME/func-blog .
```
### Publishing it
### Publishing to DockerHub
```
# tagging
docker run --rm -v "$PWD":/app treeder/bump patch
docker tag $USERNAME/functions-blog:latest $USERNAME/functions-blog:`cat VERSION`
docker tag $USERNAME/func-blog:latest $USERNAME/func-blog:`cat VERSION`
# pushing to docker hub
docker push $USERNAME/functions-blog
docker push $USERNAME/func-blog
```
## Running it on IronFunctions
@@ -43,7 +47,13 @@ FUNCAPI=YOUR_FUNCTIONS_ADDRESS
MONGODB=YOUR_MONGODB_ADDRESS
```
### Creating our blog application in your IronFunctions
### Testing image
```
./test.sh
```
### Running with IronFunctions
With this command we are going to create an application with name `blog` and also defining the app configuration `DB`.
@@ -65,7 +75,7 @@ Now, we can create our blog routes:
```
curl -X POST --data '{
"route": {
"image": "'$USERNAME'/functions-blog",
"image": "'$USERNAME'/func-blog",
"path": "/posts"
}
}' http://$FUNCAPI/v1/apps/blog/routes
@@ -74,7 +84,7 @@ curl -X POST --data '{
```
curl -X POST --data '{
"route": {
"image": "'$USERNAME'/functions-blog",
"image": "'$USERNAME'/func-blog",
"path": "/posts/:id"
}
}' http://$FUNCAPI/v1/apps/blog/routes
@@ -83,15 +93,15 @@ curl -X POST --data '{
```
curl -X POST --data '{
"route": {
"image": "'$USERNAME'/functions-blog",
"image": "'$USERNAME'/func-blog",
"path": "/token"
}
}' http://$FUNCAPI/v1/apps/blog/routes
```
### Testing your Blog
#### Testing function
Now that we created our IronFunction route, lets test our routes
Now that we created our IronFunction route, let's test our routes
```
curl -X POST http://$FUNCAPI/r/blog/posts
@@ -99,9 +109,9 @@ curl -X POST http://$FUNCAPI/r/blog/posts
This command should return `{"error":"Invalid authentication"}` because we aren't sending any token.
## Authentication
#### Authentication
### Creating a blog user
##### Creating a blog user
First let's create our blog user. In this example an user `test` with password `test`.
@@ -109,7 +119,7 @@ First let's create our blog user. In this example an user `test` with password `
docker run --rm -e CONFIG_DB=$MONGODB -e NEWUSER='{ "username": "test", "password": "test" }' $USERNAME/functions-blog
```
### Getting authorization token
##### Getting authorization token
Now, to get authorized to post in our Blog API endpoints we must request a new token with a valid user.
@@ -129,6 +139,6 @@ Let's save that token in the environment
BLOG_TOKEN=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOiIyMDE2LTA5LTAxVDAwOjQzOjMxLjQwNjY5NTIxNy0wMzowMCIsInVzZXIiOiJ0ZXN0In0.aPKdH3QPauutFsFbSdQyF6q1hqTAas_BCbSYi5mFiSU
```
### Posting in your blog
##### Posting in your blog
curl -X POST --header "Authentication: JWT $BLOG_TOKEN" --data '{ "title": "My New Post", "body": "Hello world!" }' http://$FUNCAPI/r/blog/posts
curl -X POST --header "Authorization: Bearer $BLOG_TOKEN" --data '{ "title": "My New Post", "body": "Hello world!", "user": "test" }' http://$FUNCAPI/r/blog/posts