# Configure Spring for Upsun Fixed


You now have a *project* running on Upsun Fixed.
In many ways, a project is just a collection of tools around a Git repository.
Just like a Git repository, a project has branches, called *environments*.
Each environment can then be activated.
*Active* environments are built and deployed,
giving you a fully isolated running site for each active environment.

Once an environment is activated, your app is deployed through a cluster of containers.
You can configure these containers in three ways, each corresponding to a [YAML file](https://fixed.docs.upsun.com/learn/overview/yaml):

- **Configure apps** in a `.platform.app.yaml` file.
  This controls the configuration of the container where your app lives.
- **Add services** in a `.platform/services.yaml` file.
  This controls what additional services are created to support your app,
  such as databases or search servers.
  Each environment has its own independent copy of each service.
  If you're not using any services, you don't need this file.
- **Define routes** in a `.platform/routes.yaml` file.
  This controls how incoming requests are routed to your app or apps.
  It also controls the built-in HTTP cache.
  If you're only using the single default route, you don't need this file.

Start by creating empty versions of each of these files in your repository:

```bash
# Create empty Upsun Fixed  configuration files
mkdir -p .platform && touch .platform/services.yaml && touch .platform/routes.yaml
```

Now that you've added these files to your project,
configure each one for Spring in the following sections.
Each section covers basic configuration options and presents a complete example
with comments on why Spring requires those values.

## Configure apps in `.platform.app.yaml`

Your app configuration in a  `.platform.app.yaml` file is allows you to configure nearly any aspect of your app.
For all of the options, see a [complete reference](https://fixed.docs.upsun.com/create-apps/app-reference/single-runtime-image.md).
The following example shows a complete configuration with comments to explain the various settings.

Explaining the file line by line, notice the following settings:

 - ``name``: The application name.
 - ``type`` Where you define the language, in this case, Java, and the version.
 - ``disk``: The disk space that the application needs in megabytes.
 - ``hooks.build``: The command to package the application.
 - ``web.commands``: The order to start the application, where the port is overwritten with ``--server.port=$PORT``,
using the ``PORT`` environment variable provided by Upsun Fixed to the application container.

```yaml  {location=".platform.app.yaml"}
# This file describes an application. You can have multiple applications
# in the same project.
#
# See https://docs.upsun.com/anchors/fixed/app/configure/

# The name of this app. Must be unique within a project.
name: app

# The runtime the application uses.
type: "java:11"

disk: 1024

# The hooks executed at various points in the lifecycle of the application.
hooks:
    build: mvn clean package

# The relationships of the application with services or other applications.
#
# The left-hand side is the name of the relationship as it will be exposed
# to the application in the PLATFORM_RELATIONSHIPS variable. The right-hand
# side is in the form `:`.
relationships:
    database: "db:mongodb"

# The configuration of app when it is exposed to the web.
web:
    commands:
        start: java -jar $JAVA_OPTS target/spring-mvc-maven-mongodb.jar --server.port=$PORT

```

## Add services in `.platform/services.yaml`

You can add the managed services you need for you app to run in the  `.platform/services.yaml` file.
You pick the major version of the service and security and minor updates are applied automatically,
so you always get the newest version when you deploy.
You should always try any upgrades on a development branch before pushing to production.

Spring doesn't require services to deploy, so you don't need a `.platform/services.yaml` file for now.
You can [add other services](https://fixed.docs.upsun.com/add-services.md) if desired,
such as [Solr](https://fixed.docs.upsun.com/add-services/solr.md) or [Elasticsearch](https://fixed.docs.upsun.com/add-services/elasticsearch.md).
You need to configure Spring to use those services once they're enabled.

## Define routes

All HTTP requests sent to your app are controlled through the routing and caching you define in a `.platform/routes.yaml` file.

The two most important options are the main route and its caching rules.
A route can have a placeholder of `{default}`,
which is replaced by your domain name in production and environment-specific names for your preview environments.
The main route has an `upstream`, which is the name of the app container to forward requests to.

You can enable [HTTP cache](https://fixed.docs.upsun.com/define-routes/cache.md).
The router includes a basic HTTP cache.
By default, HTTP caches includes all cookies in the cache key.
So any cookies that you have bust the cache.
The `cookies` key allows you to select which cookies should matter for the cache.

You can also set up routes as [HTTP redirects](https://fixed.docs.upsun.com/define-routes/redirects.md).
In the following example, all requests to `www.{default}` are redirected to the equivalent URL without `www`.
HTTP requests are automatically redirected to HTTPS.

If you don't include a `.platform/routes.yaml` file, a single default route is used.
This is equivalent to the following:

```yaml  {location=".platform/routes.yaml"}
https://{default}/:
  type: upstream
  upstream: <APP_NAME>:http
```

Where `<APP_NAME>` is the `name` you've defined in your [app configuration](#configure-apps-in-platformappyaml).

The following example presents a complete definition of a main route for a Spring app:

```bash  {location=".platform/routes.yaml"}
# The routes of the project.
#
# Each route describes how an incoming URL is going
# to be processed by Upsun Fixed.

"https://{default}/":
  type: upstream
  upstream: "app:http"

"https://www.{default}/":
  type: redirect
  to: "https://{default}/"

```


