Adding docker compose to test api with mongodb

This commit is contained in:
Sugui 2023-12-25 07:47:16 +01:00
parent cfae285d44
commit 88cdce819b
8 changed files with 144 additions and 15 deletions

16
.dockerignore Normal file
View File

@ -0,0 +1,16 @@
compose.yaml
node_modules
Dockerfile*
docker-compose*
.dockerignore
.git
.gitignore
README.md
LICENSE
.vscode
Makefile
helm-charts
.env
.editorconfig
.idea
coverage*

View File

@ -36,5 +36,5 @@ COPY --from=prerelease /usr/src/app/package.json .
# run the app
USER bun
EXPOSE 3000/tcp
CMD ["bun", "run", "index.ts"]
EXPOSE 8080/tcp
CMD ["bun", "run", "start"]

34
compose.yaml Normal file
View File

@ -0,0 +1,34 @@
version: '3'
services:
mongodb:
image: mongo
container_name: mongodb
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: password
MONGO_INITDB_DATABASE: bot
volumes:
- mongodb_data:/data/db
- ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
bot-api:
image: oven/bun:1
container_name: bot-api
command: bun run dev
working_dir: /usr/src/app
ports:
- "8080:8080"
depends_on:
- mongodb
environment:
MONGODB_URI: "mongodb://mongodb:27017/bot"
MONGODB_USER: "root"
MONGODB_PASS: "password"
volumes:
- ./:/usr/src/app:ro
volumes:
mongodb_data:

View File

@ -1,13 +0,0 @@
import express from "express";
const app = express();
// Hello World GET endpoint
app.get("/", (_, res) => {
res.send("Hello World!");
});
// Set the default port to 3000, or use the PORT environment variable
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Express server listening on port ${port}`));

24
mongo-init.js Normal file
View File

@ -0,0 +1,24 @@
db.createUser({
user: 'root',
pwd: 'password',
roles: [
{
role: 'readWrite',
db: 'admin',
},
{
role: 'readWrite',
db: 'bot',
},
],
});
db = new Mongo().getDB("bot");
db.images.createIndex({ "status": 1 });
db.images.createIndex({ "image": 1 }, { "unique": true });
db.images.insert({
url: "https://example.com",
status: "consumed",
tags: ["2girls", "sleeping"]
});

View File

@ -7,6 +7,9 @@
},
"peerDependencies": {
"typescript": "^5.0.0"
},"scripts": {
"start": "bun run src/index.ts",
"dev": "bun --hot run src/index.ts"
},
"dependencies": {
"@types/express": "^4.17.21",

20
src/ImageModel.ts Normal file
View File

@ -0,0 +1,20 @@
import mongoose from "mongoose";
const ImageSchema = new mongoose.Schema({
url: {
type: String,
required: true
},
status: {
type: String,
enum: {
values: ["consumed", "unavailable", "available"],
},
required: true
},
tags: {
type: [String]
}
});
export default mongoose.model('images', ImageSchema);

45
src/index.ts Normal file
View File

@ -0,0 +1,45 @@
import express from "express";
import mongoose from "mongoose";
import ImageModel from "./ImageModel";
const app = express();
app.use(express.json());
// Hello World GET endpoint
app.get("/", (_, res) => {
res.json({ message: "Blazing fast 🚀" });
});
app.get("/images", async (req, res) => {
try {
const allImages = await ImageModel.find();
res.json(allImages);
} catch (error) {
res.status(500).json({ message: error });
}
})
// Set the default port to 8080, or use the PORT environment variable
const start = async () => {
const port = process.env.PORT || 8080;
const mongo_uri: string = process.env.MONGODB_URI || "";
const mongo_user = process.env.MONGODB_USER;
const mongo_pass = process.env.MONGODB_PASS;
try {
await mongoose.connect(mongo_uri, {
authSource: "admin",
user: mongo_user,
pass: mongo_pass
});
app.listen(port, () => console.log(`Express server listening on port ${port}`));
} catch (error) {
console.error(error);
process.exit(1);
}
}
start();