Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/eko/gofast
- Owner: eko
- License: mit
- Created: 2014-11-22T11:28:40.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2023-03-07T03:47:34.000Z (almost 2 years ago)
- Last Synced: 2024-06-20T11:56:50.827Z (7 months ago)
- Topics: cors, go, golang, micro-framework, middleware, pongo2
- Language: Go
- Size: 65.4 KB
- Stars: 40
- Watchers: 6
- Forks: 6
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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 mainimport (
"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.Pathrequest.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 middlewarereturn 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!")
})
```