Go framework PASTIS

Simple Flexible Extensible

RESTful API in a minute

  
						   
package main

import (
        "fmt"
        "net/http"
        "net/url"
        "github.com/guregodevo/pastis"
)

type ChartResource struct {
}

type Chart struct {
    Name  string
    Order int
}

func (res ChartResource) Get(params url.Values) (int, interface{}){
    id := params.Get("chartid")
    fmt.Fprintf(w, "Chart id %s!", id)
    return 200, map[string]interface{} {"id":id, "size":3, "type":"line"}
}

func (res ChartResource) Post(c Chart) (int, interface{}){
    fmt.Fprintf(w, "Posted Chart %s!", c.Name)
    return 201, nil
}

func main() {
    chartResource := new(ChartResource)
    api := NewAPI()
    api.AddResource("/dashboards/:dashboardid/chart/:chartid", chartResource)
    pastis.Start(2333)
}
							
						

Flexible URL-pattern matching

  
						   
package main

import (
        "fmt"
        "net/http"
        "net/url"
        "github.com/guregodevo/pastis"
)

func comment(params url.Values, body string) (int, interface{}){
              id := params.Get("id")
              comment := params.Get("comment")
              fmt.Fprintf(w, "Posted Comment %s : %s!", id, body)
              return 201, nil
}


func main() {
    api := NewAPI()
    api.Post("^/comment/(?P<id>\\d+)$", comment) 
    pastis.Start(2333)
}						   	
							
						

Custom filter

  
						   
// Filter (post-process) Filter (as a struct that defines a FilterFunction)
func LoggingFilter(w http.ResponseWriter, request *http.Request, chain *FilterChain) {
    now := time.Now()
    chain.NextFilter(w, request)
    log.Printf("[HTTP] %s %s [%v]\n", request.Method, request.URL, time.Now().Sub(now))
}

func main() {
    api := NewAPI()
    api.Post("^/comment/(?P<id>\\d+)$", comment) 
    api.AddFilter(LoggingFilter)
    pastis.Start(2333)
}						   	
						   
						

Custom log

  
						   

func main() {
    api := pastis.NewAPI()
    api.SetLevel("INFO")
    api.SetOutputs(os.StdErr, log.Ldate|log.Ltime|log.Lshortfile, "ERROR")
    api.SetOutputs(os.StdOut, log.Ltime, "INFO", "DEBUG", "WARN") 
    api.Post("^/comment/(?P<id>\\d+)$", comment) 
    pastis.Start(2333)
}