github.com/iron-io/functions@v0.0.0-20180820112432-d59d7d1c40b2/examples/blog/README.md (about) 1 # Blog API Example 2 3 A simple serverless blog API 4 5 ## Requirements 6 7 - Remote MongoDB instance (for example heroku) 8 - Running IronFunctions API 9 10 ## Development 11 12 ### Building image locally 13 14 ``` 15 # SET BELOW TO YOUR DOCKER HUB USERNAME 16 USERNAME=YOUR_DOCKER_HUB_USERNAME 17 18 # build it 19 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 20 docker build -t $USERNAME/func-blog . 21 ``` 22 23 ### Publishing to DockerHub 24 25 ``` 26 # tagging 27 docker run --rm -v "$PWD":/app treeder/bump patch 28 docker tag $USERNAME/func-blog:latest $USERNAME/func-blog:`cat VERSION` 29 30 # pushing to docker hub 31 docker push $USERNAME/func-blog 32 ``` 33 34 ## Running it on IronFunctions 35 36 First you need a running IronFunctions API 37 38 ### First, let's define this environment variables 39 40 ``` 41 # Set your Function server address 42 # Eg. 127.0.0.1:8080 43 FUNCAPI=YOUR_FUNCTIONS_ADDRESS 44 45 # Set your mongoDB server address 46 # Eg. 127.0.0.1:27017/blog 47 MONGODB=YOUR_MONGODB_ADDRESS 48 ``` 49 50 ### Testing image 51 52 ``` 53 ./test.sh 54 ``` 55 56 ### Running with IronFunctions 57 58 With this command we are going to create an application with name `blog` and also defining the app configuration `DB`. 59 60 ``` 61 curl -X POST --data '{ 62 "app": { 63 "name": "blog", 64 "config": { "DB": "'$MONGODB'" } 65 } 66 }' http://$FUNCAPI/v1/apps 67 ``` 68 69 Now, we can create our blog routes: 70 71 - `/posts` - to create (authenticated) and list posts 72 - `/posts/:id` - to read post 73 - `/token` - to get a JWT 74 75 ``` 76 curl -X POST --data '{ 77 "route": { 78 "image": "'$USERNAME'/func-blog", 79 "path": "/posts" 80 } 81 }' http://$FUNCAPI/v1/apps/blog/routes 82 ``` 83 84 ``` 85 curl -X POST --data '{ 86 "route": { 87 "image": "'$USERNAME'/func-blog", 88 "path": "/posts/:id" 89 } 90 }' http://$FUNCAPI/v1/apps/blog/routes 91 ``` 92 93 ``` 94 curl -X POST --data '{ 95 "route": { 96 "image": "'$USERNAME'/func-blog", 97 "path": "/token" 98 } 99 }' http://$FUNCAPI/v1/apps/blog/routes 100 ``` 101 102 #### Testing function 103 104 Now that we created our IronFunction route, let's test our routes 105 106 ``` 107 curl -X POST http://$FUNCAPI/r/blog/posts 108 ``` 109 110 This command should return `{"error":"Invalid authentication"}` because we aren't sending any token. 111 112 #### Authentication 113 114 ##### Creating a blog user 115 116 First let's create our blog user. In this example an user `test` with password `test`. 117 118 ``` 119 docker run --rm -e DB=$MONGODB -e NEWUSER='{ "username": "test", "password": "test" }' $USERNAME/functions-blog 120 ``` 121 122 ##### Getting authorization token 123 124 Now, to get authorized to post in our Blog API endpoints we must request a new token with a valid user. 125 126 ``` 127 curl -X POST --data '{ "username": "test", "password": "test" }' http://$FUNCAPI/r/blog/token 128 ``` 129 130 This will output a token like this: 131 132 ``` 133 {"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOiIyMDE2LTA5LTAxVDAwOjQzOjMxLjQwNjY5NTIxNy0wMzowMCIsInVzZXIiOiJ0ZXN0In0.aPKdH3QPauutFsFbSdQyF6q1hqTAas_BCbSYi5mFiSU"} 134 ``` 135 136 Let's save that token in the environment 137 138 ``` 139 BLOG_TOKEN=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOiIyMDE2LTA5LTAxVDAwOjQzOjMxLjQwNjY5NTIxNy0wMzowMCIsInVzZXIiOiJ0ZXN0In0.aPKdH3QPauutFsFbSdQyF6q1hqTAas_BCbSYi5mFiSU 140 ``` 141 142 ##### Posting in your blog 143 144 curl -X POST --header "Authorization: Bearer $BLOG_TOKEN" --data '{ "title": "My New Post", "body": "Hello world!", "user": "test" }' http://$FUNCAPI/r/blog/posts