Skip to content

Commit

Permalink
ggg
Browse files Browse the repository at this point in the history
  • Loading branch information
upayanmazumder committed Oct 24, 2024
1 parent 63ef594 commit 4612db7
Show file tree
Hide file tree
Showing 10 changed files with 983 additions and 46 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/docker-image.yml
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
58 changes: 58 additions & 0 deletions Dockerfile
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
46 changes: 46 additions & 0 deletions api/server.js
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}`);
});
5 changes: 5 additions & 0 deletions docker-compose.yml
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
10 changes: 10 additions & 0 deletions qwik/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,13 @@ By default, the Cloudflare pages adaptor _does not_ include a `public/_routes.js
In the above example, it's saying _all_ pages should be SSR'd. However, the root static files such as `/favicon.ico` and any static assets in `/build/*` should be excluded from the Functions, and instead treated as a static file.

In most cases the generated `dist/_routes.json` file is ideal. However, if you need more granular control over each path, you can instead provide you're own `public/_routes.json` file. When the project provides its own `public/_routes.json` file, then the Cloudflare adaptor will not auto-generate the routes config and instead use the committed one within the `public` directory.

## Express Server

This app has a minimal [Express server](https://expressjs.com/) implementation. After running a full build, you can preview the build using the command:

```
npm run serve
```

Then visit [http://localhost:8080/](http://localhost:8080/)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { cloudflarePagesAdapter } from "@builder.io/qwik-city/adapters/cloudflare-pages/vite";
import { nodeServerAdapter } from "@builder.io/qwik-city/adapters/node-server/vite";
import { extendConfig } from "@builder.io/qwik-city/vite";
import baseConfig from "../../vite.config";

Expand All @@ -7,9 +7,9 @@ export default extendConfig(baseConfig, () => {
build: {
ssr: true,
rollupOptions: {
input: ["src/entry.cloudflare-pages.tsx", "@qwik-city-plan"],
input: ["src/entry.express.tsx", "@qwik-city-plan"],
},
},
plugins: [cloudflarePagesAdapter()],
plugins: [nodeServerAdapter({ name: "express" })],
};
});
Loading

0 comments on commit 4612db7

Please sign in to comment.