# Configure project for production

Note: This guide assumes that you use the default hostname, `central.internal` as hostname for your central system.

## Create production build

### Central
The production release of central backend and frontend can be realised on a computer which has NodeJS and Rust installed by executing the following command at the root of the project:

```bash
make
```

The backend will be available at this location:

```
central_backend/target/release/central_backend
```

### Python device
The Python device isn't production ready yet.


### ESP32 device

#### Flashing the device directly
Use the following commands to flash a device (inside ESP-IDF environnment):

```bash
idf.py build
idf.py flash
```


#### Getting an OTA update
Use the following command to build an OTA update:

```bash
idf.py build
```

The OTA update is then located in `build/main.bin`


## Pre-requisites
* A server running a recent Linux (Debian / Ubuntu preferred) with `central` as hostname
* DHCP configured on the network

## Configure DNS server

If you need to setup a DNS server / proxy to point `central.internal` to the central server IP, you can follow this guide.

### Retrieve DNS server binary
Use [DNSProxy](https://gitlab.com/pierre42100/dnsproxy) as DNS server. Get and compile the sources:

```bash
git clone https://gitlab.com/pierre42100/dnsproxy
cd dnsproxy
cargo build --release
scp target/release/dns_proxy USER@CENTRAL_IP:/home/USER
```

Then, on the target server, install the binary to its final destination:

```bash
sudo mv dns_proxy /usr/local/bin/
```

### Configure DNS server
Configure the server as a service `/etc/systemd/system/dns.service`:

```conf
[Unit]
Description=DNS server
After=syslog.target
After=network.target

[Service]
RestartSec=2s
Type=simple
User=root
Group=root
WorkingDirectory=/tmp
ExecStart=/usr/local/bin/dns_proxy -l "CENTRAL_IP:53" -c "central.internal. A CENTRAL_IP"
Restart=always

[Install]
WantedBy=multi-user.target
```

Enable and start the new service:

```bash
sudo systemctl enable dns
sudo systemctl start dns
```

Check that it works correctly:

```bash
dig central.internal. @CENTRAL_IP
```

You should get an entry like this if it works:

```
;; ANSWER SECTION:
central.internal.	0	IN	A	CENTRAL_IP
```

Then, in your DHCP service, define the central as the DNS server.

## Configure server

### Create a user dedicated to the central
```bash
sudo adduser --disabled-login central
```

### Install binary
You can use `scp` to copy the binary to the target server:

```bash
scp central_backend/target/release/central_backend pierre@central:/home/pierre
```

Then the executable must be installed system-wide:

```bash
sudo mv central_backend /usr/local/bin/
```

### Create configuration file
Create a configuration file in `/home/central/config.yaml`:

```bash
sudo touch /home/central/config.yaml
sudo chown central:central /home/central/config.yaml
sudo chmod 400 /home/central/config.yaml
sudo nano /home/central/config.yaml
```

Sample configuration:

```conf
SECRET=RANDOM_VALUE
COOKIE_SECURE=true
LISTEN_ADDRESS=0.0.0.0:443
ADMIN_USERNAME=admin
ADMIN_PASSWORD=FIXME
HOSTNAME=central.internal
STORAGE=/home/central/storage
FRONIUS_ORIG=http://10.0.0.10
```

### Test configuration
Run the following command to check if the configuration is working:

```bash
sudo -u central central_backend -c /home/central/config.yaml fronius -c
```

### Create systemd unit file
Once you confirmed the configuration is working, you can configure a system service, in `/etc/systemd/system/central.service`:

```conf
[Unit]
Description=Central backend server
After=syslog.target
After=network.target

[Service]
RestartSec=2s
Type=simple
User=central
Group=central
WorkingDirectory=/home/central
ExecStart=/usr/local/bin/central_backend -c /home/central/config.yaml fronius -c
Restart=always
Environment=USER=central 
HOME=/home/central

[Install]
WantedBy=multi-user.target
```

Enable & start service:
```bash
sudo systemctl enable central
sudo systemctl start central
```