Skip to content

Commit

Permalink
improve docker compose setup, mount config so not required in build
Browse files Browse the repository at this point in the history
  • Loading branch information
tdjsnelling committed Sep 9, 2022
1 parent 4a37894 commit 586fe5d
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 21 deletions.
5 changes: 2 additions & 3 deletions api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
FROM node:16-alpine
WORKDIR /app
COPY config.js /config.js
COPY api/ .
WORKDIR /sqtracker/app
COPY . .
RUN yarn
EXPOSE 44444
CMD yarn start
2 changes: 1 addition & 1 deletion api/src/controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getTorrentsPage } from './torrent'
import { getUserRatio } from '../utils/ratio'
import { mail } from '../index'

const sendVerificationEmail = async (address, token) => {
export const sendVerificationEmail = async (address, token) => {
await mail.sendMail({
from: `"${process.env.SQ_SITE_NAME}" <${process.env.SQ_MAIL_FROM_ADDRESS}>`,
to: address,
Expand Down
2 changes: 1 addition & 1 deletion api/src/middleware/announce.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const handleAnnounce = async (req, res, next) => {
ratio !== -1 &&
Number(params.left > 0)
) {
res.statusMessage = 'Ratio is below minimum threshold'
res.statusMessage = `Ratio is below minimum threshold ${process.env.SQ_MINIMUM_RATIO}`
res.sendStatus(403)
return
}
Expand Down
19 changes: 18 additions & 1 deletion api/src/setup/createAdminUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ import bcrypt from 'bcrypt'
import crypto from 'crypto'
import jwt from 'jsonwebtoken'
import User from '../schema/user'
import { sendVerificationEmail } from '../controllers/user'

const createAdminUser = async () => {
const existingAdmin = await User.findOne({ username: 'admin' }).lean()
if (!existingAdmin) {
const created = Date.now()

const hash = await bcrypt.hash('admin', 10)

const adminUser = new User({
username: 'admin',
email: 'admin@sqtracker',
email: process.env.SQ_ADMIN_EMAIL,
role: 'admin',
password: hash,
created,
Expand All @@ -29,7 +32,21 @@ const createAdminUser = async () => {
},
process.env.SQ_JWT_SECRET
)

await adminUser.save()

const emailVerificationValidUntil = created + 48 * 60 * 60 * 1000
const emailVerificationToken = jwt.sign(
{
user: process.env.SQ_ADMIN_EMAIL,
validUntil: emailVerificationValidUntil,
},
process.env.SQ_JWT_SECRET
)
await sendVerificationEmail(
process.env.SQ_ADMIN_EMAIL,
emailVerificationToken
)
}
}

Expand Down
3 changes: 2 additions & 1 deletion client/.dockerignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
**/node_modules/
**/node_modules/
.next
17 changes: 8 additions & 9 deletions client/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
FROM node:16-alpine AS builder
WORKDIR /app
COPY config.js /config.js
COPY client/ .
WORKDIR /sqtracker
COPY . .
RUN yarn install
RUN echo "{}" > /config.js
RUN ./node_modules/.bin/next build

FROM node:16-alpine
WORKDIR /app
COPY --from=builder /config.js /config.js
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/next.config.js ./next.config.js
WORKDIR /sqtracker/client
COPY --from=builder /sqtracker/node_modules ./node_modules
COPY --from=builder /sqtracker/public ./public
COPY --from=builder /sqtracker/.next ./.next
COPY --from=builder /sqtracker/next.config.js ./next.config.js
EXPOSE 44445
CMD ["node_modules/.bin/next", "start", "-p", "44445"]
1 change: 1 addition & 0 deletions client/components/Notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export const NotificationsProvider = ({ children }) => {
p="20px"
pb="40px"
overflowX="hidden"
zIndex={999}
_css={{ '> * + *': { mt: 3 } }}
>
{notifications.map((notification, i) => (
Expand Down
14 changes: 9 additions & 5 deletions docker-compose.yml → docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ services:
api:
container_name: sq_api
build:
context: .
dockerfile: ./api/Dockerfile
context: ./api
dockerfile: Dockerfile
ports:
- "127.0.0.1:44444:44444"
labels:
Expand All @@ -39,20 +39,24 @@ services:
- "traefik.http.services.api.loadbalancer.server.port=44444"
- "traefik.http.middlewares.api-prefix.stripprefix.prefixes=/api"
- "traefik.http.routers.api.middlewares=api-prefix"
volumes:
- ./config.js:/sqtracker/config.js
depends_on:
- database
- tracker
client:
container_name: sq_client
build:
context: .
dockerfile: ./client/Dockerfile
context: ./client
dockerfile: Dockerfile
ports:
- "127.0.0.1:44445:44445"
labels:
- "traefik.enable=true"
- "traefik.http.routers.client.entrypoints=web"
- "traefik.http.routers.client.rule=PathPrefix(`/`)"
- "traefik.http.services.client.loadbalancer.server.port=44445"
volumes:
- ./config.js:/sqtracker/config.js
depends_on:
- api
- api

0 comments on commit 586fe5d

Please sign in to comment.