Code Monkey home page Code Monkey logo

go-serverless's Introduction

go-serverless - Run Docker containers as serverless funcitons

This application is in active development

This means that some features might not be quite polished yet. Expect bugs, but feel free to report them here.


What is this

go-serverless is an application that makes it easy to run Docker containers as if they were serverless functions through a simple HTTP API. It exposes a set of API endpoints to both manage these functions as well as call them.

How do I create these serverless function images?

You start with putting together a regular Docker image. The image itself can use any language and runtime you can think of, from Go to Rust to Node.js to COBOL if you're feeling like a Chad. As long as you can run it in Docker, you should be good to go.

Once you have your Docker image, you should get to actually building out your serverless function. Since your serverless function container will be working over an HTTP api, it will be given the JSON request body and you are expected to return a JSON response.

When running your code, the following environment variables will be set:

# REQUEST_ID will be a UUID unique to this function call
REQUEST_ID="00000000"

# CONTEXT_PATH will be based off of the REQUEST_ID
# and is what you will need to use
CONTEXT_PATH="/tmp/context/00000000"

Additionally, two files are also passed to the container:

# The request body passed to your function
$CONTEXT_PATH/request.json

# The resulting response of your function
$CONTEXT_PATH/response.json

Your container should read the request.json file as necessary, do what it do, and write the response into response.json. Once your container exits, the response will be passed to the function caller through the HTTP API.

You can have a look at the hello-world function for inspiration.

Getting started

There's a few ways to run go-serverless:

  • Running it directly on your machine
  • Running it directly on docker
  • Running it with docker compose

1. Running it directly on your machine

This method assumes that you at the very least have git and go installed.

Start by cloning the directory

git clone https://github.com/xprnio/go-serverless
cd go-serverless

If you have make installed, you can just run make run which will both build and run the app

make run

If you don't have make installed and don't want to, you can also just build it directly

mkdir bin
go build \
  -o bin/go-serverless \
  cmd/main.go

# Start the application
./bin/go-serverless

2. Running it directly on docker

The below environment variables are optional, as they are also included in the Dockerfile and are already set to the values shown below. However, if you want to use a different volume for the request contexts, you will need to modify both the environment variables as well as the volume mount.

docker run -d \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -e CONTEXT_NAME=serverless_context \
  -e CONTEXT_PATH=/tmp/context \
  -v serverless_context:/tmp/context \
  -v data:/data \
  -p 9999:9999 \
  ghcr.io/xprnio/go-serverless

3. Running it with docker compose

This method is probably the most convenient way of getting started since it makes configuring the app much more easier.

Start by grabbing the docker-compose.yml

curl -o docker-compose.yml \
  https://raw.githubusercontent.com/xprnio/go-serverless/main/docker-compose.yml

Once that is downloaded, just run the following

docker compose up -d

Using the API

Listing functions

curl http://localhost:9999/v1/functions

Listing routes

curl http://localhost:9999/v1/routes

Creating functions

To create a function, you will need an image. Currently only public registries are supported.

curl http://localhost:9999/v1/functions -d '{
  "image": "ghcr.io/xprnio/serverless-hello-world:latest"
}'

Creating routes

Once you have created the function, you can route it to a specific path through it's id.

curl http://localhost:9999/v1/routes -d '{
  "path": "/hello-world",
  "function_id": "00000000-0000-0000-0000-000000000000"
}'

Running functions

Once you have routed your function, you can call it from /r/{path}

curl http://localhost:9999/r/hello-world -d '{
  "message": "hello-world"
}'

go-serverless's People

Contributors

xprnio avatar

Stargazers

Tim Kersey avatar  avatar  avatar Karl Soone avatar Dammian Miller avatar

Watchers

 avatar

go-serverless's Issues

Implement route management API

  • Listing routes
    GET /v1/routes
  • Creating routes
    POST /v1/routes
  • Modifying routes
    PUT /v1/routes/:id
  • Deleting routes
    DELETE /v1/routes/:id

Add support for OCI/containerd

Instead of forcing users to use Docker, it should be possible for them to use whatever containerd implementation they want.

  • Pulling images
  • Creating volumes
  • Running containers

Implement support for authenticated registries

Currently only pulling from unauthenticated registries is supported. However, registries can either require authentication for all images or just specific images.

  • Add support for managing registries
    • Listing registries
    • Creating registries
    • Updating registries
    • Deleting registries
  • Add support for authenticating with registries

DRAFT: Improve documentation

Currently the only documentation available is the README.

  • Creating images capable of running as functions
  • Application administration
    • Managing functions
    • Managing routes
  • Calling functions

Implement support for managing and tracking images

It should be possible to both manage and track images used within the system.

  • Listing all images
  • Pulling images
  • Labeling images when pulled
    To indicate that an image is used as a function, a label should be added to it
  • Getting image details
    Since multiple versions of an image can exist, all versions should be included in the details
  • Pulling private images
    Since images can be private, authentication should be supported on an image level

Add support for function environment variables

Since functions themselves currently depend solely on the request body, it would make sense to allow functions to be pre-configured with environment variables.

  • Managing function environment variables
  • Passing environment variables to containers

Add support for monitoring function calls

Currently all function calls are only somewhat visible from the application logs themselves.
It would make sense to have a way to keep track of function calls, their inputs and outputs, as well as any logs they produce.

  • Viewing application logs
  • Viewing function invocations
  • Viewing function invocation logs

Add support for pre-configured environments

It should be possible to create and manage environments which would allow for attaching volumes to function calls automatically, either to share a cache or other data.

  • Managing environments
    • Listing environments
    • Creating environments
    • Updating environments
    • Deleting environments
    • Attaching environments to function calls and routes
  • Managing environment variables
  • Managing environment volumes
    • Listing volumes
    • Creating volumes
    • Deleting volumes
    • Attaching volumes to environments

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.