Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ dist-ssr
*.sln
*.sw?

.env
.env
.env.*
!.env.example
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ The project is being developed in stages. The frontend is built with React and T

## **Status**

The project is currently in an early stage (first progress submission). At the moment, the frontend uses local mock data (`src/data/countries.mock.json`) to demonstrate how the search, list, and detail views are intended to work. This data will be replaced by real-time calls to the GraphQL backend in a later submission.
The project is under active development. The frontend uses Apollo Client to fetch country data from the GraphQL backend, which retrieves data from PostgreSQL. The backend is hosted on the group's NTNU VM and managed using systemd.

Additional functionality, including user-generated country status and notes, is planned for later development.

## **Implemented so far:**

Expand Down Expand Up @@ -91,10 +93,18 @@ npm run dev

Open [http://localhost:5173/](http://localhost:5173/) in your browser.

The frontend uses Apollo Client to query the GraphQL backend. By default, the GraphQL endpoint is [http://localhost:3001/graphql](http://localhost:3001/graphql). It can be overridden with the `VITE_GRAPHQL_URL` environment variable.
The frontend uses Apollo Client to query the GraphQL backend. By default, the GraphQL endpoint is `http://localhost:3001/graphql`. When connecting to the backend hosted on the NTNU VM, the endpoint is `http://it2810-28.idi.ntnu.no:3001/`. The endpoint can be overridden with the `VITE_GRAPHQL_URL` environment variable.

Both the backend and the database connection must be available for country data to load.

### Backend Deployment (NTNU VM)

The GraphQL backend and PostgreSQL database are hosted on the group's NTNU VM (`it2810-28.idi.ntnu.no`).

The backend runs as a systemd service (`t28-backend`), allowing it to continue running after SSH logout and start automatically when the VM boots.

For information about the backend configuration, frontend communication, and starting, restarting, or monitoring the backend service, see [Backend Deployment](docs/backendDeployment.md).

### **Other Commands**

| Command | Description |
Expand Down
3 changes: 2 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"test": "echo \"Error: no test specified\" && exit 1",
"db:check": "node --env-file=.env src/checkDb.ts",
"db:seed": "node --env-file=.env src/seed.ts",
"dev": "node --env-file=.env --watch src/server.ts"
"dev": "node --env-file=.env --watch src/server.ts",
"start": "node --env-file=.env src/server.ts"
},
"keywords": [],
"author": "",
Expand Down
11 changes: 9 additions & 2 deletions backend/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { ApolloServer } from '@apollo/server';
import { startStandaloneServer } from '@apollo/server/standalone';
import { pool } from './db.ts';

const PORT = 3001;
const PORT = Number(process.env.PORT ?? 3001);
const HOST = process.env.HOST ?? '0.0.0.0';
const COUNTRY_COLUMNS = `
code, name, capital, continent, languages, population, area,
flag_url AS "flagUrl"
Expand Down Expand Up @@ -80,6 +81,12 @@ const resolvers = {
};

const server = new ApolloServer({ typeDefs, resolvers });
const { url } = await startStandaloneServer(server, { listen: { port: PORT } });

const { url } = await startStandaloneServer(server, {
listen: {
port: PORT,
host: HOST,
},
});

console.log(`GraphQL server ready at ${url}`);
14 changes: 14 additions & 0 deletions deploy/t28-backend.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[Unit]
Description=T28 GraphQL Backend
After=network.target postgresql.service

[Service]
Type=simple
User=guroklo
WorkingDirectory=/home/guroklo/T28-Project-2/backend
ExecStart=/usr/bin/npm run start
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
118 changes: 118 additions & 0 deletions docs/backendDeployment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# Backend Deployment

## Overview

The application consists of a React frontend, a GraphQL backend, and a PostgreSQL database.

The backend and database are hosted on the NTNU virtual machine (VM), `it2810-28.idi.ntnu.no`.

The backend is built with Node.js, TypeScript, and Apollo Server. It processes requests from the frontend and retrieves data from PostgreSQL.

## How the System Works

When a user interacts with the application, the frontend sends GraphQL requests to the backend.

The backend processes these requests, queries the PostgreSQL database, and returns the requested data to the frontend.

For example, when a user opens a country page, the frontend requests information about that country. The backend retrieves the relevant data from PostgreSQL and returns it for display.

The communication flow is:

User
|
v
React Frontend
|
| GraphQL request
v
Node.js Backend (NTNU VM)
|
| SQL query
v
PostgreSQL Database (NTNU VM)

The backend handles data retrieval, searching, filtering, and pagination, while the frontend handles the user interface.

## Backend Hosting

The backend runs on the NTNU VM and is managed by systemd, a Linux service manager.

Systemd allows the backend to:

- Continue running after SSH sessions are closed.
- Start automatically when the VM reboots.
- Restart automatically if the backend process fails.
- Be monitored through service status and logs.

The service configuration is defined in `deploy/t28-backend.service`.
The backend is available at [http://it2810-28.idi.ntnu.no:3001/](http://it2810-28.idi.ntnu.no:3001/) when connected to the NTNU network or VPN.

The systemd service runs under a Linux user with access to the backend directory. The `User` and `WorkingDirectory` settings must match the configuration on the VM.

## Starting and Restarting the Backend

To access the VM through SSH, a connection to the NTNU network or NTNU VPN is required.

```bash
ssh <ntnu-username>@it2810-28.idi.ntnu.no
```

Once connected, the backend can be managed using systemd.

**Start the backend:**

```bash
sudo systemctl start t28-backend
```

**Restart the backend:**

```bash
sudo systemctl restart t28-backend
```

**Stop the backend:**

```bash
sudo systemctl stop t28-backend
```

**Check the backend status:**

```bash
sudo systemctl status t28-backend
```

**View backend logs:**

```bash
sudo journalctl -u t28-backend -f
```

The backend is configured to start automatically when the VM reboots, so manual startup is normally unnecessary.

## Database

PostgreSQL runs on the same VM as the backend.

The database is only accessible locally on the VM and is not directly exposed to external networks.

The backend connects to PostgreSQL using environment variables stored in `backend/.env`.

This file contains database credentials and is excluded from Git to protect sensitive information.

For more information, see [Database Setup](databaseSetup.md).

## Frontend Communication

The frontend uses Apollo Client to communicate with the GraphQL API.

The backend endpoint is configured through the `VITE_GRAPHQL_URL` environment variable.

This allows the frontend to communicate with different backend environments without changing the application code.

The configured backend endpoint is `http://it2810-28.idi.ntnu.no:3001/`.

Users interact with the application through the frontend and do not need direct SSH access to the VM or database.

The GraphQL endpoint must be reachable from the frontend. The current setup uses HTTP with CORS configured to allow frontend requests.