Question

Run all endpoitns of web server serverless

Firebase has a great feature. You can build a generic express.js API and connect it to the http handler with a custom name.

It looks along these lines:

const secure = express()
secure.use(cors)
secure.use(cookieParser)
secure.use(validateFirebaseIdToken(admin))

secure.post('/download', download(admin, database))
secure.post('/addPurchase_v2', addPurchase(admin, database))

exports.pApi = functions.https.onRequest(secure)

This feature lets you run multiple apis serverless while still having them as regular express applications. If need by, you can run those express applications locally and test or run them on your own server hosting API with a server.

How can I do the same with Digital Ocean Functions? I would like to build a similar API using Go Fiber or Go Chi frameworks, run and test them as regular APIs and have them deployed serverless on Digital Ocean Functions.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Matt Welke
DigitalOcean Employee
DigitalOcean Employee badge
April 25, 2023

If I understand your question correctly, you want to serve requests to multiple endpoints using a single function, where the function’s URL becomes the base of the URLs of the multiple endpoints you want to serve.

For example, you’d deploy your function, and it would get a URL like https://<host>.doserverless.co/api/v1/web/<namespace>/default/my-function. And then you’d serve requests to at least these two URLs:

  • https://<host>.doserverless.co/api/v1/web/<namespace>/default/my-function/download
  • https://<host>.doserverless.co/api/v1/web/<namespace>/default/my-function/addPurchase_v2

And you’re saying that right now, Firebase supports deploying code this way.

Right now, Functions doesn’t officially support code deployed this way, using routers to map handlers to different endpoints. Functions is designed to be used where you have a function for every endpoint. So in this case, you’d deploy at least two functions, perhaps naming them “download” and “addPurchase_v2”. We have techniques you can use to share code between them, like the lib directory (if you store the shared code in the same repo as the functions) and Go modules (if you store the shared code in its own place on GitHub or any other public Git repository).

Because we don’t officially support code deployed this way, we instead have our own function signature defined that isn’t based on http.HandlerFunc. Instead, it’s based on data coming in and data going out.

While we don’t officially support code deployed this way, we do officially support your function knowing the full URL that caused it to be invoked. Using this data, you can do your own manual routing. See our docs on the event parameter (the path data) for more info about this.

Additionally, for some programming languages there are community-supported libraries that do this mapping for you. For example, right now, this exists for Node.js. This allows you to code in a multi-endpoint router style. Right now, to my knowledge, no such community project exists for Go.

Be sure to let us know if you have any other questions about using Functions.

Basically Google Cloud Functions accept basic http.Handler function as entry point: https://cloud.google.com/functions/docs/writing/write-http-functions#http-example-go

That means chi Router can be used to handle all the routing of the API within the same serverless function. How can I do that in Digital Ocean?

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up