This service is build with Node.JS using the Express framework. It is intended to manage social media "Posts" to an ATP instance as JSON documents and uses Simple Oracle Document Access (SODA) to persist the data.
The DDL to create the database table looks like so:
Create the schema/user:
CREATE USER postsvc IDENTIFIED BY "STRONGPASSWORD";
GRANT create session TO postsvc;
GRANT create table TO postsvc;
GRANT create view TO postsvc;
GRANT create any trigger TO postsvc;
GRANT create any procedure TO postsvc;
GRANT create sequence TO postsvc;
GRANT create synonym TO postsvc;
GRANT soda_app TO postsvc;
GRANT UNLIMITED TABLESPACE TO postsvc;
For best results, the post-svc
should be tested in Docker locally so make sure you have Docker installed.
docker build -t phx.ocir.io/toddrsharp/cloud-native-microservice/post-svc .
Push:
docker push phx.ocir.io/toddrsharp/cloud-native-microservice/post-svc
docker run -d --rm \
--env DB_USER \
--env DB_PASSWORD \
--env CONNECT_STRING \
--env POST_COLLECTION \
--env ACCESS_TOKEN \
--env SECRET_KEY \
--env REGION \
--env STORAGE_TENANCY \
--env BUCKET \
-p 3000:3000 \
-t phx.ocir.io/toddrsharp/cloud-native-microservice/post-svc
Test the endpoints as described below
This service will persist social media "posts" as JSON documents. The general document format is as follows:
{
"userId": "[String]",
"title": "[String]",
"type": "[String - one of: text, image, link, video]",
"key": "[Optional - String]", //auto generated by service
"content": "[Optional - String]",
"postedOn": "[Date]"
}
Save a new post (image/video - returns 201 Created
):
curl -iX POST http://localhost:3000/post -F 'post={"userId":"8C561D58E856DD25E0532010000AF462", "title": "Hello", "type": "image", "postedOn": "2019-07-16T15:57:17"}' -F 'upload=@./build-resource/oracle_cloud.jpg'
HTTP/1.1 100 Continue
HTTP/1.1 201 Created
X-Powered-By: Express
Access-Control-Allow-Origin: *
Content-Type: application/json; charset=utf-8
Content-Length: 128
ETag: W/"80-GnZIOrxd8Lt1Zq1fRMs+zbzNT6k"
Date: Wed, 17 Jul 2019 01:24:50 GMT
Connection: keep-alive
{"id":"D0724C30A9804F85BFCADAC86DCC4F90","createdOn":"2019-07-17T01:24:52.475825Z","lastModified":"2019-07-17T01:24:52.475825Z"}
Save a new post (text/link - returns 201 Created
):
curl -iX POST http://localhost:3000/post -F 'post={"userId":"8C561D58E856DD25E0532010000AF462", "title": "Hello", "type": "text", "content": "hi", "postedOn": "2019-07-16T15:57:17"}' -F 'upload=@./post1.json'
HTTP/1.1 100 Continue
HTTP/1.1 201 Created
X-Powered-By: Express
Access-Control-Allow-Origin: *
Content-Type: application/json; charset=utf-8
Content-Length: 128
ETag: W/"80-GnZIOrxd8Lt1Zq1fRMs+zbzNT6k"
Date: Wed, 17 Jul 2019 01:24:50 GMT
Connection: keep-alive
{"id":"D0724C30A9804F85BFCADAC86DCC4F90","createdOn":"2019-07-17T01:24:52.475825Z","lastModified":"2019-07-17T01:24:52.475825Z"}
Save a new post with invalid data (returns 400 Bad Request
):
curl -iX POST http://localhost:3000/post -F 'post={"userId":"", "title": "", "type": "foo", "postedOn": "asdf"}'
HTTP/1.1 100 Continue
HTTP/1.1 400 Bad Request
X-Powered-By: Express
Access-Control-Allow-Origin: *
Content-Type: application/json; charset=utf-8
Content-Length: 666
ETag: W/"29a-SLEkJP0Cv1ai5ufY8tVHBsLPNt0"
Date: Thu, 18 Jul 2019 13:25:39 GMT
Connection: keep-alive
[{"message":"\"userId\" is not allowed to be empty","path":["userId"],"type":"any.empty","context":{"value":"","invalids":[""],"key":"userId","label":"userId"}},{"message":"\"userId\" must only contain alpha-numeric characters","path":["userId"],"type":"string.alphanum","context":{"value":"","key":"userId","label":"userId"}},{"message":"\"title\" is not allowed to be empty","path":["title"],"type":"any.empty","context":{"value":"","invalids":[""],"key":"title","label":"title"}},{"message":"\"postedOn\" must be a number of milliseconds or valid date string","path":["postedOn"],"type":"date.base","context":{"value":"asdf","key":"postedOn","label":"postedOn"}}]
Update an existing post (returns 200 OK
:
curl -iX PUT http://localhost:3000/post/D0724C30A9804F85BFCADAC86DCC4F90 -F 'post={"userId":"8C561D58E856DD25E0532010000AF462", "title": "Hello", "type": "text", "content": "hi, world", "postedOn": "2019-07-16T15:57:17"}'
HTTP/1.1 100 Continue
HTTP/1.1 200 OK
X-Powered-By: Express
Access-Control-Allow-Origin: *
Content-Type: application/json; charset=utf-8
Content-Length: 128
ETag: W/"80-50Ex5F4jwyiOTwJ8vVkRcFbiMEw"
Date: Wed, 17 Jul 2019 01:26:03 GMT
Connection: keep-alive
{"id":"D0724C30A9804F85BFCADAC86DCC4F90","createdOn":"2019-07-17T01:24:52.475825Z","lastModified":"2019-07-17T01:26:04.999894Z"}
Update an existing post with invalid data (returns 400 Bad Request
):
curl -iX PUT http://localhost:3000/post/D0724C30A9804F85BFCADAC86DCC4F90 -F 'post={"userId":"8C561D58E856DD25E0532010000AF462", "title": "", "type": "text", "content": "hi, world", "postedOn": "2019-07-16T15:57:17"}'
HTTP/1.1 100 Continue
HTTP/1.1 400 Bad Request
X-Powered-By: Express
Access-Control-Allow-Origin: *
Content-Type: application/json; charset=utf-8
Content-Length: 157
ETag: W/"9d-tLFV/E5pYY5/tXZV1iUIonhM1Ms"
Date: Thu, 18 Jul 2019 13:30:21 GMT
Connection: keep-alive
[{"message":"\"title\" is not allowed to be empty","path":["title"],"type":"any.empty","context":{"value":"","invalids":[""],"key":"title","label":"title"}}]
Get a post by ID:
curl -iX GET http://localhost:3000/post/11D60176464F4FD9BFD625FB79730575
HTTP/1.1 200 OK
X-Powered-By: Express
Access-Control-Allow-Origin: *
Content-Type: application/json; charset=utf-8
Content-Length: 272
ETag: W/"110-sl//10GolzZQt8TGgiprUaqBbWQ"
Date: Tue, 16 Jul 2019 20:13:08 GMT
Connection: keep-alive
{"id":"11D60176464F4FD9BFD625FB79730575","createdOn":"2019-07-16T20:09:26.174380Z","lastModified":"2019-07-16T20:11:00.330014Z","document":{"userId":"8C561D58E856DD25E0532010000AF462","title":"Hello","type":"text","content":"Hello World","postedOn":"2019-07-16T15:57:17"}}
Get all posts by user ID:
curl -iX GET http://localhost:3000/post/user/8C561D58E856DD25E0532010000AF462
HTTP/1.1 200 OK
X-Powered-By: Express
Access-Control-Allow-Origin: *
Content-Type: application/json; charset=utf-8
Content-Length: 1109
ETag: W/"455-A/bZ8d3i4xDiu0hpl7DwwBhUuCY"
Date: Tue, 16 Jul 2019 20:32:02 GMT
Connection: keep-alive
[{"id":"11D60176464F4FD9BFD625FB79730575","createdOn":"2019-07-16T20:09:26.174380Z","lastModified":"2019-07-16T20:11:00.330014Z","document":{"userId":"8C561D58E856DD25E0532010000AF462","title":"Hello","type":"text","content":"Hello World","postedOn":"2019-07-16T15:57:17"}},{"id":"8D648568F1144F8FBF50D06274B397A9","createdOn":"2019-07-16T20:05:23.400850Z","lastModified":"2019-07-16T20:07:49.205996Z","document":{"userId":"8C561D58E856DD25E0532010000AF462","title":"Hello","type":"text","content":"Hello World","postedOn":"2019-07-16T15:57:17"}},{"id":"E42FF88A25AC4F52BF2A891123A6414D","createdOn":"2019-07-16T20:00:23.096193Z","lastModified":"2019-07-16T20:00:23.096193Z","document":{"userId":"8C561D58E856DD25E0532010000AF462","title":"Hello","type":"text","content":"hi","postedOn":"2019-07-16T15:57:17","updatedOn":"2019-07-16T15:57:17"}},{"id":"294331C941774F17BF1871A8D80EB2E1","createdOn":"2019-07-16T20:03:17.748867Z","lastModified":"2019-07-16T20:03:17.748867Z","document":{"userId":"8C561D58E856DD25E0532010000AF462","title":"Hello","type":"text","content":"hi","postedOn":"2019-07-16T15:57:17"}}]
Get posts by user ID (paginated):
curl -iX GET http://localhost:3000/post/user/8C561D58E856DD25E0532010000AF462/0/1
HTTP/1.1 200 OK
X-Powered-By: Express
Access-Control-Allow-Origin: *
Content-Type: application/json; charset=utf-8
Content-Length: 274
ETag: W/"112-2WSahnJdgkVk/jjAPKnaIUQkFxc"
Date: Tue, 16 Jul 2019 20:29:01 GMT
Connection: keep-alive
[{"id":"11D60176464F4FD9BFD625FB79730575","createdOn":"2019-07-16T20:09:26.174380Z","lastModified":"2019-07-16T20:11:00.330014Z","document":{"userId":"8C561D58E856DD25E0532010000AF462","title":"Hello","type":"text","content":"Hello World","postedOn":"2019-07-16T15:57:17"}}]
Delete a post:
curl -iX DELETE http://localhost:3000/post/E42FF88A25AC4F52BF2A891123A6414D
HTTP/1.1 204 No Content
X-Powered-By: Express
Access-Control-Allow-Origin: *
Date: Tue, 16 Jul 2019 20:37:43 GMT
Connection: keep-alive
Delete a post that does not exist:
curl -iX DELETE http://localhost:3000/post/E42FF88A25AC4F52BF2A891123A6414D
HTTP/1.1 404 Not Found
X-Powered-By: Express
Access-Control-Allow-Origin: *
Date: Tue, 16 Jul 2019 20:37:45 GMT
Connection: keep-alive
Content-Length: 0
Create a secret to store necessary variables:
apiVersion: v1
kind: Secret
metadata:
name: post-svc-secrets
data:
dbUser: [Base 64 Encoded Value]
dbPassword: [Base 64 Encoded Value]
connectString: [Base 64 Encoded Value]
accessToken: [Base 64 Encoded Value]
secretKey: [Base 64 Encoded Value]
region: [Base 64 Encoded Value]
storageTenancy: [Base 64 Encoded Value]
bucket: [Base 64 Encoded Value]
---
Deploy secret with:
kubectl -f deploy secret.yaml
Create an app.yaml
(see example in this repo) and deploy with:
kubectl -f deploy app.yaml