Coder Social home page Coder Social logo

scripts.servingtokens.com's Introduction

The Bash Builder takes an entrypoint of a Bash function, imports its dependencies, and bundles them into a serverless function.

A simple "hello world" example:

handler() {
	echo "Hello, from $(bash --version | head -n1)"
}

Demo: https://vercel.import.sh/api/hello

Usage

This example will detail creating an uppercase endpoint which will be accessed as https://my-deployment.url/api/uppercase. This endpoint will convert the provided querystring to uppercase using only Bash functions and standard Unix CLI tools.

Start by creating the project structure:

mkdir -p my-bash-project/api/uppercase
cd my-bash-project/api/uppercase

Inside the my-bash-project/api/uppercase directory, create an index.sh file with the following contents:

import "[email protected]"
import "[email protected]"

handler() {
	local path
	path="$(jq -r '.path' < "$1")"
	querystring "$path" | querystring_unescape | string_upper
}

The final step is to define a build that will take this entrypoint (index.sh), build it, and turn it into a serverless function using a vercel.json configuration in the root directory (my-bash-project):

{
	"version": 2,
	"functions": {
		"api/*.sh": { "runtime": "[email protected]" }
	}
}

Import can be configured by adding options to the import property of the config. The IMPORT_ prefix must not be set in this case:

{
	"version": 2,
	"functions": {
		"api/*.sh": {
			"runtime": "[email protected]",
			"config": {
				"import": {
					"DEBUG": "1"
				}
			}
		}
	}
}

By passing in a querystring, the endpoint will return the uppercased version.

Demo: https://vercel.import.sh/api/uppercase?hello%20world

Build Logic

If your serverless function requires additional files to be added into the final bundle, an optional build() function may be defined.

Any files added to the current working directory at build-time will be included in the output serverless function.

build() {
	date > build-time.txt
}

handler() {
	echo "Build time:   $(cat build-time.txt)"
	echo "Current time: $(date)"
}

Demo: https://vercel.import.sh/api/build

Response Headers

The default Content-Type is text/plain; charset=utf8 but you can change it by setting a response header.

handler() {
	http_response_header "Content-Type" "text/html; charset=utf8"
	echo "<h1>Current time</h1><p>$(date)</p>"
}

Demo: https://vercel.import.sh/api/response-headers

JSON Response

It is common for serverless functions to communicate via JSON, so you can use the http_response_json function to set the Content-Type to application/json; charset=utf8.

handler() {
	http_response_json
	echo "{ \"title\": \"Current time\", \"body\": \"$(date)\" }"
}

Demo: https://vercel.import.sh/api/response-json

Status Code

The default status code is 200 but you can change it with the http_response_code function.

handler() {
	http_response_code 500
	echo "Internal Server Error from Bash!"
}

Demo: https://vercel.import.sh/api/response-status-code

Redirect

You can use the http_response_redirect function to set the location and, optionally, the status code. The default status code is 302 (temporary redirect) but you could use a permanent redirect by setting the second argument to 301.

handler() {
	http_response_redirect "https://twitter.com/vercel" 301
	echo "Redirecting..."
}

Demo: https://vercel.import.sh/api/redirect

Importing Dependencies

Bash, by itself, is not very useful for writing serverless function handler logic because it does not have a standard library. For this reason, import is installed and configured by default, which allows your script to easily include additional functionality and helper logic.

For example, the querystring import may be used to parse input parameters from the request URL:

import "[email protected]"

handler() {
	local path
	local query
	path="$(jq -r '.path' < "$1")"
	query="$(querystring "$path")"
	echo "Querystring is: $query"
}

Demo: https://vercel.import.sh/api/querystring?a=b

Bash Version

With the Bash Builder, the handler script is executed using GNU Bash 4.

handler() {
	bash --version
}

Demo: https://vercel.import.sh/api/bash-version

scripts.servingtokens.com's People

Contributors

divopsresources avatar

Watchers

 avatar

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.