gotogether

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

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

Go to latest
Published: May 2, 2013 License: MIT Imports: 12 Imported by: 2

README

`gotogether` - Resource Compiler for Go
=================================

`gotogether`  a directory of resource into a Go source file so you can still
deploy a single executable as a web server with all the CSS, image files, JS ...
included.

This is a fork of tebeka's nrsc: http://bitbucket.org/tebeka/nrsc

Installing
==========
::

    go get github.com/carbocation/gotogether

Also grab the `gotogether` script from here_

And you'll need `zip` somewhere in your path.

.. _here: http://bit.ly/gotogether-script

Invocation
==========
::

    go build
    gotogether <executable> <resource dir> [zip options]


API
===
The `gotogether` package has the following interface

`gotogether.Handle(prefix string)`
    This will register with the `net/http` module to handle all paths starting with prefix. 

    When a request is handled, `prefix` is stripped and then a resource is
    located and served.

    Resource that are not found will cause an HTTP 404 response.
    

`gotogether.Get(path string) Resource`
    Will return a resource interface (or `nil` if not found) (see resource interface below).
    This allows you more control on how to serve.


`LoadTemplates(t *template.Template, filenames ...string) (*template.Template, error)`
    Will load named templates from resources. If the argument "t" is `nil`, it is
    created from the first resource.

Resource Interface
------------------

`func Open() (io.Reader, error)`
    Returns a reader to resource data

`func Size() int64`
    Returns resource size (to be used with `Content-Length` HTTP header).

`func ModTime() time.Time`
    Returns modification time (to be used with `Last-Modified` HTTP header).


Example Code
------------
::

    package main

    import (
            "fmt"
            "net/http"
            "os"

            "github.com/carbocation/gotogether"
    )

    func indexHandler(w http.ResponseWriter, req *http.Request) {
            fmt.Fprintf(w, "Hello World\n")
    }

    func main() {
            gotogether.Handle("/static/")
            http.HandleFunc("/", indexHandler)
            if err := http.ListenAndServe(":8080", nil); err != nil {
                    fmt.Fprintf(os.Stderr, "error: %s\n", err)
                    os.Exit(1)
            }
    }

Contact
=======
https://github.com/carbocation/gotogether
    
License
=======
MIT (see `LICENSE.txt`_)

.. _`LICENSE.txt`: https://github.com/carbocation/gotogether/src/tip/LICENSE.txt

Documentation

Overview

Serving resource from zip file appended to Go executable (this enables on file deploy).

Making it happen:

  1. Add code to serve resources (see example below)
  2. Compile your executable
  3. Run the `gotogether` script from https://github.com/carbocation/gotogether/src
  4. Deploy

Example code:

package main

import (
	"fmt"
	"net/http"
	"os"

	"github.com/carbocation/gotogether"
)

type params struct {
	Number  int
}

func indexHandler(w http.ResponseWriter, req *http.Request) {
	t, err := gotogether.LoadTemplates(nil, "t.html")
	if err != nil {
		http.NotFound(w, req)
	}
	if err = t.Execute(w, params{7}); err != nil {
		http.NotFound(w, req)
	}
}

func main() {
	gotogether.Handle("/static/")
	http.HandleFunc("/", indexHandler)
	if err := http.ListenAndServe(":8080", nil); err != nil {
		fmt.Fprintf(os.Stderr, "error: %s\n", err)
		os.Exit(1)
	}
}

Index

Constants

View Source
const (
	Version = "0.3.2"
)

Variables

View Source
var ResourceMap map[string]Resource = nil

Functions

func Handle

func Handle(prefix string) error

Handle register HTTP handler under prefix

func Initialize

func Initialize() error

func LoadTemplates

func LoadTemplates(t *template.Template, filenames ...string) (*template.Template, error)

LoadTemplates loads named templates from resources. If the argument "t" is nil, it is created from the first resource.

Types

type Resource

type Resource interface {
	Name() string
	Open() (io.ReadCloser, error)
	Size() int64
	ModTime() time.Time
}

func Get

func Get(path string) Resource

Get returns the named resource (nil if not found)

Jump to

Keyboard shortcuts

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