webapp

package module
v0.0.0-...-70fceca Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2015 License: MIT Imports: 3 Imported by: 0

README

WebApp

WebApp adds a layer on top of goji project to make it faster to get up and running.

Usage example

package main

import (
    "fmt"
    "net/http"
    "time"

    "github.com/jinzhu/gorm"
    "github.com/johnwilson/webapp"
    "github.com/johnwilson/webapp/plugins"
    "github.com/johnwilson/webapp/sys"
    _ "github.com/mattn/go-sqlite3"
    "github.com/zenazn/goji"
    "github.com/zenazn/goji/graceful"
    "github.com/zenazn/goji/web"
)

type Page struct {
    sys.Controller
}

func (pg *Page) SendMail(p sys.JobParams) interface{} {
    // get values
    from := p["from"].(string)
    to := p["to"].(string)
    msg := fmt.Sprintf("mail sent from: %s to: %s", from, to)

    // simulate send mail
    time.Sleep(5 * time.Second)

    return map[string]string{"status": msg}
}

func (pg *Page) hello(c web.C, w http.ResponseWriter, r *http.Request) {
    orm := pg.GetPlugin("plugin.orm", c).(*gorm.DB)
    res := orm.Raw("SELECT sqlite_version();")
    var version string
    res.Row().Scan(&version)
    pg.Render(c).Text(w, http.StatusOK, "sqlite: "+version)
}

func (pg *Page) mailer(c web.C, w http.ResponseWriter, r *http.Request) {
    j := sys.NewAsyncJob(make(chan interface{}))
    j.Set("from", c.URLParams["from"])
    j.Set("to", c.URLParams["to"])

    pg.AddJob("mailer", j)

    reply := <-j.Result

    pg.Render(c).JSON(w, 200, reply)
}

func main() {
    webapp.Application.Init("config.toml")

    // serve static files
    goji.Use(webapp.Application.ApplyStatic)

    // plugins
    webapp.Application.RegisterPlugin("orm", new(plugins.Gorm))

    // controller
    pg := new(Page)
    pg.NewJobQueue("mailer", pg.SendMail, 2)
    goji.Get("/", pg.hello)
    goji.Get("/mail/:from/:to", pg.mailer)

    graceful.PostHook(func() {
        webapp.Application.Close()
    })
    goji.Serve()
}
Code source and libraries

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Application = sys.NewApp()
)

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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