Skip to content

Commit e5425dd

Browse files
committed
Configured eslint for Next.js; Removed login functionality from API
1 parent 1ba9dcd commit e5425dd

File tree

25 files changed

+161
-449
lines changed

25 files changed

+161
-449
lines changed

README.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,31 @@ Hence, we need to enable it on our own. To do that, set the following environmen
4646
ENABLE_TRUST_PROXY=true
4747
```
4848

49-
## Creating a local user
49+
## Creating a User
5050

51-
In order to interact with the API, you need to have an API key. To create one, execute the following command.
51+
In order to interact with the service, you need to have a user. You can interact with the service using the user's API key.
52+
53+
To create one, set up the following variable in your `.env` file:
54+
55+
```sh
56+
57+
```
58+
59+
When the app starts for the very first time, a user with the provided email will be generated, and their subscription will be renewed for 10 years.
60+
61+
Additionally, an API key will be generated for the user and printed in the application logs. The log containing the API key will look something like the following:
5262

5363
```sh
54-
docker exec <container_id | container_name> node /app/apps/api/dist/src/scripts/create-local-user.js <email>
64+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
65+
@ API key: testcktI8Sa71QUgYtest @
66+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
5567
```
5668

57-
After running the above command, you will get an API key which you can use to interact with the app.
69+
> CAUTION: Keep the generated API key confidential, as anyone could use it to store files on your instance.
5870
59-
You can only run this script once.
71+
## API documentation
6072

61-
> CAUTION: Keep the generated API key confidential as anyone could be able to store files on your instance.
73+
In order to interact with the service, you have to use the REST API. Our official Postman collection is available [here](https://www.postman.com/dark-rocket-625879/codelit/collection/5b8hfkr/medialit).
6274

6375
## Development
6476

apps/api/package.json

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,25 @@
3838
"@medialit/models": "workspace:*",
3939
"@medialit/thumbnail": "workspace:*",
4040
"@medialit/utils": "workspace:^0.1.0",
41-
"aws-sdk": "^2.1099.0",
41+
"aws-sdk": "^2.1692.0",
4242
"cors": "^2.8.5",
43-
"dotenv": "^16.0.0",
43+
"dotenv": "^16.4.7",
4444
"express": "^4.18.2",
4545
"express-fileupload": "^1.3.1",
4646
"joi": "^17.6.0",
47-
"jsonwebtoken": "^8.5.1",
4847
"mongoose": "^8.0.1",
49-
"nodemailer": "^6.7.2",
5048
"passport": "^0.5.2",
5149
"passport-jwt": "^4.0.0",
52-
"passport-magic-link": "^2.1.0",
53-
"pino": "^7.9.1",
54-
"pug": "^3.0.2"
50+
"pino": "^7.9.1"
5551
},
5652
"devDependencies": {
5753
"@types/cors": "^2.8.12",
5854
"@types/express": "^4.17.20",
5955
"@types/express-fileupload": "^1.2.2",
60-
"@types/jsonwebtoken": "^8.5.8",
6156
"@types/mongoose": "^5.11.97",
6257
"@types/node": "^20.8.7",
63-
"@types/nodemailer": "^6.4.4",
6458
"@types/passport": "^1.0.7",
6559
"@types/passport-jwt": "^3.0.6",
66-
"@types/pug": "^2.0.6",
6760
"@typescript-eslint/eslint-plugin": "^5.17.0",
6861
"@typescript-eslint/parser": "^5.17.0",
6962
"eslint": "^8.12.0",
File renamed without changes.

apps/api/src/index.ts

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,87 @@ loadDotFile();
44
import express from "express";
55
import connectToDatabase from "./config/db";
66
import passport from "passport";
7-
import userRoutes from "./user/routes";
8-
import jwt from "./user/passport-strategies/jwt";
9-
import apikeyRoutes from "./apikey/routes";
107
import mediaRoutes from "./media/routes";
118
import presignedUrlRoutes from "./presigning/routes";
129
import mediaSettingsRoutes from "./media-settings/routes";
1310
import logger from "./services/log";
11+
import { createUser, findByEmail } from "./user/queries";
12+
import { Apikey, User } from "@medialit/models";
13+
import { createApiKey } from "./apikey/queries";
14+
import { spawn } from "child_process";
1415

1516
connectToDatabase();
1617
const app = express();
1718

1819
app.set("trust proxy", process.env.ENABLE_TRUST_PROXY === "true");
1920

20-
passport.use(jwt);
21-
// passport.use(apikey);
22-
app.use(passport.initialize());
2321
app.use(express.json());
2422

25-
app.use("/user", userRoutes(passport));
2623
app.use("/settings/media", mediaSettingsRoutes(passport));
27-
app.use("/settings/apikey", apikeyRoutes(passport));
2824
app.use("/media/presigned", presignedUrlRoutes);
2925
app.use("/media", mediaRoutes);
3026

3127
const port = process.env.PORT || 80;
3228

33-
// TODO: Put checks for mandatory env vars
29+
if (process.env.EMAIL) {
30+
createAdminUser();
31+
}
3432

35-
app.listen(port, () => {
36-
logger.info(`Medialit server running at ${port}`);
33+
checkDependencies().then(() => {
34+
app.listen(port, () => {
35+
logger.info(`Medialit server running at ${port}`);
36+
});
3737
});
38+
39+
async function checkDependencies() {
40+
try {
41+
// Check ffmpeg
42+
await new Promise((resolve, reject) => {
43+
const ffmpeg = spawn("ffmpeg", ["-version"]);
44+
ffmpeg.on("error", () =>
45+
reject(new Error("ffmpeg is not installed")),
46+
);
47+
ffmpeg.on("exit", (code) => {
48+
if (code === 0) resolve(true);
49+
else reject(new Error("ffmpeg is not installed"));
50+
});
51+
});
52+
53+
// Check webp
54+
await new Promise((resolve, reject) => {
55+
const webp = spawn("cwebp", ["-version"]);
56+
webp.on("error", () => reject(new Error("webp is not installed")));
57+
webp.on("exit", (code) => {
58+
if (code === 0) resolve(true);
59+
else reject(new Error("webp is not installed"));
60+
});
61+
});
62+
} catch (error: any) {
63+
logger.error({ error: error.message });
64+
process.exit(1);
65+
}
66+
}
67+
68+
async function createAdminUser() {
69+
try {
70+
const email = process.env.EMAIL!.toLowerCase();
71+
const user: User | null = await findByEmail(email);
72+
73+
if (!user) {
74+
const user = await createUser(
75+
email,
76+
undefined,
77+
new Date(
78+
new Date().setFullYear(new Date().getFullYear() + 100),
79+
),
80+
"subscribed",
81+
);
82+
const apikey: Apikey = await createApiKey(user.id, "internal");
83+
console.log("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
84+
console.log(`@ API key: ${apikey.key} @`);
85+
console.log("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
86+
}
87+
} catch (error) {
88+
logger.error({ error }, "Failed to create admin user");
89+
}
90+
}

apps/api/src/scripts/create-local-user.ts

Lines changed: 0 additions & 36 deletions
This file was deleted.
File renamed without changes.

apps/api/src/user/queries.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { User } from "@medialit/models";
1+
import { SubscriptionStatus, User } from "@medialit/models";
22
import UserModel from "./model";
33

44
export async function getUser(id: string): Promise<User | null> {
@@ -11,9 +11,17 @@ export async function findByEmail(email: string): Promise<User | null> {
1111
});
1212
}
1313

14-
export async function createUser(email: string): Promise<User> {
14+
export async function createUser(
15+
email: string,
16+
name?: string,
17+
subscriptionEndsAfter?: Date,
18+
subscriptionStatus?: SubscriptionStatus,
19+
): Promise<User> {
1520
return await UserModel.create({
1621
email,
1722
active: true,
23+
name,
24+
subscriptionEndsAfter,
25+
subscriptionStatus,
1826
});
1927
}
File renamed without changes.

0 commit comments

Comments
 (0)