Skip to content

schollz/faas

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

71 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

faas (funciton-as-a-service)

Make any (Go) function into an API with one HTTP request.

This is a FaaS: functions as a service. But, in actuality, its more of a FaaSSS: functions as a stupidly simple service. Imagine iron.io/functions, or zeit/now or openfaas or apex or sky-island but more simple and more stupid.

Unlike others, this FaaS requires no coding, no init-ing, no pushing, no updating, and no bumping. You Just make one HTTP request with the name of the package, the name of the function, and any input. Right now it only works for Go. It can easily be extended to Python (WIP).

The codebase is only about 600 lines of code in total. Basically a request tells the server to fetch the function, determine the inputs/outputs and write a wrapper function that is served from a Docker container.

Examples

Try it right now with my instance at https://faas.schollz.com.

You can make (almost) any exported Go function into a API!

Run Md5Sum to get a md5 hash of "hello, world":

$ curl https://faas.schollz.com/?import=github.com/schollz/utils&func=Md5Sum(%22hello,%20world%22)
e4d7f1b4ed2e42d15898f4b27b019da4

Run IngredientsFromURL to get the ingredients from any website:

$ curl https://faas.schollz.com/?import=github.com/schollz/ingredients&func=IngredientsFromURL(%22https://cooking.nytimes.com/recipes/12320-apple-pie%22)
{"ingredients": [{"name":"butter","comment":"unsalted","measure":{"amount":2,"name":"tablespoons","cups":0.25}},{"name":"apples","measure":{"amount":2.5,"name":"pounds","cups":14.901182654402104}},{"name":"allspice","comment":"ground","measure":{"amount":0.25,"name":"teaspoon","cups":0.0104165}},{"name":"cinnamon","comment":"ground","measure":{"amount":0.5,"name":"teaspoon","cups":0.020833}},{"name":"salt","comment":"kosher","measure":{"amount":0.25,"name":"teaspoon","cups":0.0104165}},{"name":"sugar","comment":"plus 1 tablespoon","measure":{"amount":0.75,"name":"cup","cups":1.5}},{"name":"flour","comment":"all purpose","measure":{"amount":2,"name":"tablespoons","cups":0.25}},{"name":"cornstarch","measure":{"amount":2,"name":"teaspoons","cups":0.083332}},{"name":"apple cider vinegar","measure":{"amount":1,"name":"tablespoon","cups":0.125}},{"name":"pie dough","measure":{"amount":1,"name":"whole","cups":0}},{"name":"egg","measure":{"amount":1,"name":"whole","cups":0}}],"err": null

Run MarkdownToHTML from a Go playground:

$ curl -d '{"markdown":"*hello*,**world**"}' \
 	-H "Content-Type: application/json" -X POST \
 	https://faas.schollz.com/?import=https://play.golang.org/p/9xzE8Ivwupk.go&func=MarkdownToHTML
<p><em>hello</em>,<strong>world</strong></p>

Usage

You can use GET or POST to submit jobs.

For the GET requests the syntax is

/?import=IMPORTPATH&func=FUNCNAME(param1,param2...)

The IMPORTPATH is the import path (e.g. github.com/x/y) or a URL containing the file with the function. The FUNCNAME is the name of the function. Note, you do need to URL encode the strings so that FUNCNAME("hello, world") -> FUNCNAME(%22hello,%20world%22)

For the POST requests the syntax is:

/?import=IMPORTPATH&func=FUNCNAME

with the body with the inputs {"param":"value"}.

That's it! The first time you run it will take ~1 minute while the Docker image is built.

How does it work?

When you make a POST/GET request to the faas server it will locate the given function and given package and it will generate a Docker container that accepts a JSON input with the parameters and outputs a JSON containing the output variables. This Docker container automatically shuts down after some time of inactivity. Subsequent requests then will load the previously built container for use.

Host yourself

You need to install Docker, and make sure gzip is installed.

Then build faas with Go:

git clone https://github.com/schollz/faas
cd faas
go generate
go build -v

Now you can run:

./faas --debug

Now you can try it out:

curl http://localhost:8090/?import=github.com/schollz/utils&func=Md5Sum(%22hello,%20world%22)

OR post data:

curl -d '{"s":"hello, world"}' -H "Content-Type: application/json" -X POST http://localhost:8090/?import=github.com/schollz/utils&func=Md5Sum

Note that the JSON "s" comes from the function Md5Sum itself.

License

MIT