How to Deploy Gleam on Codesphere?

Gleam is a newly released language, claimed to be optimized for concurrency and refactoring. It is compact and easy-to-learn (as per the creators you can learn it in an afternoon). This Elm and Rust-inspired language has a compiler that serves as a programming assistant. To sum up some of the features of Gleam, we can say its small surface area gives it the simplicity of Go while also enabling you to leverage Erlang and Elixir tools. In addition, it is type-safe, and functional, as well as highly scalable.

Gleam can run on JS runtimes, which means you can run it anywhere including (but not limited to) mobile devices and browsers. Let’s create a Gleam project for a simple backend web application. Once the project is set up, we'll proceed to deploy it on Codesphere.       

Creating a Gleam Project

We are going to work on a simple Gleam backend web application and deploy it on Codesphere. Let’s start by creating a gleam project.  

gleam new my_web_app

Replace “my_web_app” with the name you want to give to your project. It will create a Gleam project, you can then run this command to go to your project directory.

cd my_web_app

Now open src/my_web_app.gleam file and write this code.

mport gleam/io
import mist
import gleam/erlang/process
import gleam/bytes_builder
import gleam/http/response.{Response}

pub fn main() {
  let assert Ok(_) =
    web_service
    |> mist.new
    |> mist.port(3000)
    |> mist.start_http
  process.sleep_forever()
}

fn web_service(_request) {
  let body = bytes_builder.from_string("Hello, Gleam!")
  Response(200, [], mist.Bytes(body))
}

Running Gleam on Codesphere

It is pretty straightforward to run gleam on Codesphere. All you have to do is create a ci.yml file in your project directory and replace the contents with this.

prepare:
  steps:
    - command: "nix-env -iA nixpkgs.gleam"
    - command: "nix-env -iA nixpkgs.erlang"
    - command: "nix-env -iA nixpkgs.rebar3"
    - command: gleam add mist gleam_http gleam_erlang
test:
  steps: []
run:
  steps:
    - command: gleam run

This will install Gleam and all the required dependencies like Erlang, Rebar, and Mist to run this web application. Go to CI pipeline, hit prepare and run stages and your Gleam web application will be up and running in no time. 

You can access the code here from the GitHub repository.

I hope you enjoyed exploring Gleam as much as I did!