# App Lifecycle

Copper provides an "App Container" that runs your application code. This container manages the app lifecycle i.e. running code during startup (ex. opening database connections) and shutdown (ex. closing database connections). It also listens to interrupt signals so it can allow for a graceful shutdown of the app.

The `copper.App` struct, returned by `copper.New()` contains a pre-configured [logger](/copper/core/logging.md), [configuration loader](/copper/core/configuration.md), and the app lifecycle manager - that you can use to add custom logic on app startup & shutdown.

### Cleanup Code

To run cleanup code when your app is shutting down, register the function using the app lifecycle -

```go
func NewServer(lc *clifecycle.Lifecycle) *Server {
  myServer := &Server{..}
  
  // Stop the server when the app is shutting down..
  lc.OnStop(func(ctx context.Context) error {
    return myServer.Stop(ctx)
  })
  
  return myServer
}
```


---

# 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/core/app-lifecycle.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.
