Skip to content

Commit c477bd6

Browse files
committed
refactoring + add node app
1 parent 7fb02c6 commit c477bd6

26 files changed

+2869
-131
lines changed

.github/workflows/app-python-ci.yml

Lines changed: 0 additions & 68 deletions
This file was deleted.

.github/workflows/node-ci.yml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
name: Node.js CI
2+
3+
on:
4+
push:
5+
paths:
6+
- 'app_node/**'
7+
- '.github/workflows/node-ci.yml'
8+
pull_request:
9+
paths:
10+
- 'app_node/**'
11+
- '.github/workflows/node-ci.yml'
12+
13+
defaults:
14+
run:
15+
working-directory: ./app_node # Set the default directory of the Node.js project
16+
17+
jobs:
18+
19+
lint-and-test:
20+
runs-on: ubuntu-latest
21+
22+
steps:
23+
- name: Checkout code repository
24+
uses: actions/checkout@v2
25+
26+
- name: Set up Node.js
27+
uses: actions/setup-node@v2
28+
with:
29+
node-version: '14' # Change to your preferred Node.js version
30+
31+
- name: Install dependencies
32+
run: |
33+
npm install
34+
35+
- name: Lint with ESLint
36+
run: |
37+
npx eslint .
38+
39+
- name: Run tests
40+
run: |
41+
npx mocha test.js
42+
43+
security-check:
44+
runs-on: ubuntu-latest
45+
permissions:
46+
actions: read
47+
contents: read
48+
security-events: write
49+
50+
steps:
51+
- name: Checkout code repository
52+
uses: actions/checkout@v2
53+
54+
- name: Run Snyk to check for vulnerabilities
55+
uses: snyk/actions/node@master
56+
continue-on-error: true # To make sure that SARIF upload gets called
57+
env:
58+
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
59+
with:
60+
args: --all-projects --sarif
61+
62+
build-and-push:
63+
needs: [lint-and-test, security-check]
64+
runs-on: ubuntu-latest
65+
66+
steps:
67+
68+
- name: Checkout code repository
69+
uses: actions/checkout@v2
70+
71+
- name: Set up Docker Buildx
72+
uses: docker/setup-buildx-action@v3
73+
74+
- name: Login to Docker Hub
75+
uses: docker/login-action@v1
76+
with:
77+
username: ${{ secrets.DOCKER_USERNAME }}
78+
password: ${{ secrets.DOCKER_PASSWORD }}
79+
80+
- name: Build and push Docker image
81+
run: |
82+
docker buildx build -t m4k4rich/random-node-app:latest .
83+
docker buildx build --push -t m4k4rich/random-node-app:latest .
84+
working-directory: ./app_node # Change to your project directory

.github/workflows/python-ci.yml

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
name: Python CI
2+
3+
on:
4+
push:
5+
paths:
6+
- 'app_python/**'
7+
- '.github/workflows/python-ci.yml'
8+
pull_request:
9+
paths:
10+
- 'app_python/**'
11+
- '.github/workflows/python-ci.yml'
12+
13+
defaults:
14+
run:
15+
working-directory: ./app_python # We set the default directory of python project
16+
17+
jobs:
18+
19+
lint-and-test:
20+
runs-on: ubuntu-latest
21+
strategy:
22+
matrix:
23+
python-version: ["3.10"]
24+
25+
steps:
26+
- name: Checkout code repository
27+
uses: actions/checkout@v2
28+
29+
- name: Set up Python
30+
uses: actions/setup-python@v2
31+
with:
32+
python-version: ${{ matrix.python-version }}
33+
cache: 'pip'
34+
cache-dependency-path: |
35+
./app_python/requirements.txt
36+
37+
- name: Install dependencies
38+
run: |
39+
python -m pip install --upgrade pip
40+
pip install -r requirements.txt
41+
42+
- name: Lint with flake8
43+
run: |
44+
pip install flake8
45+
flake8 .
46+
47+
- name: Run unit tests
48+
run: |
49+
python test_app.py
50+
51+
security-check:
52+
runs-on: ubuntu-latest
53+
permissions:
54+
actions: read
55+
contents: read
56+
security-events: write
57+
58+
steps:
59+
- name: Checkout code repository.
60+
uses: actions/checkout@v3
61+
62+
- name: Check for vulnerabilities using Snyk
63+
uses: snyk/actions/python-3.10@master
64+
continue-on-error: true # To make sure that SARIF upload gets called
65+
env:
66+
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
67+
with:
68+
args:
69+
--package-manager=pip
70+
--skip-unresolved
71+
--file=app_python/requirements.txt
72+
73+
build-and-push:
74+
needs: [lint-and-test, security-check]
75+
runs-on: ubuntu-latest
76+
77+
steps:
78+
79+
- name: Checkout code repository
80+
uses: actions/checkout@v2
81+
82+
- name: Set up Docker Buildx
83+
uses: docker/setup-buildx-action@v3
84+
85+
- name: Login to Docker Hub
86+
id: login
87+
uses: docker/login-action@v1
88+
with:
89+
registry: docker.io
90+
username: ${{ secrets.DOCKER_USERNAME }}
91+
password: ${{ secrets.DOCKER_PASSWORD }}
92+
93+
- name: Build Docker image
94+
run: |
95+
docker buildx build -t m4k4rich/time-python-app:latest .
96+
docker buildx build --push -t m4k4rich/time-python-app:latest .
97+
working-directory: ./app_python # Change to your project directory
98+
99+
- name: Check Docker Hub Login Status
100+
run: echo "Logged in to Docker Hub successfully."
101+
102+
- name: Logout from Docker Hub
103+
run: docker logout
104+
105+

app_node/.eslintrc.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
env:
2+
browser: true
3+
commonjs: true
4+
es2021: true
5+
plugins:
6+
- "mocha"
7+
ignorePatterns:
8+
- "**/test.js"
9+
extends: eslint:recommended
10+
parserOptions:
11+
ecmaVersion: latest
12+
rules: {}

app_node/DOCKER.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Containerization Lab
2+
3+
> In this .md file, I describe how I crafted the dockerfile, what best practices I used. How did I build and test an image. How did I push and pull an image, verifying and validating its functionality.
4+
5+
## Docker application ( `Quick Guide` )
6+
7+
> You can find my image on dockerhub, the link is clickable - [my-node-app](https://hub.docker.com/layers/m4k4rich/my-node-app/dev/images/sha256:ae865650ef996ee89da47f6bda8182234f62f24a43d4210e96e0a2fd9db4af51)
8+
9+
1. **How to build?**
10+
11+
- Clone a repository.
12+
- Change directory to app_python.
13+
- Run ```docker build -t registry/name/tag .```
14+
15+
2. **How to pull?**
16+
17+
- Login in your dockerhub account.
18+
- Run ```docker pull m4k4rich/my-node-app/dev```
19+
20+
3. **How to run?**
21+
22+
- Pull or build an image first.
23+
- Run ```docker run -p PORT:8080 m4k4rich/my-node-app/dev``` instead of `PORT` specify which port you want to use**

app_node/Dockerfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Use an official Node.js runtime as a parent image
2+
FROM node:14-alpine
3+
4+
# Set the working directory in the container
5+
WORKDIR /app
6+
7+
# Copy all of the application files to the container
8+
COPY . /app
9+
10+
# Create user and set ownership and permissions as required
11+
RUN adduser myuser -D && chown -R myuser /app
12+
13+
# Pick the running user
14+
USER myuser
15+
16+
# Expose the port on which your Node.js app will run
17+
EXPOSE 3000
18+
19+
# Start the Node.js application
20+
CMD ["node", "app.js"]

app_node/NODE.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Node.js Web Application
2+
![Node-CI workflow](https://github.com/m4k4rich/core-course-labs/actions/workflows/node-ci.yml/badge.svg)
3+
4+
> This is a Node.js application that showcases the current time in Moscow, adhering to best practices and coding standards. It has been thoroughly tested, ensuring that the time updates seamlessly upon page refresh.
5+
6+
## Framework Selection
7+
8+
**Node.js is an excellent choice for this task for the following reasons:**
9+
10+
1. Efficiency and Speed:
11+
- Node.js is known for its event-driven, non-blocking I/O architecture. This makes it highly efficient and responsive, ideal for delivering real-time updates like the current time.
12+
13+
2. Ecosystem:
14+
- Node.js boasts a rich ecosystem of packages and libraries, enabling me to easily implement the required functionality without reinventing the wheel.
15+
16+
## Adherence to Best Practices
17+
Code Consistency:
18+
- I ensured that the codebase maintained a consistent style, adhering to the established best practices within the Node.js community.
19+
20+
## Coding Standards
21+
22+
> While Node.js doesn't enforce strict coding standards, there are some common conventions and best practices followed by developers when writing Node.js code.
23+
24+
1. Use const and let:
25+
- In this script, const is used for variables that should not be reassigned (http, server, port), and let is used for variables (randomNumber) that may change their value.
26+
27+
2. Arrow Functions:
28+
- The script uses arrow functions for defining the callback functions, such as (req, res) => {...} and () => {...}. Arrow functions are commonly used in Node.js for concise and clean syntax.

app_node/app.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const http = require('http');
2+
3+
const server = http.createServer((req, res) => {
4+
// Generate a random number between 1 and 21
5+
const randomNumber = Math.floor(Math.random() * 21) + 1;
6+
7+
// Set the response headers
8+
res.writeHead(200, { 'Content-Type': 'text/plain' });
9+
10+
// Send the random number as the response
11+
res.end(`Random Number: ${randomNumber}\n`);
12+
});
13+
14+
const port = 3000;
15+
server.listen(port, () => {
16+
console.log(`Server is running on http://localhost:${port}`);
17+
});

0 commit comments

Comments
 (0)