Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/B2Bitcoin/B2BitcoinBootik i…
Browse files Browse the repository at this point in the history
…nto increment-challenge-score
  • Loading branch information
ithiame committed Jun 16, 2023
2 parents 16f5084 + 1df658b commit e37cec8
Show file tree
Hide file tree
Showing 13 changed files with 293 additions and 185 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ Add `.env.local` or `.env.{development,test,production}.local` files for secrets
- `S3_KEY_SECRET` - Credentials for the S3-compatible object storage
- `S3_REGION` - Region from the S3-compatible object storage

## Production

### Running

```shell
pnpm run build
node build/index.js
```

You can set the `PORT` environment variable to change from the default 3000 port to another port.

You can also use [pm2](https://pm2.keymetrics.io/docs/usage/quick-start/) to manage your node application, and run it on multiple cores.

### Maintenance mode

It's possible to enable maintenance mode in the admin.

To correctly recognize your IP, if you are behind a reverse proxy like nginx, you will need to set the `ADDRESS_HEADER` to `X-Forwaded-For` and the `XFF_DEPTH` header to `1` in the environment.

## Local development

### Running the app
Expand Down
4 changes: 2 additions & 2 deletions src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width" />
%sveltekit.head%
</head>
<body data-sveltekit-preload-data="hover">
<div style="display: contents">%sveltekit.body%</div>
<body>
%sveltekit.body%
</body>
</html>
20 changes: 20 additions & 0 deletions src/error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>%sveltekit.error.message%</title>
</head>
<body
style="
text-align: center;
display: flex;
gap: 3rem;
flex-direction: column;
align-items: center;
"
>
<!--<p>Status: %sveltekit.status%</p>-->
<span style="font-weight: 500; margin-top: 4rem">Message: %sveltekit.error.message%</span>
<img style="max-width: 500px" src="/logo" alt="Logo" />
</body>
</html>
23 changes: 17 additions & 6 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { ZodError } from 'zod';
import type { HandleServerError, Handle } from '@sveltejs/kit';
import { type HandleServerError, type Handle, error } from '@sveltejs/kit';
import { collections } from '$lib/server/database';
import { ObjectId } from 'mongodb';
import { addYears } from 'date-fns';

import '$lib/server/locks';
import { ADMIN_LOGIN, ADMIN_PASSWORD } from '$env/static/private';
import { runtimeConfig } from '$lib/server/runtime-config';

export const handleError = (({ error, event }) => {
console.error('handleError', error);
Expand Down Expand Up @@ -43,11 +44,8 @@ export const handleError = (({ error, event }) => {
}) satisfies HandleServerError;

export const handle = (async ({ event, resolve }) => {
if (
(event.url.pathname.startsWith('/admin/') || event.url.pathname === '/admin') &&
ADMIN_LOGIN &&
ADMIN_PASSWORD
) {
const isAdminUrl = event.url.pathname.startsWith('/admin/') || event.url.pathname === '/admin';
if (isAdminUrl && ADMIN_LOGIN && ADMIN_PASSWORD) {
const authorization = event.request.headers.get('authorization');

if (!authorization?.startsWith('Basic ')) {
Expand All @@ -73,6 +71,19 @@ export const handle = (async ({ event, resolve }) => {
}
}

if (
runtimeConfig.isMaintenance &&
!isAdminUrl &&
event.url.pathname !== '/logo' &&
!event.url.pathname.startsWith('/picture/raw/') &&
!runtimeConfig.maintenanceIps.split(',').includes(event.getClientAddress())
) {
if (event.request.method !== 'GET') {
throw error(405, 'Site is in maintenance mode. Please try again later.');
}
throw error(503, 'Site is in maintenance mode. Please try again later.');
}

const token = event.cookies.get('bootik-session');

event.locals.sessionId = token || crypto.randomUUID();
Expand Down
2 changes: 2 additions & 0 deletions src/lib/server/runtime-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const defaultConfig = {
BTC_EUR: 30_000,
orderNumber: 0,
subscriptionNumber: 0,
isMaintenance: false,
maintenanceIps: '',
brandName: 'My Space',
subscriptionDuration: 'month' as 'month' | 'day' | 'hour',
subscriptionReminderSeconds: 24 * 60 * 60,
Expand Down
20 changes: 14 additions & 6 deletions src/lib/types/Challenge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@ import type { Timestamps } from './Timestamps';
export interface Challenge extends Timestamps {
_id: string;
name: string;

/* If empty, works on all products */
productId: string[];
productIds: string[];

goal: {
currency: Currency;
currency?: Currency;
amount: number;
};

progress: {
currency: Currency;
amount: number;
};
/**
* totalProducts: The goal is to sell a certain number of products
*/
mode: 'totalProducts' | 'moneyAmount';

recurring: false | 'monthly';

progress: number;

beginsAt?: Date;
endsAt: Date;
}
2 changes: 2 additions & 0 deletions src/lib/types/Picture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ export interface Picture extends Timestamps {
formats: ImageData[];
};
}

export const DEFAULT_LOGO = 'https://coyo.dev/icons/logo.png';
1 change: 1 addition & 0 deletions src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export async function load({ depends, locals }) {
: null;

return {
isMaintenance: runtimeConfig.isMaintenance,
exchangeRate: runtimeConfig.BTC_EUR,
brandName: runtimeConfig.brandName,
logoPicture,
Expand Down
Loading

0 comments on commit e37cec8

Please sign in to comment.