mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Examples changes (#201)
This commit is contained in:
1
examples/blog/.gitignore
vendored
1
examples/blog/.gitignore
vendored
@@ -0,0 +1 @@
|
||||
/func
|
||||
@@ -1,8 +1,5 @@
|
||||
FROM iron/go:dev
|
||||
FROM iron/go
|
||||
|
||||
ADD . $GOPATH/src/github.com/iron-io/functions/examples/blog
|
||||
WORKDIR $GOPATH/src/github.com/iron-io/functions/examples/blog
|
||||
ADD func .
|
||||
|
||||
RUN go get .
|
||||
|
||||
ENTRYPOINT ["go", "run", "main.go"]
|
||||
ENTRYPOINT ["./func"]
|
||||
@@ -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
|
||||
12
examples/blog/build.sh
Executable file
12
examples/blog/build.sh
Executable file
@@ -0,0 +1,12 @@
|
||||
#!/bin/bash
|
||||
set -ex
|
||||
|
||||
FUNCPKG=$(pwd | sed "s|$GOPATH/src/||")
|
||||
|
||||
# glide image to install dependencies
|
||||
../build-glide.sh
|
||||
docker run --rm -v "$PWD":/go/src/$FUNCPKG -w /go/src/$FUNCPKG glide up
|
||||
|
||||
# build image
|
||||
docker run --rm -v "$PWD":/go/src/$FUNCPKG -w /go/src/$FUNCPKG iron/go:dev go build -o func
|
||||
docker build -t iron/func-blog .
|
||||
3
examples/blog/functions.yml
Normal file
3
examples/blog/functions.yml
Normal file
@@ -0,0 +1,3 @@
|
||||
image: iron/func-blog
|
||||
build:
|
||||
- ./build.sh
|
||||
18
examples/blog/glide.lock
generated
Normal file
18
examples/blog/glide.lock
generated
Normal file
@@ -0,0 +1,18 @@
|
||||
hash: 5ed6b2d92ab59777802bcfbd57690d5cfb128160bca6cfa799abeb28a8baad4d
|
||||
updated: 2016-11-01T02:15:49.751792317Z
|
||||
imports:
|
||||
- name: github.com/dgrijalva/jwt-go
|
||||
version: d2709f9f1f31ebcda9651b03077758c1f3a0018c
|
||||
- name: golang.org/x/crypto
|
||||
version: 9477e0b78b9ac3d0b03822fd95422e2fe07627cd
|
||||
subpackages:
|
||||
- bcrypt
|
||||
- blowfish
|
||||
- name: gopkg.in/mgo.v2
|
||||
version: 3f83fa5005286a7fe593b055f0d7771a7dce4655
|
||||
subpackages:
|
||||
- bson
|
||||
- internal/json
|
||||
- internal/sasl
|
||||
- internal/scram
|
||||
testImports: []
|
||||
10
examples/blog/glide.yaml
Normal file
10
examples/blog/glide.yaml
Normal file
@@ -0,0 +1,10 @@
|
||||
package: github.com/iron-io/functions/examples/blog
|
||||
import:
|
||||
- package: github.com/dgrijalva/jwt-go
|
||||
version: ^3.0.0
|
||||
- package: golang.org/x/crypto
|
||||
subpackages:
|
||||
- bcrypt
|
||||
- package: gopkg.in/mgo.v2
|
||||
subpackages:
|
||||
- bson
|
||||
@@ -12,13 +12,12 @@ import (
|
||||
func HandlePostCreate(db *database.Database, auth map[string]interface{}) {
|
||||
var post *models.Post
|
||||
|
||||
err := json.Unmarshal([]byte(os.Getenv("PAYLOAD")), &post)
|
||||
if err != nil {
|
||||
fmt.Println("Invalid post")
|
||||
if err := json.NewDecoder(os.Stdin).Decode(&post); err != nil {
|
||||
fmt.Printf("Couldn't decode post JSON: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
post, err = db.SavePost(post)
|
||||
post, err := db.SavePost(post)
|
||||
if err != nil {
|
||||
fmt.Println("Couldn't save that post")
|
||||
return
|
||||
|
||||
@@ -13,8 +13,6 @@ import (
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
// import "github.com/iron-io/functions/examples/blog/database"
|
||||
|
||||
var jwtSignKey = []byte("mysecretblog")
|
||||
|
||||
type Response map[string]interface{}
|
||||
@@ -32,9 +30,9 @@ func SendError(err interface{}) {
|
||||
|
||||
func HandleToken(db *database.Database) {
|
||||
var login *models.User
|
||||
err := json.Unmarshal([]byte(os.Getenv("PAYLOAD")), &login)
|
||||
if err != nil {
|
||||
fmt.Println("Missing username and password")
|
||||
|
||||
if err := json.NewDecoder(os.Stdin).Decode(&login); err != nil {
|
||||
fmt.Printf("Couldn't decode login JSON: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
16
examples/blog/test.sh
Executable file
16
examples/blog/test.sh
Executable file
@@ -0,0 +1,16 @@
|
||||
#!/bin/bash
|
||||
set -x
|
||||
|
||||
./build.sh
|
||||
|
||||
# test it
|
||||
docker stop test-mongo-func
|
||||
docker rm test-mongo-func
|
||||
|
||||
docker run -p 27017:27017 --name test-mongo-func -d mongo
|
||||
|
||||
echo '{ "title": "My New Post", "body": "Hello world!", "user": "test" }' | docker run --rm -i -e METHOD=POST -e ROUTE=/posts -e CONFIG_DB=mongo:27017 --link test-mongo-func:mongo -e TEST=1 iron/func-blog
|
||||
docker run --rm -i -e METHOD=GET -e ROUTE=/posts -e CONFIG_DB=mongo:27017 --link test-mongo-func:mongo -e TEST=1 iron/func-blog
|
||||
|
||||
docker stop test-mongo-func
|
||||
docker rm test-mongo-func
|
||||
Reference in New Issue
Block a user