-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
63ef594
commit 4612db7
Showing
10 changed files
with
983 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
name: Docker Image CI | ||
|
||
on: | ||
push: | ||
branches: ["main"] | ||
pull_request: | ||
branches: ["main"] | ||
|
||
permissions: | ||
contents: read | ||
packages: write | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Login to GitHub Container Registry | ||
uses: docker/login-action@v1 | ||
with: | ||
registry: ghcr.io | ||
username: ${{ github.actor }} | ||
password: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- name: Build and Push Docker Image | ||
run: | | ||
docker build . --tag ghcr.io/upayanmazumder/file-server:latest --build-arg NODE_ENV=production | ||
docker push ghcr.io/upayanmazumder/file-server:latest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
ARG NODE_VERSION=18.18.2 | ||
|
||
################################################################################ | ||
# Use node image for base image for all stages. | ||
FROM node:${NODE_VERSION}-alpine as base | ||
|
||
# Set working directory for all build stages. | ||
WORKDIR /usr/src/app | ||
|
||
################################################################################ | ||
# Create a stage for installing production dependencies. | ||
FROM base as deps | ||
|
||
# Download dependencies as a separate step to take advantage of Docker's caching. | ||
# Leverage a cache mount to /root/.yarn to speed up subsequent builds. | ||
# Leverage bind mounts to package.json and yarn.lock to avoid having to copy them | ||
# into this layer. | ||
RUN --mount=type=bind,source=package.json,target=package.json \ | ||
--mount=type=bind,source=yarn.lock,target=yarn.lock \ | ||
--mount=type=cache,target=/root/.yarn \ | ||
yarn install --frozen-lockfile | ||
|
||
################################################################################ | ||
# Create a stage for building the application. | ||
FROM deps as build | ||
|
||
# Copy the rest of the source files into the image. | ||
COPY . . | ||
|
||
# Run the build script. | ||
RUN yarn run build | ||
|
||
################################################################################ | ||
# Create a new stage to run the application with minimal runtime dependencies | ||
# where the necessary files are copied from the build stage. | ||
FROM base as final | ||
|
||
# Use production node environment by default. | ||
ENV NODE_ENV production | ||
ENV ORIGIN https://file-server.upayan.space | ||
|
||
# Run the application as a non-root user. | ||
USER node | ||
|
||
# Copy package.json so that package manager commands can be used. | ||
COPY package.json . | ||
|
||
# Copy the production dependencies from the deps stage and also | ||
# the built application from the build stage into the image. | ||
COPY --from=deps /usr/src/app/node_modules ./node_modules | ||
COPY --from=build /usr/src/app/dist ./dist | ||
COPY --from=build /usr/src/app/server ./server | ||
|
||
# Expose the port that the application listens on. | ||
EXPOSE 3200 | ||
|
||
# Run the application. | ||
CMD yarn serve |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const express = require('express'); | ||
const fileUpload = require('express-fileupload'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
|
||
const app = express(); | ||
const PORT = 3000; | ||
|
||
app.use(fileUpload()); | ||
app.use(express.static('uploads')); | ||
|
||
app.get('/', (req, res) => { | ||
res.send('Welcome to the File Sharing Server'); | ||
}); | ||
|
||
app.post('/upload', (req, res) => { | ||
if (!req.files || Object.keys(req.files).length === 0) { | ||
return res.status(400).send('No files were uploaded.'); | ||
} | ||
|
||
let uploadedFile = req.files.file; | ||
let uploadPath = path.join(__dirname, 'uploads', uploadedFile.name); | ||
|
||
uploadedFile.mv(uploadPath, (err) => { | ||
if (err) { | ||
return res.status(500).send(err); | ||
} | ||
|
||
res.send('File uploaded!'); | ||
}); | ||
}); | ||
|
||
app.get('/download/:filename', (req, res) => { | ||
let filename = req.params.filename; | ||
let filePath = path.join(__dirname, 'uploads', filename); | ||
|
||
if (fs.existsSync(filePath)) { | ||
res.download(filePath); | ||
} else { | ||
res.status(404).send('File not found'); | ||
} | ||
}); | ||
|
||
app.listen(PORT, () => { | ||
console.log(`Server is running on port ${PORT}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
services: | ||
file-server: | ||
image: upayanmazumder/file-server:latest | ||
ports: | ||
- 3200:3000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.