Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/eko/gofast

A simple micro-framework written in Go
https://github.com/eko/gofast

cors go golang micro-framework middleware pongo2

Last synced: 3 months ago
JSON representation

A simple micro-framework written in Go

Awesome Lists containing this project

README

        

Gofast - A light, fast and simple Go micro-framework
====================================================

[![GoDoc](https://godoc.org/github.com/eko/gofast?status.png)](https://godoc.org/github.com/eko/gofast)
[![Build Status](https://secure.travis-ci.org/eko/gofast.png?branch=master)](http://travis-ci.org/eko/gofast)

This is a light and fast micro-framework I wrote in order to train to Go language.

This project uses "pongo2" library for rendering templates (compatible with Django Jinja templates)

Installation
------------

Prior to Go 1.11:

```bash
$ git clone [email protected]:eko/gofast.git
$ go get -u github.com/flosch/pongo2
$ go get -u golang.org/x/net/http2
$ go get -u golang.org/sirupsen/logrus
```

Prefer using `dep`?

```bash
$ dep ensure
```

When using Go 1.11 (go modules) or later:

```bash
$ go build ./...
```

Running an application
----------------------

```bash
$ go run app.go
2015/01/26 21:57:35 gofast v1.0-beta
2015/01/26 21:57:48 [POST] 200 | route: 'add' | url: "/add/toto" (time: 143.238us)
```

This will run the application on port 8080. Optionnaly, you can provide a port number this way:

```bash
$ PORT=8005 go run app.go
```

A simple application example
----------------------------

Because an example will explain it better, here is an application example with things you can do with Gofast:

```go
package main

import (
"github.com/eko/gofast"
)

func main() {
app := gofast.Bootstrap()
app.SetAssetsDirectory("assets")
app.SetViewsDirectory("views")

// This adds a fallback route for 404 (not found) resources
app.SetFallback(func(context gofast.Context) {
context.GetResponse().SetStatusCode(404)
app.Render(context, "404.html")
})

// You can add a simple GET route
app.Get("homepage", "/$", func(context gofast.Context) {
app.Render(context, "index.html")
})

// ... or add a more complex POST route with a URL parameter
app.Post("add", "/add/([a-zA-Z]+)$", func(context gofast.Context) {
request := context.GetRequest()

pattern := context.GetRoute().GetPattern()
url := request.GetHttpRequest().URL.Path

request.AddParameter("name", pattern.FindStringSubmatch(url)[1])

// ... your custom code

app.Render(context, "add.html")
})

app.Listen()
}
```

HTTP/2 Support
--------------

You can use HTTP/2 support by using the following line instead of app.Listen():

```
app.ListenHttp2("./fullchain.pem", "./privkey.pem")
```

Of course, you will have to precize SSL certificate and private key.

You can also set `Link` headers in order to preload assets:

```
response := c.GetResponse()

response.Header().Add("Link", "; rel=preload")
response.Header().Add("Link", "; rel=preload")
```

Templating
----------

You can use all Pongo2 template features and retrieve data like this:

```twig
{% extends "../layout.html" %}

{% block navigation %}
{% include "../include/navigation.html" with current="blog" %}
{% endblock %}

Retrieve a "name" parameter


{{ request.GetParameter("name") }}

Retrieve a "data" POST form value


{{ request.GetFormValue("data") }}


```

You have access to both `request` and `response` objects from context.

Requesting this example
-----------------------

Using the example given below, here is the request results:

```bash
> $ curl -X GET http://127.0.0.1:8080/

Welcome to the index template!

> $ curl -X POST -d'data=my post data' http://127.0.0.1:8080/add/toto

Retrieve a "name" parameter


toto

Retrieve a "data" POST form value


my post data


```

Middlewares
-----------

You can add some middlewares in your application by the following way:

```go
app.Use(func(context gofast.Context, next gofast.MiddlewareFunc) gofast.Handler {
// Some code before calling the next middleware
handler := next(context, next)
// Some code after calling the next middleware

return handler
})
```

It allows you to access `context` (request, response, current route) and also
allows to define a new `handler` function to update the application behavior.

Default CORS headers
--------------------

Following CORS headers are enabled by default when the request has an "Origin" header:

```
Access-Control-Allow-Headers: Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization
Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE
Access-Control-Allow-Origin: domainname.tld
```

You can override any header (including CORS ones) by the following way into each action:

```go
app.Get("retrieve-data", "/retrieve$", func(context gofast.Context) {
response := context.GetResponse()
response.Header().Set("Access-Control-Allow-Methods", "GET")
response.Header().Set("Content-Type", "application/json")

fmt.Fprint(response, "{result: 200}")
})
```

Use the logger
--------------

The Logrus logger instance can be used to write information in your application stdout if you need it. You can retrieve the logger instance from the context as follows:

```go
app.Get("test-logger", "/test-logger$", func(context gofast.Context) {
logger := context.GetLogger()
logger.Info("Roger, this is my log information!")
})
```