# Routing

### Routers

Routers are simple Go structs that implement the `chttp.Router` interface. Routers can be used to define and group together HTTP routes.

```go
type Router interface {
    Routes() []Route
}
```

{% hint style="info" %}
By default, Copper provides `pkg/app/router.go`. Create more routers as your project grows to organize and group routes.
{% endhint %}

Add a router to a package using the Copper CLI -

```
copper scaffold:router <package>
```

### Sample Router

{% code title="pkg/rockets/router.go" %}

```go
package rockets

import (..)

type NewRouterParams struct {
	RW     *chttp.JSONReaderWriter
	Logger clogger.Logger
}

func NewRouter(p NewRouterParams) *Router {
	return &Router{
		rw:     p.RW,
		logger: p.Logger,
	}
}

// Router struct implements the `chttp.Router` interface. It holds all the
// dependencies needed for its handlers.
type Router struct {
	rw     *chttp.JSONReaderWriter
	logger clogger.Logger
}

// Routes returns a list of chttp.Route managed by this Router.
func (ro *Router) Routes() []chttp.Route {
	return []chttp.Route{
		{
			Path:    "/rockets/{id}/launch",
			Methods: []string{http.MethodPost},
			Handler: ro.HandleRocketLaunch,
		},
	}
}

// HandleRocketLaunch is invoked when a POST request is made to the 
// "/rockets/{id}/launch" endpoint
func (ro *Router) HandleRocketLaunch(w http.ResponseWriter, r *http.Request) {
	// Handle request here..
}
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gocopper.gitbook.io/copper/http/routing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
