# Creating a new Configurator Project

*** WIP: there is currently no template. Currently a reference Project is used as a starting point. Alternatively the newest Schema can be exported without data. (SUGGESTION make a separate Project for the Konfigurator DB and have Tagged Versions and Releases).

## Subdomain creation and Service Preparation
 - Configurators live under subdomains of `subdomain.raumgleiter.com`  
 - Open the Hostpoint website and sign in. (login@raum... in Keepass).
 - Navigate to `/Domains/raumgleiter.com > DNS Zone bearbeiten` [^link]:  
   ![](_res/Creating_DNS.jpg)
 - Create a new subdomain A new configurator requires a specific new sub-domain, which can be created via adding an `A` type DNS-Record to the hosting server:
`projectName.raumgleiter.com	A	300	-	185.32.125.110`  
   ![](_res/Creating_DNS2.jpg)

[^link]: https://admin.hostpoint.ch/customer/Domains/Dns/Edit?_dflt_vsid_=3836da0e-5896-4bd7-8454-8f1457a4a84d&_vsid_=c5cf604a-0df2-44d9-b2b6-4231f07ebbfd&name=raumgleiter.com

## Create a Database
 - Open the Hostpoint website and sign in. (login@raum... in Keepass).
 - Navigate to `/Services/Datenbanken`  
   ![](_res/Creating_DB.jpg)
 - Click on `Datenbank erstellen`
 - Name it `konfiguratorDeinKonfigurator`
 - Create a new DB user and save their credentials in Keepass
 - the IP Adress of the follwoing machines has to be added to the Hosts Whitelist in order to access the DB:
    - Host Server (185.32.125.110)
    - Backup Server (85.195.242.219)
    - The IP of your Dev Machine(s) ([google for `your ip`](http://ip.hostpoint.ch))  
    - You can also skipp adding individual IPs for now and just add ```%``` to whitelist anyone. 
   ![](_res/Creating_DB2.jpg)

##### Copy an old Database
 - Make sure the [necessary programs](initial_setup.md) are installed.
 - Open `MySQL Workbench` on your computer.
 - Search KeePass for a user of an existing project:  
   ![](_res/Creating_DB3.jpg)
 - Adjust the hosts of the projects DB user to whitelist your machine.
 - Create Connection to the new DB and a DB of an existing project:  
   ![](_res/Creating_DB8.jpg)
 - Open the existing projects DB
 - Export the data to a file (Schema + Data):
   - Note: You can use the `Daten: Einfügen` option to decide whether you need the rows of the table and what to do with any existing rows in the DB that will import the data.
   ![](_res/Creating_DB9.jpg)
 - Restart HeidiSQL
 - Open the new projects DB
 - Use `Datei > SQL-Datei laden...`
 - Adjust the name in the opened SQL command to the new projects DB. Best use `[Ctrl + F]` to do so.
 ![](_res/Creating_DB10.jpg)
 - After the import has completed, click on refresh and check that the tables and their rows have been imported.
 ![](_res/Creating_DB11.jpg)

## Linux Port Mapping in Server
- Send a mail to Binarium with these Information:
```
Hi Luchin

Könntest Du mir Bitte ein Gefallen tun und die Einstellungen für die
folgende Konfigurator App vorzubereiten?:

User: configurator

URL: https://configurator-staging.raumgleiter.com
(DNS ist schon erstellt)

DB Infos / ENV_VARS:
Environment=DATABASE_HOST= raumglei.mysql.db.hostpoint.ch
Environment=DATABASE_NAME= raumglei_confStaging
Environment=DATABASE_USER= raumglei_cStage

Environment=DATABASE_PASS= 1234dasistkeinechtesPasswort
Environment=DATABASE_PORT= 3306

Whitelist: Host Maschine (185.32.125.110) ist schon «gewhitelistet» beim Hostpoint und auch die Backup Maschine (85.195.242.219).

Danke und liebe Grüsse,
```

## Creating a new Repository
Configurator Application repositories currently live under: [WEB -> CONFIRUATORS](https://git.raumgleiter.com/web/configurators).

### Duplicate an existing Project (Experimental)
 - Duplicate a git project using [this script](Utility/CreateConfiguratorRepo.bat).
 - Clone the project and make changes to it.

### (Existing Workflow)
##### Naming
- Name for Repository and slug: {{project_name}}_configurator
- Caps: small caps
- Spacing: use underscores, no spaces or special chars
##### To Improve
- Use Gitlab Templates (Upgrade to Gitlab EE)
- Create a Project "Template" in Gitlab and Export. Import at creation time.

### *Fragen*
 - Muss die DB zum Repo passen?
 - Kann man das deployment in einen Dist ordner erstellen ums nicht in gitignore adden zu müssen?

### First Steps

##### Replace project data
- update project name in README.md
- update DB credentials and Project Information
    - package.json
    - .vscode/launch.json
- replace analytics script for project in the HEAD of root/public/index.html

##### Install
 - run `npm install` in the root directory to install dependencies
 - run `npm run prod` to test if the application is working properly and can connect to the database. After running the command, the application should print the following:   
`...`    
`[nodemon] statring 'node app.js'`    
`Server UP at port: 3003`  
This means the application is running.
 - Go to a browser and enter the following test link:  
   http://localhost:3003/?T=1&O=1&Z=4.5&E=EG&W=108.2&V=630000&mt=24
 - Find a valid Type Code in the database
   - Connect to the database
   - In the `Abfrage` tab type `SELECT * FROM wk_types` and run it to view the `wk_types` table.
   ![](_res/Creating_Code.jpg)
 - On the website, fill in a type from the `code` column.
   ![](_res/Creating_Link.jpg)

#### Data Model
[Data Model](database_model.md)

Depending of the Base-Data and Base-App used for starting, the example Link will show one configuration.

### Deployment
 - Set deployment variables in "root/deployment.sh"
    - You get the app name from Binarium
 - Run the deployment script to test that the script prepares the files and initializes the service
   - Confirm with `yes` that the connection is trusted
   - Use the password from KeePass user `Configurator`
   - If the password doesn't work, type it manually.
   ![](_res/Creating_Deploy.jpg)
 - After running the script, a successfull deployment will print the service status
    - The deployment runs if the status is "active (running)"
 - Add the folder that was created by the deploy script to the `.gitignore` file.
    ![](_res/Creating_Deploy.jpg)
 - Go to a browser and enter the following test link with your subdomain and the `code` attribute:
   https://configurator-staging.raumgleiter.com/?T=TL01

##### Alternative
The script does not seem to be stable yet. If it doesn't work, follow these steps:
 - Connect using PuTTY
 - CD to the folder, install, restart and run
 ```
 cd yourconfiguratorproject
 npm install
 sudo systemctl restart yourconfiguratorproject
 systemctl status yourconfiguratorproject
 ```


### Edit Data and Application
To Make changes for the Application or the Data refer to the following Documents:
- [updating the application](updating_application.md)
- [updating the data](updating_data.md)
