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

Fix/wiki#63 #77

Closed
wants to merge 5 commits into from
Closed
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
2 changes: 1 addition & 1 deletion api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:16
FROM node:18.18.0
ENV NODE_ENV=production
ENV SENTRY_DSN="https://[email protected]/4504645996576768"
LABEL org.opencontainers.image.source=https://github.com/tdjsnelling/sqtracker
Expand Down
46 changes: 45 additions & 1 deletion api/src/controllers/wiki.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,51 @@ export const getWiki = async (req, res, next) => {
next(e);
}
};

export const getWikis = async (req, res, next) => {
try {
const result = await Wiki.aggregate([
{
$lookup: {
from: "users",
as: "createdBy",
let: { userId: "$createdBy" },
pipeline: [
{
$match: { $expr: { $eq: ["$_id", "$$userId"] } },
},
{
$project: {
username: 1,
},
},
],
},
},
{
$unwind: {
path: "$createdBy",
preserveNullAndEmptyArrays: true,
},
},
]);
if (!result || result.length === 0) {
res.status(404).send("No wikis found");
return;
}
let page = result[0];
if (process.env.SQ_ALLOW_UNREGISTERED_VIEW && !req.userId && !page.public) {
page = null;
}
const query = {};
if (process.env.SQ_ALLOW_UNREGISTERED_VIEW && !req.userId) {
query.public = true;
}
const allPages = await Wiki.find(query, { slug: 1, title: 1 }).lean();
res.json({ page, allPages });
} catch (e) {
next(e);
}
};
export const deleteWiki = async (req, res, next) => {
try {
if (req.userRole !== "admin") {
Expand Down
2 changes: 2 additions & 0 deletions api/src/routes/wiki.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import express from "express";
import {
createWiki,
getWiki,
getWikis,
deleteWiki,
updateWiki,
} from "../controllers/wiki";
Expand All @@ -11,6 +12,7 @@ const router = express.Router();
export default () => {
router.post("/new", createWiki);
router.post("/update/:wikiId", updateWiki);
router.get("/", getWikis);
router.get("*", getWiki);
router.delete("*", deleteWiki);
return router;
Expand Down
8 changes: 5 additions & 3 deletions api/src/utils/validateConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const configSchema = yup
.string()
.oneOf(["open", "invite", "closed"])
.required(),
SQ_DISABLE_EMAIL: yup.boolean().required(),
SQ_ALLOW_ANONYMOUS_UPLOADS: yup.boolean().required(),
SQ_MINIMUM_RATIO: yup.number().min(-1).required(),
SQ_MAXIMUM_HIT_N_RUNS: yup.number().integer().min(-1).required(),
Expand All @@ -22,6 +23,7 @@ const configSchema = yup
SQ_BP_COST_PER_INVITE: yup.number().min(0).required(),
SQ_BP_COST_PER_GB: yup.number().min(0).required(),
SQ_SITE_WIDE_FREELEECH: yup.boolean().required(),

SQ_TORRENT_CATEGORIES: yup.lazy((value) => {
const entries = Object.keys(value).reduce((obj, key) => {
obj[key] = yup
Expand Down Expand Up @@ -51,12 +53,12 @@ const configSchema = yup
}),
SQ_EXTENSION_BLACKLIST: yup.array().of(yup.string()).min(0),
SQ_SITE_DEFAULT_LOCALE: yup
.string()
.oneOf(["en", "es", "it", "ru", "de", "zh", "eo", "fr"]),
.string()
.oneOf(["en", "es", "it", "ru", "de", "zh", "eo", "fr"])
.required(),
SQ_BASE_URL: yup.string().matches(httpRegex).required(),
SQ_API_URL: yup.string().matches(httpRegex).required(),
SQ_MONGO_URL: yup.string().matches(mongoRegex).required(),
SQ_DISABLE_EMAIL: yup.boolean(),
SQ_MAIL_FROM_ADDRESS: yup
.string()
.email()
Expand Down
4 changes: 2 additions & 2 deletions client/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:16 AS builder
FROM node:18.18.0 AS builder
ARG SENTRY_AUTH_TOKEN
ENV NODE_ENV=production
ENV SENTRY_ORG=tdjsnelling
Expand All @@ -10,7 +10,7 @@ RUN yarn install
RUN echo "{}" > /config.js
RUN ./node_modules/.bin/next build

FROM node:16-alpine
FROM node:18.18.0
ENV NODE_ENV=production
ENV SENTRY_DSN="https://[email protected]/4504646040616960"
LABEL org.opencontainers.image.source=https://github.com/tdjsnelling/sqtracker
Expand Down
49 changes: 33 additions & 16 deletions client/pages/wiki/[[...slug]].js
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,18 @@ const Wiki = ({ page, allPages, token, userRole, slug }) => {
</form>
)}
</>
) : allPages.length > 0 ? (
<>
{allPages.map((p) => (
<Link key={`page-${p.slug}`} href={`/wiki${p.slug}`} passHref>
<Text as="a" display="block">
{p.title}
</Text>
</Link>
))}
</>
) : (
<Text color="grey">{getLocaleString("wikiThereNothingHereYet")}</Text>
<Text>{getLocaleString("wikiThereNothingHereYet")}</Text>
)}
{showDeleteModal && (
<Modal close={() => setShowDeleteModal(false)}>
Expand All @@ -272,7 +282,6 @@ const Wiki = ({ page, allPages, token, userRole, slug }) => {
export const getServerSideProps = withAuthServerSideProps(
async ({ token, fetchHeaders, isPublicAccess, query: { slug } }) => {
if (!token && !isPublicAccess) return { props: {} };

const parsedSlug = slug?.length ? slug.join("/") : "";

const {
Expand All @@ -281,21 +290,29 @@ export const getServerSideProps = withAuthServerSideProps(
} = getConfig();

const { role } = token ? jwt.verify(token, SQ_JWT_SECRET) : { role: null };

try {
const wikiRes = await fetch(`${SQ_API_URL}/wiki/${parsedSlug}`, {
headers: fetchHeaders,
});
if (
wikiRes.status === 403 &&
(await wikiRes.text()) === "User is banned"
) {
throw "banned";
}
const { page, allPages } = await wikiRes.json();
return {
props: { page, allPages, token, userRole: role, slug: parsedSlug },
};
let page, allPages;
if (parsedSlug.length === 0) {
const wikiRes = await fetch(`${SQ_API_URL}/wiki`, {
headers: fetchHeaders,
});
({ allPages } = await wikiRes.json());

return {
props: {allPages, token, userRole: role, slug: parsedSlug, },
};
} else {
const wikiRes = await fetch(`${SQ_API_URL}/wiki/${parsedSlug}`, {
headers: fetchHeaders,
});
if (wikiRes.status === 403 && (await wikiRes.text()) === "User is banned") {
throw "banned";
}
({ page, allPages } = await wikiRes.json());
return {
props: { page, allPages, token, userRole: role, slug: parsedSlug },
};
}
} catch (e) {
if (e === "banned") throw "banned";
return { props: { token, userRole: role, slug: parsedSlug } };
Expand Down
2 changes: 1 addition & 1 deletion config.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ module.exports = {

// Disables sending of any emails and removes the need for an SMTP server.
// Fine for testing, not recommended in production as users will not be able to reset their passwords.
SQ_DISABLE_EMAIL: false,
SQ_DISABLE_EMAIL: true,

// The email address that mail will be sent from.
// Not required if SQ_DISABLE_EMAIL=true.
Expand Down
64 changes: 32 additions & 32 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
version: "3.9"
services:
traefik:
image: "traefik:v2.5"
container_name: "sq_traefik"
command:
- "--api.insecure=true"
- "--providers.file=true"
- "--providers.file.filename=/config/traefik.yml"
- "--entrypoints.webinsecure.address=:80"
- "--entrypoints.web.address=:443"
- "--entryPoints.web.proxyProtocol.insecure"
- "--entryPoints.web.forwardedHeaders.insecure"
- "[email protected]"
- "--certificatesresolvers.tlsresolver.acme.storage=/letsencrypt/acme.json"
- "--certificatesresolvers.tlsresolver.acme.httpchallenge=true"
- "--certificatesresolvers.tlsresolver.acme.httpchallenge.entrypoint=webinsecure"
ports:
- "80:80"
- "443:443"
- "8080:8080"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
- ./letsencrypt:/letsencrypt
- ./traefik.yml:/config/traefik.yml
# nginx:
# image: "nginx:latest"
# container_name: "sq_nginx"
# restart: always
# traefik:
# image: "traefik:v2.5"
# container_name: "sq_traefik"
# command:
# - "--api.insecure=true"
# - "--providers.file=true"
# - "--providers.file.filename=/config/traefik.yml"
# - "--entrypoints.webinsecure.address=:80"
# - "--entrypoints.web.address=:443"
# - "--entryPoints.web.proxyProtocol.insecure"
# - "--entryPoints.web.forwardedHeaders.insecure"
# - "[email protected]"
# - "--certificatesresolvers.tlsresolver.acme.storage=/letsencrypt/acme.json"
# - "--certificatesresolvers.tlsresolver.acme.httpchallenge=true"
# - "--certificatesresolvers.tlsresolver.acme.httpchallenge.entrypoint=webinsecure"
# ports:
# - "80:80"
# - "443:443"
# - "8080:8080"
# volumes:
# - ./nginx.conf:/etc/nginx/nginx.conf
# - "/var/run/docker.sock:/var/run/docker.sock:ro"
# - ./letsencrypt:/letsencrypt
# - ./traefik.yml:/config/traefik.yml
nginx:
image: "nginx:latest"
container_name: "sq_nginx"
restart: always
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
database:
container_name: sq_mongodb
image: mongo:6.0
Expand All @@ -40,9 +40,9 @@ services:
- ./data:/data/db
api:
container_name: sq_api
image: ghcr.io/tdjsnelling/sqtracker-api:latest
image: klaiment_api:latest
ports:
- "127.0.0.1:3001:3001"
- "3001:3001"
volumes:
- type: bind
source: ./config.js
Expand All @@ -51,9 +51,9 @@ services:
- database
client:
container_name: sq_client
image: ghcr.io/tdjsnelling/sqtracker-client:latest
image: klaiment:latest
ports:
- "127.0.0.1:3000:3000"
- "3000:3000"
volumes:
- type: bind
source: ./config.js
Expand Down
8 changes: 8 additions & 0 deletions nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ http {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
location /app {
proxy_pass http://sq_api:3001;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}

location /api/ {
rewrite /api/(.*) /$1 break;
Expand Down
Loading