Skip to content

Commit

Permalink
Merge pull request #4 from timeowilliams/setup-backend
Browse files Browse the repository at this point in the history
Setup backend
  • Loading branch information
timeowilliams authored Aug 25, 2024
2 parents 1a297ca + c6f7a39 commit 4f404da
Show file tree
Hide file tree
Showing 20 changed files with 1,625 additions and 166 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/integration_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Integration Tests
on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
test:
name: Run Integration Tests
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 'lts/*'

- name: Install dependencies
run: npm install
working-directory: ./backend

- name: Run Integration Tests
env:
MONGODB_URI: ${{ secrets.MONGODB_URI }}
JWT_SECRET: ${{ secrets.JWT_SECRET }}
run: npm run test:integration
working-directory: ./backend
23 changes: 23 additions & 0 deletions .github/workflows/package_size.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Package Size Report

on: pull_request

permissions:
pull-requests: write
contents: read

jobs:
pkg-size-report:
name: Package Size Report
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 20

- name: Package size report
uses: pkg-size/action@v1.1.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
26 changes: 2 additions & 24 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ jobs:
id-token: write # to enable use of OIDC for npm provenance
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 'lts/*'
- name: Install dependencies
Expand All @@ -33,25 +33,3 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: npx semantic-release

update-badge:
needs: release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Get latest release version
id: get_version
run: |
VERSION=$(gh release list --limit 1 | cut -f1)
echo "::set-output name=version::$VERSION"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Create badge
uses: schneegans/dynamic-badges-action@v1.6.0
with:
auth: ${{ secrets.GIST_SECRET }}
gistID: <your-gist-id>
filename: deepfocus-version.json
label: version
message: ${{ steps.get_version.outputs.version }}
color: blue
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</p>

[![Release](https://github.com/timeowilliams/deepFocus/actions/workflows/release.yml/badge.svg)](https://github.com/timeowilliams/deepFocus/actions/workflows/release.yml)
[![Integration Tests](https://github.com/timeowilliams/deepFocus/actions/workflows/integration_tests.yml/badge.svg)](https://github.com/timeowilliams/deepFocus/actions/workflows/integration_tests.yml)
[![Version](https://img.shields.io/npm/v/project.svg)](https://www.npmjs.com/package/project)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)

Expand Down Expand Up @@ -50,7 +51,7 @@ pnpm install

## Usage


Note, for running this app locally, you may run into issues with active-window. Try running `npm install --ignore-scripts` to fix this. More info [here](https://github.com/sindresorhus/active-window/issues/10).

## Goals

Expand Down Expand Up @@ -92,4 +93,4 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file

[Timeo Williams] - [@timeowilliams](https://twitter.com/timeowilliams) - timeo.williams@gmail.com

Project Link: [https://github.com/timeowilliams/deepFocus](https://github.com/timeowilliams/deepFocus)
Project Link: [https://github.com/timeowilliams/deepFocus](https://github.com/timeowilliams/deepFocus)
9 changes: 9 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Node modules
node_modules/

# Build outputs
dist/
out/

# Environment variables
.env
65 changes: 65 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
## Getting Started

### Prerequisites

- Node.js LTS >=v20.12.2
- npm v10.5.0 or pnpm
- MongoDB instance

### Installation

#### Backend

1. Navigate to the `backend` directory:

```bash
cd backend
```

2. Install dependencies:

```bash
pnpm install
```

3. Create a `.env` file with your MongoDB URI:

```env
MONGO_URI=your_mongodb_uri
JWT_SECRET=your_jwt_secret
```

4. Start the backend server:
```bash
pnpm start
```

#### Frontend

1. Navigate to the `frontend` directory:

```bash
cd frontend
```

2. Install dependencies:

```bash
pnpm install
```

3. Start the Electron app:
```bash
pnpm start
```

## Goals

- [x] Allow all users to download on any machine through an Electron JS app
- [x] Migrate to TypeScript and implement SolidJS
- [x] Set up CI/CD pipeline and automatic releases
- [x] Implement changelog using conventional commits
- [ ] Allow users to enter session goals and customize productive/unproductive sites
- [ ] Migrate from electron-storage to SQLite for improved data handling
- [ ] Add integration and automated tests
- [ ] Implement user authentication and cloud-based data persistence using MongoDB and JWT
45 changes: 45 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "deepfocus-backend",
"version": "1.0.0",
"type": "module",
"main": "src/server.ts",
"scripts": {
"start": "tsx src/server.ts",
"dev": "tsx watch .",
"test": "echo \"Error: no test specified\" && exit 1",
"format": "prettier --write \"**/*.+(js|ts|jsx|tsx|json|css|md|yml|yaml|html)\"",
"format:check": "prettier --check \"**/*.+(js|ts|jsx|tsx|json|css|md|yml|yaml|html)\"",
"build": "copyfiles -u 1 src/**/* dist",
"test:integration": "tsx node_modules/mocha/bin/mocha src/tests/integration/**/*.test.ts --exit"
},
"dependencies": {
"bcrypt": "^5.1.1",
"bcryptjs": "^2.4.3",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.17.1",
"format": "^0.2.2",
"jsonwebtoken": "^8.5.1",
"mongoose": "^8.5.4",
"tsx": "^4.18.0"
},
"devDependencies": {
"@types/bcrypt": "^5.0.2",
"@types/chai": "^4.3.17",
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/jsonwebtoken": "^9.0.6",
"@types/mocha": "^10.0.7",
"@types/supertest": "^6.0.2",
"chai": "^5.1.1",
"crypto": "^1.0.1",
"mocha": "^10.7.3",
"prettier": "^3.3.3",
"supertest": "^7.0.0",
"ts-node": "^9.1.1",
"typescript": "^4.2.4"
},
"engines": {
"node": ">=20.0.0"
}
}
31 changes: 31 additions & 0 deletions backend/src/connectToDB.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import mongoose from 'mongoose'
import dotenv from 'dotenv'

dotenv.config()
const uri = process.env.MONGODB_URI || ''

if (!uri) {
throw new Error('MONGODB_URI is not defined in the environment variables')
}

async function connectToDB(): Promise<typeof mongoose> {
try {
// Connect to the MongoDB server using Mongoose
await mongoose.connect(uri)
console.log('Connected successfully to MongoDB server')
return mongoose
} catch (error) {
console.error('Error connecting to the database:', error)
throw error
}
}
async function closeDBConnection(): Promise<void> {
try {
await mongoose.connection.close()
console.log('Disconnected successfully from MongoDB server')
} catch (error) {
console.error('Error closing the database connection:', error)
}
}

export { connectToDB, closeDBConnection }
32 changes: 32 additions & 0 deletions backend/src/models/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import mongoose, { Document, Schema } from 'mongoose'

// Define the User interface
interface IUser extends Document {
username: string
password: string
firstName: string
lastName: string
country: string
language: string
}

// Create the User schema
const UserSchema: Schema = new Schema({
username: {
type: String,
required: true,
unique: true,
match: [/.+@.+\..+/, 'Please fill a valid email address'] // Email validation
},
password: { type: String, required: true },
firstName: { type: String, required: true },
lastName: { type: String, required: true },
country: { type: String, required: true },
language: { type: String, required: true }
})

// Create the User model
const User = mongoose.model<IUser>('User', UserSchema)

export default User
export { IUser }
Loading

0 comments on commit 4f404da

Please sign in to comment.