goldie

package module
v0.0.0-...-ec3aa90 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2015 License: MIT Imports: 13 Imported by: 0

README

Goldie

A just right web framework for Go

Inspired by Nancy and the SDHP. Uses Gorilla Mux behind the scenes.

Warning

Extreme alpha stuff. Do not use.

Install

go get github.com/jskrepnek/goldie

Serve

import (
  "github.com/jskrepnek/goldie"
)

func main() {
  
  goldie.Get["/hello/{name}"] = func (string name) string {
    return "Hello " + name
  }
  
  goldie.Serve()
}

Supports

  • Model binding
  • Go Http templates

Modules

Use modules to group related methods together and tie into the dependency injection system.

package main

import (
	"github.com/jskrepnek/goldie"
	"net/http"
)

type TestModule struct {
}

func (this *TestModule) Construct(m *goldie.Module) {

	// the base route for all actions
	m.Path = "/test"

	// invoked before every module action
	m.Before.Add(func(req *http.Request) goldie.Response {
		log.Println("Before")
		return goldie.Response{}
	})

	// invoked after every module action
	m.After.Add(func(r *goldie.Response) {
		log.Println("After")
	})

	// registered with the path /test/spot
	m.Get["/spot"] = func() string {
		return "spot"
	}
}

func init() {
	goldie.AddModule(&TestModule{})
}

Model Binding

Goldie will try to bind parameters from the route, query string, and request body. Since Go does not retain function argument names during compliation, we can only reliably bind to a single value type.

package main

import (
	"github.com/jskrepnek/goldie"
)

type Widget struct {
	Id int
	Type string
	Strength int
}

func init() {

	// bind to an integer
	// GET /widget/123
	goldie.Get["/widget/{id}"] = func(id int) Widget {
		return repo.Get(id)
	}

	// bind from the query string to a string
	// GET /widget?type=upright 
	goldie.Get["/widget"] = func(type string) []Widget {
		return repo.GetByType(type)
	}

	// bind to a struct from the request body
	// POST /widget
	// { "id":123, "type":"downright", "strength":44}
	//
	goldie.Post["/widget"] = func(widget Widget) Widget {
		return repo.Add(widget)
	}

	// bind from a route variable and the request to a struct
	// PUT /widget/123
	// { "type":"downright", "strength":44}
	goldie.Put["/widget/{id}"] = func(widget Widget) Widget {
		return repo.Update(widget)
	}

	// bind from a route variable to a value type and the request body to a struct
	// PUT /widget/123
	// { "type":"downright", "strength":44}
	goldie.Put["/widget/{id}"] = func(id int, widget Widget) Widget {
		return repo.Update(id, widget)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Templates map[string]string = map[string]string{}

Functions

func AddModule

func AddModule(mc ModuleConstructor)

func Serve

func Serve()

Types

type Action

type Action interface{}

type AfterInterceptor

type AfterInterceptor func(*Response)

type AfterInterceptors

type AfterInterceptors []AfterInterceptor

func (*AfterInterceptors) Add

type BeforeInterceptor

type BeforeInterceptor func(req *http.Request) Response

type BeforeInterceptors

type BeforeInterceptors []BeforeInterceptor

func (*BeforeInterceptors) Add

type Module

type Module struct {
	Path   string
	Before BeforeInterceptors
	After  AfterInterceptors
	Get    Routes
	Post   Routes
	Put    Routes
	Delete Routes
}

type ModuleConstructor

type ModuleConstructor interface {
	Construct(*Module)
}

type Response

type Response struct{}

type Route

type Route interface{}

type Routes

type Routes map[Route]Action
var (
	Get    Routes = Routes{}
	Post   Routes = Routes{}
	Put    Routes = Routes{}
	Delete Routes = Routes{}
)

type View

type View struct {
	Name  string
	Model interface{}
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL