Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add workflow for test #33

Merged
merged 7 commits into from
Sep 15, 2023
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
30 changes: 30 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Run tests

on:
push:
branches: ['master']
pull_request:
# The branches below must be a subset of the branches above
branches: ['master']

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Git checkout
uses: actions/checkout@v3

- name: Use Node.js
uses: actions/setup-node@v3

- name: Start MongoDB
uses: supercharge/mongodb-github-action@1.10.0

- run: npm install

- run: cp -R app/config.sample/ app/config/

- run: npm run test:all
env:
CI: true
10 changes: 10 additions & 0 deletions app/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const server = require('./server');
const serverConfig = require('./config/server.config');

// Set the port based on environment or configuration
const PORT = process.env.PORT || serverConfig.PORT;

// Start the server
server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});
8 changes: 1 addition & 7 deletions app/config.sample/db.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
const dbConfig = {
USER: 'user-name',
PASSWORD: 'password',
DB: 'db-name',
};

module.exports = {
URL: `mongodb+srv://${dbConfig.USER}:${dbConfig.PASSWORD}@cluster123.xyz.mongodb.net/${dbConfig.DB}`,
URL: 'mongodb://localhost:27017',
};
33 changes: 12 additions & 21 deletions app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,24 @@ const db = require('./models');
const dbConfig = require('./config/db.config');
const morgan = require('../morgan-config'); // Import the custom Morgan configuration
const rolesSeeding = require('./seed/role.seed');
const serverConfig = require('./config/server.config');
const userRoutes = require('./routes/user.routes');

const app = express();
const server = express();

// Enable security headers with helmet
app.use(helmet());
server.use(helmet());

// Enable gzip compression for responses
app.use(compression());
server.use(compression());

// Set up CORS for specific origins
const corsOptions = {
origin: clientConfig.ORIGIN,
};
app.use(cors(corsOptions));
server.use(cors(corsOptions));

// Use morgan with the custom tokens
app.use(
server.use(
morgan(
':method :url :colored-status :response-time ms - :colored-ip - :user-agent'
)
Expand All @@ -39,11 +38,11 @@ const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // Limit each IP to 100 requests per windowMs
});
app.use(limiter);
server.use(limiter);

// Parse JSON and URL-encoded requests
app.use(express.json());
app.use(express.urlencoded({extended: true}));
server.use(express.json());
server.use(express.urlencoded({extended: true}));

// Connect to MongoDB and seed roles
db.mongoose
Expand All @@ -61,20 +60,12 @@ db.mongoose
});

// Define a root route
app.get('/', (req, res) => {
server.get('/', (req, res) => {
res.json({message: 'Welcome to our production-ready application.'});
});

// Mount the routes
app.use('/api/auth', authRoutes);
app.use('/api/test', userRoutes);
server.use('/api/auth', authRoutes);
server.use('/api/test', userRoutes);

// Set the port based on environment or configuration
const PORT = process.env.PORT || serverConfig.PORT;

// Start the server
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});

module.exports = app;
module.exports = server;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
},
"main": "server.js",
"scripts": {
"start:server": "nodemon ./app/server.js",
"start:server": "nodemon ./app/app.js",
"lint:stage": "lint-staged",
"lint:commit": "commitlint --edit",
"format:prettier": "prettier -w .",
"lint": "eslint .",
"lint:fix": "eslint --fix .",
"test:all": "jest --watchAll",
"test:all": "jest",
"coverage": "jest -i --coverage"
},
"repository": {
Expand Down