
Already have a Go Fly App? Try adding Tigris for file storage. Tigris is scalable, secure, global object storage and you can integrate it quickly with your Go app.
The example application
You can get the code for the example from the GitHub repository. Justgit clone https://github.com/fly-apps/go-example to get a local copy.
The go-example application is, as you’d expect for an example, small. It’s a Go application that uses the HTTP server and templates from the standard library. Here’s all the code from main.go:
main function starts a server that responds with an HTML page showing the fly region that served the request. The template lives in ./templates/ and is embedded into the binary using the embed package in go 1.16+.
The template itself, index.html.tmpl, is very simple too:
Building the application
As with most Go applications, a simplego build will create a hellofly binary which we can run. It’ll default to using port 8080 and you can view it on localhost:8080 with your browser. So, the raw application works. Now to package it up for Fly.
Install flyctl and log in
We are ready to start working with Fly and that means we needflyctl, our CLI app for managing apps on Fly. If you’ve already installed it, carry on. If not, hop over to our installation guide. Once that’s installed you’ll want to log in to Fly.
Launch the app on Fly.io
To launch an app on fly, runflyctl launch in the directory with your source code. This will create and configure a fly app for you by inspecting your source code, then prompt you to deploy.
flyctl has set.
Choosing yes will bring you to a page in your browser where you can change any configuration, such as the region to deploy into or the size of the VM created.
Once that is complete, flyctl will create a fly.toml file containing the configuration selected. It might then begin a deployment, or let you know you should start a deployment yourself via flyctl deploy.
Inside fly.toml
The fly.toml file now contains a default configuration for deploying your app. In the process of creating that file, flyctl has also created a Fly-side application slot of the same name, “hellofly”. If we look at the fly.toml file we can see the name in there:
flyctl command will always refer to this file in the current directory if it exists, specifically for the app name value at the start. That name will be used to identify the application to the Fly service. The rest of the file contains settings to be applied to the application when it deploys.
You can see in the [build] section the builder image and the version of Go that was detected in your go.mod file. You can change or add build arguments to configure the build process using the [build.args] section.
Inside Dockerfile
We use a Debian base image which has CGO enabled by default. A minimal alternative without CGO is Alpine:
CGO enabled, read here.
Deploying to Fly.io
To deploy your app, just run:fly.toml file, and get the app name hellofly from there. Then flyctl will start the process of deploying our application to the Fly platform. Flyctl will return you to the command line when it’s done.
Viewing the deployed app
Now the application has been deployed, let’s find out more about its deployment. The commandflyctl status will give you all the essential details.
Connecting to the app
The quickest way to browse your newly deployed application is with theflyctl apps open command.
Add Tigris Global Storage (optional)
Tigris provides CDN-like accessibility for your stored objects. Tigris has S3-API compatibility, automatically manages data replication and caching for you, and offers AuthN and AuthZ authentication mechanisms. Learn more about Tigris Object Storage.-
Create a Tigris bucket: run the create command inside your Go Fly App’s directory. Afterwards, make sure to take note of the credentials received:
-
Retrieve AWS SDK modules: Tigris has an S3 compatible API service, thus, we can easily use AWS SDK Go modules to connect with the Tigris Bucket we’ve just created.
-
Create a Client function that will return an instance ready to connect with a Tigris bucket:
The
LoadDefaultConfig()function will create an aws.Config instance that can load credentials from environment variables. Passing this instance to theNewFromConfig()function creates an S3 service client we’ve configured to connect with the Tigris endpoint. -
Set up Tigris Credentials.
To successfully connect with our Tigris Bucket, we can set our Tigris credentials as AWS SDK environment variables.
In a local setup, this should look something like:
For our Go Fly App, these environment variables should have already been set as secrets during the running of
fly storage create:Of course, this would only automatically happen if the create command was run in a directory containing afly.tomlfor our Go Fly app. But if that wasn’t the case, we can still manually set necessary secrets like so: -
Upload a file: Aside from connecting with a Bucket, we can use other AWS SDK functions. Let’s use the
PutObjectfunction to upload a text file on the go like so:
Bonus points
If you want to know what IP addresses the app is using, tryflyctl ips list: