Having Fun with Clojurescript on Google Cloud Function

Zero One Group
Zero One Group
Published in
4 min readJul 16, 2020

By Doni Rubiagatra, Senior Engineer at Zero One Technology

Clojure and ClojureScript

Here at Zero One Technology (ZOT), we are extensively using Clojure and ClojureScript. We typically use the Luminus template for the backend and Re-frame for the frontend in our projects. Although we are currently happy with the current development and deployment situation in the Clojure ecosystem, we would like to explore a number of possibilities to maximise Clojure’s potential in a cloud environment. As a relatively young programming language, support for Clojure or sample resources for the current cloud features are few and far between. These challenges often hide Clojure’s true potential in cloud environments.

Serverless Function

What does it mean to use a serverless function? To put it simply, we write software in the form of a function that has a single purpose. Afterwards, a cloud computing service will host and maintain it.

The main advantages of using serverless functions are: efficiency in cost, speed in development, and not having to worry about provisioning the server.

Moreover, serverless functions are typically priced with a pay-as-you-go scheme, which means that you could create the MVP of your backend application practically for free!

Introducing the Google Cloud Function

Released for production use in 2018, the Google Cloud Function is a scalable pay-as-you-go Functions as a Service (FaaS) to run your code with zero server management. Being one of Google Cloud Platform’s products, you can expect extremely reliable infrastructure managed by Google. The current supported runtimes on Google Cloud Function are NodeJS 10, Python, Go and Java.

Google Cloud Functions | Google

Why Clojure and What is Clojurescript?

Here in Zero One Technology, the reasons for using Clojure are very much in line with Rich Hickey‘s rationales:

  • A functional programming language.
  • A rich set of immutable, persistent data structures.
  • A dialect of lisp, which empowers code-as-data philosophy.
  • Seamless interoperability with Java’s rich ecosystem.

“ClojureScript is a compiler for Clojure that targets JavaScript. It emits JavaScript code which is compatible with the advanced compilation mode of the Google Closure optimizing compiler.”

https://clojurescript.org/

Essentially, you get the fun and joy of coding in Clojure, but your code will compile seamlessly to JavaScript.

Getting Started

First, make sure that you have your NodeJS installed on your system. For this article, I am using NodeJS version 12 and NPM version 6.

➜ node -v
v12.18.1
➜ npm -v
6.14.5

Next, we create the cljs-google-cloud project using shadow-cljs. It provides you with everything you need to compile your ClojureScript code with a focus on simplicity and ease of use. We will discuss what shadow-cljs is in a future Zero One Group blog post — stay tuned!

➜ npx create-cljs-project cljs-google-cloud-function

The command above will create a project with these folders and files. The file shadow-clsj.edn will be used to configure your ClojureScript builds and dependencies. The file package.json is used by NPM to manage JavaScript dependencies. Finally, the directory src is where your code is located.

➜ ls  
node_modules package.json package-lock.json shadow-cljs.edn src

Using Firebase Function as Development Environment

Since joining Google back in 2014, Firebase has developed a product called ‘Cloud Function for Firebase’. It is a joint product shared between the Google Cloud Platform and Firebase, which provides a framework that lets you automatically run backend code in response to triggered events. By using Firebase functions, it becomes straightforward to set up a development environment for Google Cloud Function.

You can start by installing these dependencies onto your project and add an empty firebase.json. Next, you can create a project in Firebase and login via the CLI. This is only to help the development of the project, and not for its deployment on Firebase.

➜ npm i firebase-tools — save-dev
➜ npm i firebase-admin firebase-functions
➜ echo {} > firebase.json
➜ npx firebase login
➜ npx firebase use <your-firebase-project>

The starter code, the build and running your function locally

Create the file src/main/cloud_function.cljs and update the shadow-cljs.edn with the following code. We required “firebase-functions” as “fun” and using fun/https to create an HTTP-triggered function.

src/main/cloud_function.cljs

We call this build “cloud-function”, and it will be compiled to functions/index.js.

shadow-cljs.edn

Next, compile the code and add package.json to functions/package.json.

➜ npx shadow-cljs compile cloud-function
➜ cp package.json functions/package.json

Run your development server, and it will run locally at localhost:5000.

➜ npx firebase serve

Release the function and Deploy to Google Cloud Function.

Before we can deploy it, you can compile the source code for performance optimisation.

➜ npx shadow-cljs release cloud-function

Install the Google Cloud SDK and configure your GCP project on your machine. After that, cd into the functions directory and you should be able to directly deploy to Google Cloud Function.

➜ cd functions
➜ gcloud functions deploy home — region asia-east2 — runtime nodejs10 — trigger-http

Finally, we deployed the function and you can check it by the given URL.

Final Thought

As we are using Clojure as our main programming language, being able to use it on the Google Cloud Function opens numerous exciting possibilities for our future projects. We have found that Google Cloud Function is easy to use, coupled with really good documentation. You can check the starter in this open-sourced repository. Finally, check out Zero One Group’s other open-source projects!

Follow Zero One Group at Instagram, Twitter, Facebook, and LinkedIn. Visit our website at www.zero-one-group.com

--

--

Zero One Group
Zero One Group

A leading integrated technology services company, providing personalised solutions to even the most challenging business problems. www.zero-one-group.com