Blog API example (#377)

This commit is contained in:
Travis Reeder
2017-09-29 15:59:39 -07:00
committed by GitHub
parent a491d1bfde
commit 35bcc4e658
39 changed files with 218 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
# Blog API in 10 minutes
This is a simple blog API with a function to receive a list of posts and a function to create a post.
## Run it
```
# Start MySQL:
docker run --name mysql --net=host -p 3306:3306 -e MYSQL_ROOT_PASSWORD=pass -d mysql:8
docker run -it --rm --link mysql:mysql mysql mysql -ppass -hmysql -e "create database blog"
docker run -it --rm --link mysql:mysql mysql mysql -ppass -hmysql -e "show databases"
# create schema
fn run -e DB_USER=root -e DB_PASS=pass schema
# Test locally:
# Check if any posts, should be none
fn run -e DB_USER=root -e DB_PASS=pass posts
# Add one
cat post.json | fn run -e DB_USER=root -e DB_PASS=pass posts/create
# Check again
fn run -e DB_USER=root -e DB_PASS=pass posts
# Set app configs
fn apps config set blog DB_USER root
fn apps config set blog DB_PASS pass
# fn deploy it!
fn deploy --all
```
## TODO:
* [ ] Add some way to ignore funcs on deploy, ie: schema

View File

@@ -0,0 +1 @@
name: blog

View File

@@ -0,0 +1,22 @@
package main
import (
"encoding/json"
"fmt"
"os"
)
type Person struct {
Name string
}
func main() {
p := &Person{Name: "World"}
json.NewDecoder(os.Stdin).Decode(p)
mapD := map[string]string{
"message": fmt.Sprintf("Hello %s", p.Name),
"posts": "http://localhost:8080/r/blog/posts",
}
mapB, _ := json.Marshal(mapD)
fmt.Println(string(mapB))
}

View File

@@ -0,0 +1,5 @@
version: 0.0.20
runtime: go
entrypoint: ./func
build_image: ""
run_image: ""

View File

@@ -0,0 +1,4 @@
{
"title": "Blog Post 1",
"body": "This is the body. This is the body. This is the body. This is the body. This is the body. This is the body. "
}

View File

@@ -0,0 +1,5 @@
source 'https://rubygems.org'
gem 'json', '> 2'
gem 'sequel'
gem 'mysql2'

View File

@@ -0,0 +1,5 @@
source 'https://rubygems.org'
gem 'json', '> 2'
gem 'sequel'
gem 'mysql2'

View File

@@ -0,0 +1,20 @@
require 'json'
require 'sequel'
DB = Sequel.connect("mysql2://docker.for.mac.localhost/blog?user=#{ENV['DB_USER']}&password=#{ENV['DB_PASS']}")
payload = STDIN.read
if payload == ""
puts ({"error" => "Invalid input"}).to_json
exit 1
end
payload = JSON.parse(payload)
# create a dataset from the items table
items = DB[:posts]
# populate the table
items.insert(:title => payload['title'], :body => payload['body'])
puts ({"status"=>"success", "message" => "Post inserted successfully."}).to_json

View File

@@ -0,0 +1,5 @@
version: 0.0.8
runtime: ruby
entrypoint: ruby func.rb
build_image: treeder/ruby-mysql
run_image: treeder/ruby-mysql

View File

@@ -0,0 +1,26 @@
{
"tests": [
{
"input": {
"body": {
"name": "Johnny"
}
},
"output": {
"body": {
"message": "Hello Johnny"
}
}
},
{
"input": {
"body": ""
},
"output": {
"body": {
"message": "Hello World"
}
}
}
]
}

View File

@@ -0,0 +1,14 @@
require 'json'
require 'sequel'
DB = Sequel.connect("mysql2://docker.for.mac.localhost/blog?user=#{ENV['DB_USER']}&password=#{ENV['DB_PASS']}")
items = DB[:posts]
rlist = []
items.each_with_index do |x,i|
STDERR.puts "item: #{x}"
rlist << x
end
r = {posts: rlist}
puts r.to_json

View File

@@ -0,0 +1,5 @@
version: 0.0.2
runtime: ruby
entrypoint: ruby func.rb
build_image: treeder/ruby-mysql
run_image: treeder/ruby-mysql

View File

@@ -0,0 +1,26 @@
{
"tests": [
{
"input": {
"body": {
"name": "Johnny"
}
},
"output": {
"body": {
"message": "Hello Johnny"
}
}
},
{
"input": {
"body": ""
},
"output": {
"body": {
"message": "Hello World"
}
}
}
]
}

View File

@@ -0,0 +1,5 @@
source 'https://rubygems.org'
gem 'json', '> 2'
gem 'sequel'
gem 'mysql2'

View File

@@ -0,0 +1,10 @@
require "sequel"
DB = Sequel.connect("mysql2://docker.for.mac.localhost/blog?user=#{ENV['DB_USER']}&password=#{ENV['DB_PASS']}")
# create a posts table
DB.create_table :posts do
primary_key :id
String :title
String :body
end

View File

@@ -0,0 +1,5 @@
version: 0.0.2
runtime: ruby
entrypoint: ruby create.rb
build_image: treeder/ruby-mysql
run_image: treeder/ruby-mysql

View File

@@ -0,0 +1,26 @@
{
"tests": [
{
"input": {
"body": {
"name": "Johnny"
}
},
"output": {
"body": {
"message": "Hello Johnny"
}
}
},
{
"input": {
"body": ""
},
"output": {
"body": {
"message": "Hello World"
}
}
}
]
}