Self-hosted serverless/faas blogging clap api
$ go build
Please do run the test before pull request,
$ FAUNADB_SECRET_KEY=test REDIS_HOST=test bash -c 'go test ./...'
Mock file generated from mockery, https://github.com/vektra/mockery
You can use this api for your own web component or playing with my custom-made clap button https://github.com/kh411d/clap-it
Pick one of these databases provided, if you set both FaunaDB and Redis env vars, then FaunaDB will be the most likely to be chosen.
- Go to Fauna.com web console
- Create a collection, name it as
claps
, it will be used as key-value documents - Create index for
claps
collection,- Name it as
url_idx
- Set terms value as
data.url
- Tick mark the unique box.
- Name it as
- Create a database access key from the Security tab in the left navigation, make sure the role is set to Admin, this access key needs to be set later on
FAUNADB_SECRET_KEY
env variable.
- Go to Lambda.store web console
- Create a new database, name it as you like.
- Click on the database that you've just created, take a note for Endpoint, Port, and Password, these credentials need to be set later on
REDIS_HOST
andREDIS_ PASSWORD
env variables.
Set these vars on any serverless provider you choose,
FAUNADB_SECRET_KEY
REDIS_HOST
([ENDPOINT]:[PORT]
i.e.us1-xxxxx-xxxx-32223.lambda.store:32223
)REDIS_PASSWORD
URL_HOST
(i.e. khal.web.id, used to validate the hostname)
Netlify only, edit GO_IMPORT_PATH
value on netlify.toml
file corresponds to your Github account
Build locally, don't forget to set your env vars,
$ go build -i
$ ./clapi
$ curl -X POST -d '2' http://0.0.0.0:3000/?url=http://clapi/clap
$ curl -X GET http://0.0.0.0:3000/?url=http://clapi/clap
7
Go files in the /api
directory that export a function matching the net/http Go API will be served as Serverless Functions.
//ServeHTTP net/http handler
func ServeHTTP(w http.ResponseWriter, r *http.Request) {
...
}
After deployed, as example you may access your function as this,
Base API URL: [YOUR-VERCEL-DOMAIN]/api/handler
$ curl -X POST -d '2' https://42kl24j.vercel.app/api/handler?url=http://clapi/clap
$ curl -X GET https://42kl24j.vercel.app/api/handler?url=http://clapi/clap
7
Netlify can build your source Go functions into AWS Lambda compatible binaries.
Before building your Go source, Netlify needs to know the expected Go import path for your project. Use the GO_IMPORT_PATH
environment variable to set the right import path. You can do this in your netlify.toml file. The path value should point to your source repository on your Git provider, for example github.com/kh411d/clapi.
After successful deployment, you need to add env variables required for Redis in
Settings > Build & deploy > Environment > Environment variables
and then re-deploy the app from,
Deploys > Trigger deploy > Deploy site
After deployed, as example you may access your function as this,
Base API URL: [YOUR-NETLIFY-DOMAIN]/.netlify/functions/clapi
$ curl -X POST -d '2' https://flamboyant-khorana-5b394b.netlify.app/.netlify/functions/clapi?url=http://clapi/clap
$ curl -X GET https://flamboyant-khorana-5b394b.netlify.app/.netlify/functions/clapi?url=http://clapi/clap
$ 7
I haven't tried this myself but it seems very possible, you need to have this flyctl
tool installed on your system, upon installing, you have to register for fly.io account, simply follow the guidelines Installing flyctl, and then continue with Build, Deploy, and Run a Go Application
flyctl init
will create a fly.toml
file, Select Go (Go Builtin) for the builder, and then get ready for deployment flyctl deploy
, keep in mind that flyctl will create docker images on your local computer, it will download all required images such as golang image approx 1gb, so make sure you have good storage space and internet connection.
flyctl open
will open a browser on the HTTP version of the site.