<commit_before>vitali
======
## Install
Make sure you have configured the go runtime correctly first.

```
$ go get github.com/lunastorm/vitali
```

## Run example
```
$ cd $GOPATH/src/github.com/lunastorm/vitali/example
$ go run main.go
2014/04/11 01:50:22 starting server at port 8080...
```

Open http://foo:bar@127.0.0.1:8080/user/foo/slide in the browser, and you can create a new slide or edit the example slide.

## Basic webapp folder structure
You can place almost everything in the base folder. However, you should create the "views" subfolder which is where you put the template html files, and also the i18n.json dictionary.

## Create your first resource
resources/foo.go
```
package resources
import (
    "github.com/lunastorm/vitali"
)

type Foo struct {
    vitali.Ctx
}

func (c *Foo) Get() interface{} {
    return "hello world"
}
```
Every resource struct should embed the _vitali.Ctx_ struct. Then you implement the GET or other methods for it.

## Serve the webapp
main.go
```
package main

import (
    "log"
    "net/http"
    "github.com/lunastorm/vitali"
    "./resources"
)

func main() {
    http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))
    webapp := vitali.CreateWebApp([]vitali.RouteRule{
        {"/foo", resources.Foo{
        }}, 
    })  
    http.Handle("/", webapp)
    log.Printf("starting server at port 8080...")
    http.ListenAndServe(":8080", nil)
}
```

## Routing
The first member of vitali.RouteRule is the path expression, and the second is the target resource's prototype instance. _{thisPresentsAPathParameter}_ You can access the path parameter through the PathParam method, here is an example:
```
webapp := vitali.CreateWebApp([]vitali.RouteRule{
    {"/image/{filename}", resources.Image{
    }},
})

type Image struct {
    vitali.Ctx
}

func (c *Image) Get() interface{} {
    f, _ := os.Open("images/"+c.PathParam("filename"))
    return f
}
```

## Method Dispatching
Implement the methods that returns anything (type interface{}) which corresponds to the HTTP methods.

Pre() is a special function that runs before the HTTP methods if implemented. You can do some initialization and checking in Pre(). Returning nil in Pre() means to continue to invoke the corresponding HTTP method.
```
type Foo struct {
    vitali.Ctx
}

func (c *Foo) Get() interface{}
func (c *Foo) Post() interface{}
func (c *Foo) Put() interface{}
func (c *Foo) Delete() interface{}
func (c *Foo) Pre() interface{}
```

## Predefined Response Types
Some typical HTTP responses are provided. See https://github.com/lunastorm/vitali/blob/master/response_types.go

For example,
```
func (c *Foo) Pre() interface{} {
    return c.BadRequest("bad!")
}

func (c *Foo) Get() interface{} {
    return c.NotFound()
}
```

For most client errors which leads to a response with HTTP status code 4xx, you may also return the response with a content body.
```
type ErrorMessageModel struct {
    Msg string `json:"msg"`
}

func (c *Foo) Get() interface{} {
    return c.NotFound(&ErrorMessageModel{Msg: "You've entered the wrong URL."})
}
```

## Authentication
You can provide your customized user and role provider when you implement vitali.UserProvider interface, and then setup the user provider as follows:
```
type UserProvider interface {
    AuthHeader(*http.Request) string
    GetUserAndRole(*http.Request) (string, string)
}

webapp.UserProvider = &YourUserProvider{...}
```

### Authentication Example: HTTP Basic Authentication
This example just verifies that the  username is _foo_ and the password is _<PASSWORD>_. And the role _AUTHED_ will be added when the user is authenticated.
```
import (
    "strings"
    "net/http"
    "encoding/base64"
)

type UserProvider struct {
}

func (c *UserProvider) AuthHeader(r *http.Request) (WWWAuthenticate string) {
    return `Basic realm="vitali"`
}<commit_msg>Fix README example<commit_after>vitali
======
## Install
Make sure you have configured the go runtime correctly first.

```
$ go get github.com/lunastorm/vitali
```

## Run example
```
$ cd $GOPATH/src/github.com/lunastorm/vitali/example
$ go run main.go
2014/04/11 01:50:22 starting server at port 8080...
```

Open http://foo:bar@127.0.0.1:8080/user/foo/slide in the browser, and you can create a new slide or edit the example slide.

## Basic webapp folder structure
You can place almost everything in the base folder. However, you should create the "views" subfolder which is where you put the template html files, and also the i18n.json dictionary.

## Create your first resource
resources/foo.go
```
package resources
import (
    "github.com/lunastorm/vitali"
)

type Foo struct {
    vitali.Ctx
}

func (c *Foo) Get() interface{} {
    return "hello world"
}
```
Every resource struct should embed the _vitali.Ctx_ struct. Then you implement the GET or other methods for it.

## Serve the webapp
main.go
```
package main

import (
    "log"
    "net/http"
    "github.com/lunastorm/vitali"
    "./resources"
)

func main() {
    http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))
    webapp := vitali.CreateWebApp([]vitali.RouteRule{
        {"/foo", resources.Foo{
        }}, 
    })  
    http.Handle("/", webapp)
    log.Printf("starting server at port 8080...")
    http.ListenAndServe(":8080", nil)
}
```

## Routing
The first member of vitali.RouteRule is the path expression, and the second is the target resource's prototype instance. _{thisPresentsAPathParameter}_ You can access the path parameter through the PathParam method, here is an example:
```
webapp := vitali.CreateWebApp([]vitali.RouteRule{
    {"/image/{filename}", resources.Image{
    }},
})

type Image struct {
    vitali.Ctx
}

func (c *Image) Get() interface{} {
    f, _ := os.Open("images/"+c.PathParam("filename"))
    return f
}
```

## Method Dispatching
Implement the methods that returns anything (type interface{}) which corresponds to the HTTP methods.

Pre() is a special function that runs before the HTTP methods if implemented. You can do some initialization and checking in Pre(). Returning nil in Pre() means to continue to invoke the corresponding HTTP method.
```
type Foo struct {
    vitali.Ctx
}

func (c *Foo) Get() interface{}
func (c *Foo) Post() interface{}
func (c *Foo) Put() interface{}
func (c *Foo) Delete() interface{}
func (c *Foo) Pre() interface{}
```

## Predefined Response Types
Some typical HTTP responses are provided. See https://github.com/lunastorm/vitali/blob/master/response_types.go

For example,
```
func (c *Foo) Pre() interface{} {
    return c.BadRequest("bad!")
}

func (c *Foo) Get() interface{} {
    return c.NotFound()
}
```

For most client errors which leads to a response with HTTP status code 4xx, you may also return the response with a content body.
```
type ErrorMessageModel struct {
    Msg string `json:"msg"`
}

func (c *Foo) Get() interface{} {
    return c.NotFound(ErrorMessageModel{Msg: "You've entered the wrong URL."})
}
```

## Authentication
You can provide your customized user and role provider when you implement vitali.UserProvider interface, and then setup the user provider as follows:
```
type UserProvider interface {
    AuthHeader(*http.Request) string
    GetUserAndRole(*http.Request) (string, string)
}

webapp.UserProvider = &YourUserProvider{...}
```

### Authentication Example: HTTP Basic Authentication
This example just verifies that the  username is _foo_ and the password is _<PASSWORD>_. And the role _AUTHED_ will be added when the user is authenticated.
```
import (
    "strings"
    "net/http"
    "encoding/base64"
)

type UserProvider struct {
}

func (c *UserProvider) AuthHeader(r *http.Request) (WWWAuthenticate string) {
    return `Basic realm="vitali"`
}
{'fraction_non_alphanumeric': 0.12335991767429895, 'fraction_numerical': 0.010547980447646, 'mean_word_length': 4.025856496444732, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5273549', 'n_tokens_mistral': 2460, 'n_tokens_neox': 2342, 'n_words': 860}