mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Blog API example (#377)
This commit is contained in:
5
examples/apps/blog/posts/Gemfile
Normal file
5
examples/apps/blog/posts/Gemfile
Normal file
@@ -0,0 +1,5 @@
|
||||
source 'https://rubygems.org'
|
||||
|
||||
gem 'json', '> 2'
|
||||
gem 'sequel'
|
||||
gem 'mysql2'
|
||||
5
examples/apps/blog/posts/create/Gemfile
Normal file
5
examples/apps/blog/posts/create/Gemfile
Normal file
@@ -0,0 +1,5 @@
|
||||
source 'https://rubygems.org'
|
||||
|
||||
gem 'json', '> 2'
|
||||
gem 'sequel'
|
||||
gem 'mysql2'
|
||||
20
examples/apps/blog/posts/create/func.rb
Normal file
20
examples/apps/blog/posts/create/func.rb
Normal 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
|
||||
5
examples/apps/blog/posts/create/func.yaml
Normal file
5
examples/apps/blog/posts/create/func.yaml
Normal 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
|
||||
26
examples/apps/blog/posts/create/test.json
Normal file
26
examples/apps/blog/posts/create/test.json
Normal file
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"tests": [
|
||||
{
|
||||
"input": {
|
||||
"body": {
|
||||
"name": "Johnny"
|
||||
}
|
||||
},
|
||||
"output": {
|
||||
"body": {
|
||||
"message": "Hello Johnny"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"input": {
|
||||
"body": ""
|
||||
},
|
||||
"output": {
|
||||
"body": {
|
||||
"message": "Hello World"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
14
examples/apps/blog/posts/func.rb
Normal file
14
examples/apps/blog/posts/func.rb
Normal 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
|
||||
5
examples/apps/blog/posts/func.yaml
Normal file
5
examples/apps/blog/posts/func.yaml
Normal 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
|
||||
26
examples/apps/blog/posts/test.json
Normal file
26
examples/apps/blog/posts/test.json
Normal file
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"tests": [
|
||||
{
|
||||
"input": {
|
||||
"body": {
|
||||
"name": "Johnny"
|
||||
}
|
||||
},
|
||||
"output": {
|
||||
"body": {
|
||||
"message": "Hello Johnny"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"input": {
|
||||
"body": ""
|
||||
},
|
||||
"output": {
|
||||
"body": {
|
||||
"message": "Hello World"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user