Getting started

Requirements

Installation

Install the Kocha framework:

go get -u github.com/naoina/kocha

And command line tool:

go get -u github.com/naoina/kocha/cmd/...

NOTE: If you want to use a specific Go version and/or specific GOPATH, please use a Go version manager such as gvm.

Create a new application

To create a new Kocha application, run the following command:

kocha new myapp

Where "myapp" is the application name.
Above command will create a skeleton files of app to $GOPATH/myapp.

Run the application

Change directory:

cd $GOPATH/myapp

And run the application:

kocha run

Then, open http://127.0.0.1:9100/ in your Browser.
Do you see the welcome page?

fig1

Congratulation!
You've finished the first step of the development of the Kocha app.

Write a request handler

To create a new controller, run the following command:

kocha g controller myroom

Where "myroom" is the controller name.

NOTE: The following command is same as above because g is aliased to generate subcommand:

kocha generate controller myroom

Also generate subcommand adds route into config/routes.go automatically.

Edit a controller

So let's edit as follows.

In app/controller/myroom.go, edit to:

package controller

import (
    "github.com/naoina/kocha"
)

type Myroom struct {
    *kocha.DefaultController
}

func (c *Myroom) Get(c *kocha.Context) error {
    return c.Render(map[string]interface{}{
        "name": "Alice",
    })
}

In app/view/myroom.html.tmpl, edit to:

<h1>This is {{.name}}'s room</h1>

kocha run watch the files and reload when changed.

Please open http://127.0.0.1:9100/myroom in your Browser.

fig2

You should see the changes that you have made.

Kocha uses Go's html/template. See Template for more information.

Routing parameter

Routing of Kocha can get the parameter from the requested URL path.
So let's do it.

First, In config/routes.go, edit:

Path:       "/myroom",

to

Path:       "/myroom/:name",

Second, In app/controller/myroom.go, edit:

func (c *Myroom) Get(c *kocha.Context) error {
    return c.Render(map[string]interface{}{
        "name": "Alice",
    })
}

to

func (c *Myroom) Get(c *kocha.Context) error {
    return c.Render(map[string]interface{}{
        "name": c.Params.Get("name"),
    })
}

Finally, Let's see http://127.0.0.1:9100/myroom/bob in your Browser as usual.

fig3

Kocha's routing is more powerful. See Routing for more information.

Ending

These are a part of Kocha.
Are you more interested in Kocha? OK, see the Docs!