You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I found AWS lambda in July 2015 and became a big fan of Function as a Service (FaaS) since then, Three years ago I built my own FaaS framework fx at a Go Hackathon, then I published it on Hacker News, it quickly became one of Github trending repositories and pull in 700+ stars in one day.
While I was a little bloated, I struggled to find real-world scenario for it. This week I decided to become my own user of it, so I built an Web App with fx, and fx is amazingly handy in building APIs with FaaS way.
Hello World
fx is a simple tool I build to simplify the API development, let's take a look at how easy we build an API with fx.
You define an API in func.js , looks like this.
module.exports=(ctx)=>{ctx.body='hello world'}
Then you can deploy your function to be a service with fx with one command.
$ fx up --name helloworld --port 3000 func.js
Let's test it with curl.
$ curl -v 127.0.0.1:8080
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 11
Content-Type: text/plain; charset=utf-8
Date: Tue, 06 Aug 2019 15:58:41 GMT
hello world
fx in fleself.com
fleself.com is a website (UI and APIs) are totally written with JavaScript/Node, all the APIs are built with fx.
Architecture
This's overall APIs code structure of fleself.com,
And each source code of an API is just a function, take /api/tweets/delete as example,
const{ Client }=require('pg')constjwt=require('jsonwebtoken')constk='key_xxxxxxxx'constcreate=async(ctx)=>{const{ id }=ctx.request.bodyif(!ctx.headers.authorization){ctx.throw(403,'token required')return}letuser=nulltry{consttoken=ctx.headers.authorization.split(' ')[1]user=jwt.verify(token,Buffer.from(k,'base64'))}catch(e){ctx.throw(e.status||403,e.text)return}try{constclient=newClient()awaitclient.connect()constres=awaitclient.query({text: `DELETE FROM tweets WHERE id=$1 AND user_id=$2`,values: [id,user.id],})awaitclient.end()ctx.body='deleted'}catch(e){console.warn(e)awaitclient.end()ctx.status=500}}module.exports=create
GitHub Actions Workflows
The whole development process is managed on GitHub, and I use GitHub Actions to do CI/CD, its GitHub Actions workflow looks like this.
name: apion:
push:
paths:
- '.github/**'
- 'services/**'branches:
- masterjobs:
api:
runs-on: ubuntu-lateststeps:
- name: check outuses: actions/checkout@master
- name: use Node.js ${{ matrix.node-version }}uses: actions/setup-node@v1with:
node-version: ${{ matrix.node-version }}
- name: install SSH keyuses: shimataro/ssh-key-action@v2with:
key: ${{ secrets.SSH_KEY }}name: id_rsaknown_hosts: ${{ secrets.KNOWN_HOSTS }}
- name: install fxrun: | curl -o- https://raw.githubusercontent.com/metrue/fx/master/scripts/install.sh | sudo bash fx -v
- name: add fx hostrun: | fx infra create --name <node_name> --type docker --host root@<xxx.xxx.xxx.xxx> fx use <node_name>
- name: deploy tweets_createrun: | fx up -n tweetscreate -p 6000 services/tweets/create/fx.js --force
Caddy as Services Proxy
And I use Caddy as the frontend of the service, the config looks like this,
I found AWS lambda in July 2015 and became a big fan of Function as a Service (FaaS) since then, Three years ago I built my own FaaS framework fx at a Go Hackathon, then I published it on Hacker News, it quickly became one of Github trending repositories and pull in 700+ stars in one day.
While I was a little bloated, I struggled to find real-world scenario for it. This week I decided to become my own user of it, so I built an Web App with fx, and fx is amazingly handy in building APIs with FaaS way.
Hello World
fx is a simple tool I build to simplify the API development, let's take a look at how easy we build an API with fx.
You define an API in
func.js
, looks like this.Then you can deploy your function to be a service with fx with one command.
Let's test it with
curl
.$ curl -v 127.0.0.1:8080 HTTP/1.1 200 OK Connection: keep-alive Content-Length: 11 Content-Type: text/plain; charset=utf-8 Date: Tue, 06 Aug 2019 15:58:41 GMT hello world
fx in fleself.com
fleself.com is a website (UI and APIs) are totally written with JavaScript/Node, all the APIs are built with fx.
Architecture
This's overall APIs code structure of fleself.com,
And each source code of an API is just a function, take
/api/tweets/delete
as example,GitHub Actions Workflows
The whole development process is managed on GitHub, and I use GitHub Actions to do CI/CD, its GitHub Actions workflow looks like this.
Caddy as Services Proxy
And I use Caddy as the frontend of the service, the config looks like this,
The text was updated successfully, but these errors were encountered: