forgery

Home API Reference Guide GitHub

Guide

Getting Started

With golang installed (download), get your first application started by installing forgery.

go get github.com/ricallinson/forgery

Now create the file init.go with the following content.

package main

import("github.com/ricallinson/forgery")

func main() {
    app := f.CreateServer()
    app.Get("/", func(req *f.Request, res *f.Response, next func()) {
        res.Send("Hello world.")
    })
    app.Listen(3000)
}

Start the app.

go run init.go

Now you can view the page in a browser http://localhost:3000/.

Serving Static Files

To serve static files such as CSS, JS and images you can use the f.Static middleware.

Create the file init.go with the following content.

package main

import (
    "github.com/ricallinson/forgery"
)

func main() {
    app := f.CreateServer()
    app.Use(f.Static())
    app.Get("/", func(req *f.Request, res *f.Response, next func()) {
        res.Send("<a href=\"file.txt\">file.txt</a>")
    })
    app.Listen(3000)
}

Now make a new directory named public with the a file named file.txt in it. The content of the file can be anything you like.

Start the app.

go run init.go

Now you can view the page in a browser http://localhost:3000/. When you click on the file.txt link you will be shown the content of the file.

Google App Engine

You must have forgery and the Google App Engine Go Runtime installed.

In a new directory create the file init.go with the following content.

package main

import(
    "github.com/ricallinson/forgery"
    "net/http"
)

func init() {
    app := f.CreateServer()
    app.Get("/", func(req *f.Request, res *f.Response, next func()) {
        res.Send("Hello world.")
    })
    http.Handle("/", app)
}

Now create the Google App Engine configuration file app.yaml with the following content.

application: helloworld
version: 1
runtime: go
api_version: go1

handlers:
- url: /.*
  script: _go_app 

With these 2 files complete start the local Google App Engine.

/path/to/go_appengine/goapp serve .

Now you can view the page in a browser http://localhost:8080/