Language detection API now live with DigitalOcean functions!

Introduction

DigitalOcean recently launched their offering of functions as a service [similar to AWS lambda]. The underlying tech originally came from Nimbella, which was bought by DO. I decided to introduce a new service to my main api using this new platform - kill two birds with one stone; introduce a new feature to the api while also being able to test out how ergonomic the new serverless platform is.

API

The API provides a language detection service i.e. given a piece of text identify what language it is. Currently it supports over 80 languages. The longer the text that is provided the better the prediction. Unfortunately, due to the algorithm used English is poorly supported [ironically]

Server

The server is up now and should be alive at https://api.aawadia.dev/lang-detect/v1/

It takes in a single parameter via query parameters or json request body with key script that should be the text of the language that needs to be detected eg https://api.aawadia.dev/lang-detect/v1?script=чтотыделаешь

The response is in the following format and includes the name of the language and the confidence, which is a float between 0 and 1.

1
2
3
4
5
6
{
"confidence": 1,
"elapsedUs": 290,
"language": "Russian",
"languageCode": 60
}

Serverless

Getting started with DO’s serverless offering was pretty straight forward. A single init command doctl serverless init . -l golang -t <api-token> to get the project started was all that was needed to get the proper directory layout.

By default it will create packages/sample/hello, which needs to match what will be in your project.yaml file i.e. if you want to change the name of the function from sample/hello the directory structure needs to match [package.name/actions.name in the project.yaml define what the directory structure should be]

Go modules for dependencies is fully supported as well, which was really nice to see - Generally FaaS code doesn’t have a tonne of dependencies but they will almost always have a couple.

The signature of the main function, that is executed, is func Main(args map[string]interface{}) map[string]interface{} - all query and body parameters are merged into one and passed in as args

Update the code and deploy using doctl serverless deploy . -t <token> --remote-build and after a couple of seconds the logic should be live at the URL provided by DO.

The next thing was to add the ingress to my nginx api gateway which was as simple as adding the following lines to the main server block and we are live!

1
2
3
4
5
location /lang-detect/v1 {
proxy_ssl_server_name on;
# replace with the proper url
proxy_pass https://faas-tor1.doserverless.co/api/v1/web/;
}

It was generally a painless experience - the doctl cli has been updated to provide the tools required for developing, testing, and deploying functions easily.

If your service is specific enough that it can be written out in a couple of functions [generate a jwt or send a slack notification] then using this platform can greatly simplify your infrastructure and allow you to focus on the main code logic. DO also provides a generous free tier of 25GB-hours per month.

The only thing I couldn’t figure out was how to change the default timeout [3 seconds] and memory limit [256mb].

SLA

Best effort :)