glaze

package module
v0.0.0-...-3d608bf Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2016 License: MIT Imports: 19 Imported by: 3

README

glaze

A mini web framework in Go. Glaze currently only provides a simple controller/view abstraction to make it easier to get a simple web app up and running.

This example assumes that you have a view directory laid out like so:

/path/to/views/
	layouts/
		default.html
	public/
		index.html

Glaze will always try to load any layout files from "layouts/*.html" under the given view root. The layout should be inside a {{define "layout"}}...{{end}} block.

controllers/public.go:

package controllers

import (
	"net/http"

	"github.com/octoberxp/glaze"
)

type Public struct {
	*glaze.Controller
}

func NewPublicController() *Public {
	controller, err := glaze.NewController("/path/to/views/", "public")
	if err != nil {
		panic(err)
	}

	return &Public{Controller: controller}
}

func (controller *Public) Index(w http.ResponseWriter, r *http.Request) {
	controller.RenderTemplate(w, "index", nil)
}

main.go:

package main

import (
	"net/http"
	
	"exampleapp/controllers"
)

func main() {
	// Instantiate controllers
	public := controllers.NewPublicController()

	// Configure routes
	http.HandleFunc("/", public.Index)

	// And start the server
	http.ListenAndServe(":9000", nil)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateRoutes

func GenerateRoutes(controller Controller) map[string]func(w http.ResponseWriter, r *http.Request)

GenerateRoutes calls GenerateRoutesWithPrefix, using the spinal-cased controller class name as the prefix.

func GenerateRoutesWithPrefix

func GenerateRoutesWithPrefix(controller Controller, prefix string) map[string]func(w http.ResponseWriter, r *http.Request)

GenerateRoutesWithPrefix generates a map of urls to controller methods, suitable for passing off to http.Handle. It will create one route per public method of controller that is not a method of the embedded GlazeController. Each route will be in the form "/[prefix]/[method-name]", where prefix is preserved exactly, and method names are converted to spinal-case.

func OpenLog

func OpenLog(logPath string) (*os.File, error)

OpenLog will open a log file for appending, creating it if necessary

func SafeHTML

func SafeHTML(text string) template.HTML

SafeHTML will return the given string as a template.HTML object, which will cause the template package to skip escaping.

Types

type Application

type Application struct {
	// contains filtered or unexported fields
}

Application encapsulated all the startup logic for a web app using glaze.

func (Application) CreateLog

func (app Application) CreateLog(name string) (*os.File, error)

CreateLog either opens or creates and opens a log file at the given path, and returns an append handle.

func (*Application) Initialize

func (app *Application) Initialize(processName string, port int, verbose, graceful bool) error

Initialize handles all the pre-launch initialization tasks.

func (Application) Restart

func (app Application) Restart(w http.ResponseWriter, req *http.Request)

Restart performs a graceful restart of the process

func (Application) ServeWithAutoRouting

func (app Application) ServeWithAutoRouting(controllers []Controller) error

ServeWithAutoRouting computes standard paths to every Controller that has been added to the Application, and then starts the application server.

func (Application) ServeWithHandler

func (app Application) ServeWithHandler(handler http.Handler) error

ServeWithHandler starts the server with a client-provided handler. If you want to use glaze's auto-routing, use ServeWithAutoRouting instead.

type BaseController

type BaseController struct {
	BaseTemplate *template.Template
	Templates    TemplateMap
	// contains filtered or unexported fields
}

BaseController structs hold the data necessary to bind view paths. A Controller can have any number of http handler methods added to it, and calling RenderTemplate from within a controller method will execute the given template.

func NewController

func NewController(templatePath, controllerTemplatePath string, funcMap template.FuncMap) (*BaseController, error)

NewController will create a basic controller. templatePath is the full path to the template root controllerTemplatePath is the relative path from the template root to

this controller's template directory

func (*BaseController) RenderHTML

func (controller *BaseController) RenderHTML(writer http.ResponseWriter, templateName string, data interface{}) error

RenderHTML will execute the named template using the given writer

func (*BaseController) RenderJSON

func (controller *BaseController) RenderJSON(writer http.ResponseWriter, data interface{}) error

RenderJSON will output the contents of data as JSON, setting an appropriate Content-Type header

type Controller

type Controller interface {
	RenderHTML(writer http.ResponseWriter, templateName string, data interface{}) error
	RenderJSON(writer http.ResponseWriter, data interface{}) error
}

Controller is a convenience interface that allows classes with an embedded BaseController to be passed to various glaze methods.

type TemplateMap

type TemplateMap map[string]*template.Template

TemplateMap maps string names to parsed html templates

Jump to

Keyboard shortcuts

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