<commit_before>Humble/Router
=============

[![GoDoc](https://godoc.org/github.com/go-humble/router?status.svg)](https://godoc.org/github.com/go-humble/router)

Version X.X.X (develop)

A router for client-side web applications written in pure go which compiles to
javascript via [gopherjs](https://github.com/gopherjs/gopherjs). Router works great as a
stand-alone package or in combintation with other packages in the
[Humble Framework](https://github.com/go-humble/humble).

Router supports the following features:

- Write code in pure go. It feels like go, follows go idioms, and compiles with the go tools.
- Each route consists of a path and a handler function which is triggered when path matches.
- Routes can have parameters, which are passed through as an argument to handler functions.
- Router uses history.pushState and listens to the onpopstate event in browsers that support
  it. In older browsers it automatically falls back to using a hash.
- Router can be configured to automatically intercept link click events, triggering the appropriate
  route instead of requesting a new page.


Browser Support
---------------

Router works with IE9+ (with a
[polyfill for typed arrays](https://github.com/inexorabletash/polyfill/blob/master/typedarray.js))
and all other modern browsers. Router compiles to javascript via [gopherjs](https://github.com/gopherjs/gopherjs)
and this is a gopherjs limitation.

Router is regularly tested with the latest versions of Firefox, Chrome, and Safari on Mac OS.
Each major or minor release is tested with IE9+ and the latest versions of Firefox and Chrome
on Windows.


Installation
------------

Install router like you would any other go package:

```bash
go get github.com/go-humble/router`
```

You will also need to install gopherjs if you don't already have it. The latest version is
recommended. Install gopherjs with:

```
go get -u github.com/gopherjs/gopherjs
```


Quickstart Guide
----------------

### Declaring Routes

Declaring routes works similaroy to other routing packages in go. Here's an example:

```go
// Create a new Router object
r := router.New()
// Use HandleFunc to add routes.
r.HandleFunc("/people", indexPeople)
r.HandleFunc("/people/{id}", showPerson)
// You must call Start in order to start listening for changes
// in the url and trigger the appropriate handler function.
r.Start()
```

The second argument to HandleFunc should be a router.Handler, which has the following
definition:

```go
type Handler func(context *Context)
```

And the Context type is defined as follows:

```go
type Context struct {
	// Params is the parameters from the url as a map of names to values.
	Params map[string]string
	// Path is the path that triggered this particular route. If the hash
	// fallback is being used, the value of path does not include the '#'
	// symbol.
	Path string
	// InitialLoad is true iff this route was triggered during the initial
	// page load. I.e. it is true if this is the first path that the browser
	// was visiting when the javascript finished loading.
	InitialLoad bool
}
```

### Accessing Parameters

Parameters are accessed via `context.Params`. If we have a route defined
like this:

```go
r.HandleFunc("/people/{id}", showPerson)
```

And the path `/people/123` is triggered, then the parameters passed into peopleCtrl.Show
would look like the following:
<commit_msg>Fix function name in README<commit_after>Humble/Router
=============

[![GoDoc](https://godoc.org/github.com/go-humble/router?status.svg)](https://godoc.org/github.com/go-humble/router)

Version X.X.X (develop)

A router for client-side web applications written in pure go which compiles to
javascript via [gopherjs](https://github.com/gopherjs/gopherjs). Router works great as a
stand-alone package or in combintation with other packages in the
[Humble Framework](https://github.com/go-humble/humble).

Router supports the following features:

- Write code in pure go. It feels like go, follows go idioms, and compiles with the go tools.
- Each route consists of a path and a handler function which is triggered when path matches.
- Routes can have parameters, which are passed through as an argument to handler functions.
- Router uses history.pushState and listens to the onpopstate event in browsers that support
  it. In older browsers it automatically falls back to using a hash.
- Router can be configured to automatically intercept link click events, triggering the appropriate
  route instead of requesting a new page.


Browser Support
---------------

Router works with IE9+ (with a
[polyfill for typed arrays](https://github.com/inexorabletash/polyfill/blob/master/typedarray.js))
and all other modern browsers. Router compiles to javascript via [gopherjs](https://github.com/gopherjs/gopherjs)
and this is a gopherjs limitation.

Router is regularly tested with the latest versions of Firefox, Chrome, and Safari on Mac OS.
Each major or minor release is tested with IE9+ and the latest versions of Firefox and Chrome
on Windows.


Installation
------------

Install router like you would any other go package:

```bash
go get github.com/go-humble/router`
```

You will also need to install gopherjs if you don't already have it. The latest version is
recommended. Install gopherjs with:

```
go get -u github.com/gopherjs/gopherjs
```


Quickstart Guide
----------------

### Declaring Routes

Declaring routes works similaroy to other routing packages in go. Here's an example:

```go
// Create a new Router object
r := router.New()
// Use HandleFunc to add routes.
r.HandleFunc("/people", indexPeople)
r.HandleFunc("/people/{id}", showPerson)
// You must call Start in order to start listening for changes
// in the url and trigger the appropriate handler function.
r.Start()
```

The second argument to HandleFunc should be a router.Handler, which has the following
definition:

```go
type Handler func(context *Context)
```

And the Context type is defined as follows:

```go
type Context struct {
	// Params is the parameters from the url as a map of names to values.
	Params map[string]string
	// Path is the path that triggered this particular route. If the hash
	// fallback is being used, the value of path does not include the '#'
	// symbol.
	Path string
	// InitialLoad is true iff this route was triggered during the initial
	// page load. I.e. it is true if this is the first path that the browser
	// was visiting when the javascript finished loading.
	InitialLoad bool
}
```

### Accessing Parameters

Parameters are accessed via `context.Params`. If we have a route defined
like this:

```go
r.HandleFunc("/people/{id}", showPerson)
```

And the path `/people/123` is triggered, then the parameters passed into the showPerson
function would look like the following:

{'fraction_non_alphanumeric': 0.09494979326639102, 'fraction_numerical': 0.0014766686355581807, 'mean_word_length': 3.958272327964861, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 12, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '1581037', 'n_tokens_mistral': 1893, 'n_tokens_neox': 1807, 'n_words': 882}