Part 1: Get Started

Creating a new project with Flamingo is actually straight forward.

We start by defining a new Go Module, the bare minimum we need to get started.

~$ mkdir mysite

~$ cd mysite

~/mysite$ go mod init mysite

~/mysite$ go get -v flamingo.me/flamingo/v3

First we create a main.go file to kickstart Flamingo. Beside the normal App we will also add the Flamingo requestlogger Module.

main.go

Now we can run our application with

~/mysite$ go run main.go serve

Now open http://localhost:3322/. Flamingo will give us an json error response because we don’t have anything set up yet.

The Poll app

The Django intro creates a small Poll app, so we will do the very same.

First we place a folder polls in our mysite folder. We start by adding our first view. To be consistent with the Django intro we will name the files in the same way.

We use the same names for files as in the Django intro to be consistent.

We create a views.go and define our first index view.

In Flamingo there is no concept of globals, or global state. Therefore we have to bind everything to structs to make sure flamingo can handle them properly. This will add a few more lines in the beginning, but eventually helps a lot to be more flexible.

polls/view.go

To open the view we need a route. In Flamingo we create a RoutesModule which tells Flamingo how to wire URLs and views together.

polls/urls.go

The first lines are boilerplate code. Essentially we create a new urls struct which gets an instance of our controller. This might seem useless for now but we will need it later.

The Inject method tells Dingo, the dependency injection framework, what the urls depends on.

The Routes method implements the web.RoutesModule interface and configures the RouterRegistry.

Flamingo does not have a configuration like Django where we can “mount” the package on different base-paths. We can, however, change the URLs later in a project using a routes.yml file to rewrite routes.

Last but not least we have to make our Project aware of the routes.

Flamingo uses a module-based approach. We create our module.go and register the URLs:

polls/module.go

In our main.go we will load the module by appending it in our modules list:

main.go