Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
WilcoApp committed Sep 24, 2024
1 parent 724cbb0 commit c6d94c3
Show file tree
Hide file tree
Showing 117 changed files with 20,167 additions and 46,370 deletions.
13 changes: 3 additions & 10 deletions .devcontainer/setup.sh
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
WILCO_ID="`cat .wilco`"
ENGINE_EVENT_ENDPOINT="${ENGINE_BASE_URL}/users/${WILCO_ID}/event"
ENGINE_WILCO_AI_CONFIG="${ENGINE_BASE_URL}/users/${WILCO_ID}/wilcoAiConfig"
CODESPACE_BACKEND_HOST=$(curl -s "${ENGINE_BASE_URL}/api/v1/codespace/backendHost?codespaceName=${CODESPACE_NAME}&portForwarding=${GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}" | jq -r '.codespaceBackendHost')
CODESPACE_BACKEND_URL="https://${CODESPACE_BACKEND_HOST}"
export ENGINE_EVENT_ENDPOINT="${ENGINE_BASE_URL}/users/${WILCO_ID}/event"

# Update engine that codespace started for user
curl -L -X POST "${ENGINE_EVENT_ENDPOINT}" -H "Content-Type: application/json" --data-raw "{ \"event\": \"github_codespace_started\" }"

# Export backend envs when in codespaces
echo "export CODESPACE_BACKEND_HOST=\"${CODESPACE_BACKEND_HOST}\"" >> ~/.bashrc
echo "export CODESPACE_BACKEND_URL=\"${CODESPACE_BACKEND_URL}\"" >> ~/.bashrc
echo "export ENGINE_WILCO_AI_URL=\"${ENGINE_WILCO_AI_CONFIG}\"" >> ~/.bashrc
echo "export CODESPACE_WDS_SOCKET_PORT=443" >> ~/.bashrc

# Install the Atlas CLI
curl -fsSL https://pgp.mongodb.com/server-7.0.asc | \
sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/mongodb-server-7.0.gpg &&
echo "deb [ arch=amd64,arm64 signed=/etc/apt/trusted.gpg.d/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list &&
sudo apt-get update &&
sudo apt-get install -y mongodb-atlas-cli

# Export welcome prompt in bash:
echo "printf \"\n\n☁️☁️☁️️ Anythink: Develop in the Cloud ☁️☁️☁️\n\"" >> ~/.bashrc
echo "printf \"\n\x1b[31m \x1b[1m👉 Type: \\\`docker compose up\\\` to run the project. 👈\n\n\"" >> ~/.bashrc

nohup bash -c "cd /wilco-agent && node agent.js &" >> /tmp/agent.log 2>&1
34 changes: 0 additions & 34 deletions backend/.eslintrc.js

This file was deleted.

4 changes: 0 additions & 4 deletions backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,3 @@ node_modules
.node_repl_history

.idea

dist

.env
4 changes: 0 additions & 4 deletions backend/.prettierignore

This file was deleted.

9 changes: 1 addition & 8 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
FROM node:18

RUN curl https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh --output /wait-for-it.sh

RUN chmod +x /wait-for-it.sh

WORKDIR /usr/src/backend
RUN yarn install
FROM public.ecr.aws/v0a2l7y2/wilco/anythink-backend-node:latest
10 changes: 10 additions & 0 deletions backend/Dockerfile.aws
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM node:16

WORKDIR /usr/src
COPY backend ./backend
COPY .wilco ./.wilco

# Pre-install npm packages
WORKDIR /usr/src/backend
RUN yarn install

23 changes: 21 additions & 2 deletions backend/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
# Mongo Chat Server
# Anythink Market Backend

Based on https://mongodb.github.io/chatbot/server/configure
The Anythink Market backend is Node web app written with [Express](https://expressjs.com/)

## Dependencies

- [jsonwebtoken](https://github.com/auth0/node-jsonwebtoken) - For generating JWTs used by authentication
- [mongoose](https://github.com/Automattic/mongoose) - For modeling and mapping MongoDB data to javascript
- [mongoose-unique-validator](https://github.com/blakehaswell/mongoose-unique-validator) - For handling unique validation errors in Mongoose. Mongoose only handles validation at the document level, so a unique index across a collection will throw an exception at the driver level. The `mongoose-unique-validator` plugin helps us by formatting the error like a normal mongoose `ValidationError`.
- [passport](https://github.com/jaredhanson/passport) - For handling user authentication
- [slug](https://github.com/dodo/node-slug) - For encoding titles into a URL-friendly format

## Application Structure

- `app.js` - The entry point to our application. This file defines our express server and connects it to MongoDB using mongoose. It also requires the routes and models we'll be using in the application.
- `config/` - This folder contains configuration for passport as well as a central location for configuration/environment variables.
- `routes/` - This folder contains the route definitions for our API.
- `models/` - This folder contains the schema definitions for our Mongoose models.

## Error Handling

In `routes/api/index.js`, we define a error-handling middleware for handling Mongoose's `ValidationError`. This middleware will respond with a 422 status code and format the response to have [error messages the clients can understand](https://github.com/gothinkster/realworld/blob/master/API.md#errors-and-status-codes)
89 changes: 89 additions & 0 deletions backend/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
require("dotenv").config();
var http = require("http"),
path = require("path"),
methods = require("methods"),
express = require("express"),
bodyParser = require("body-parser"),
session = require("express-session"),
cors = require("cors"),
passport = require("passport"),
errorhandler = require("errorhandler"),
mongoose = require("mongoose");

var isProduction = process.env.NODE_ENV === "production";

// Create global app object
var app = express();

app.use(cors());

// Normal express config defaults
app.use(require("morgan")("dev"));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.use(require("method-override")());
app.use(express.static(__dirname + "/public"));

app.use(
session({
secret: "secret",
cookie: { maxAge: 60000 },
resave: false,
saveUninitialized: false
})
);

if (!isProduction) {
app.use(errorhandler());
}

if (!process.env.MONGODB_URI) {
console.warn("Missing MONGODB_URI in env, please add it to your .env file");
}

mongoose.connect(process.env.MONGODB_URI);
if (isProduction) {
} else {
mongoose.set("debug", true);
}

require("./models/User");
require("./models/Item");
require("./models/Comment");
require("./config/passport");

app.use(require("./routes"));

/// catch 404 and forward to error handler
app.use(function (req, res, next) {
if (req.url === "/favicon.ico") {
res.writeHead(200, { "Content-Type": "image/x-icon" });
res.end();
} else {
const err = new Error("Not Found");
err.status = 404;
next(err);
}
});

/// error handler
app.use(function(err, req, res, next) {
console.log(err.stack);
if (isProduction) {
res.sendStatus(err.status || 500)
} else {
res.status(err.status || 500);
res.json({
errors: {
message: err.message,
error: err
}
});
}
});

// finally, let's start our server...
var server = app.listen(process.env.PORT || 3000, function() {
console.log("Listening on port " + server.address().port);
});
3 changes: 3 additions & 0 deletions backend/config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
secret: process.env.NODE_ENV === 'production' ? process.env.SECRET : 'secret'
};
18 changes: 18 additions & 0 deletions backend/config/passport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var mongoose = require('mongoose');
var User = mongoose.model('User');

passport.use(new LocalStrategy({
usernameField: 'user[email]',
passwordField: 'user[password]'
}, function(email, password, done) {
User.findOne({email: email}).then(function(user){
if(!user || !user.validPassword(password)){
return done(null, false, {errors: {'email or password': 'is invalid'}});
}

return done(null, user);
}).catch(done);
}));

25 changes: 25 additions & 0 deletions backend/lib/event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const axiosLib = require("axios");
const fs = require("fs");

const WILCO_ID = process.env.WILCO_ID || fs.readFileSync('../.wilco', 'utf8')
const baseURL = process.env.ENGINE_BASE_URL || "https://engine.wilco.gg"

const axios = axiosLib.create({
baseURL: baseURL,
headers: {
'Content-type': 'application/json',
},
});

async function sendEvent(event, metadata) {
try {
const result = await axios.post(`/users/${WILCO_ID}/event`, JSON.stringify({event, metadata}));
return result.data;
} catch (error) {
console.error(`failed to send event ${event} to Wilco engine`)
}
}

module.exports = {
sendEvent,
}
22 changes: 22 additions & 0 deletions backend/models/Comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var mongoose = require("mongoose");

var CommentSchema = new mongoose.Schema(
{
body: String,
seller: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
item: { type: mongoose.Schema.Types.ObjectId, ref: "Item" }
},
{ timestamps: true }
);

// Requires population of seller
CommentSchema.methods.toJSONFor = function(user) {
return {
id: this._id,
body: this.body,
createdAt: this.createdAt,
seller: this.seller.toProfileJSONFor(user)
};
};

mongoose.model("Comment", CommentSchema);
62 changes: 62 additions & 0 deletions backend/models/Item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
var mongoose = require("mongoose");
var uniqueValidator = require("mongoose-unique-validator");
var slug = require("slug");
var User = mongoose.model("User");

var ItemSchema = new mongoose.Schema(
{
slug: { type: String, lowercase: true, unique: true },
title: {type: String, required: [true, "can't be blank"]},
description: {type: String, required: [true, "can't be blank"]},
image: String,
favoritesCount: { type: Number, default: 0 },
comments: [{ type: mongoose.Schema.Types.ObjectId, ref: "Comment" }],
tagList: [{ type: String }],
seller: { type: mongoose.Schema.Types.ObjectId, ref: "User" }
},
{ timestamps: true }
);

ItemSchema.plugin(uniqueValidator, { message: "is already taken" });

ItemSchema.pre("validate", function(next) {
if (!this.slug) {
this.slugify();
}

next();
});

ItemSchema.methods.slugify = function() {
this.slug =
slug(this.title) +
"-" +
((Math.random() * Math.pow(36, 6)) | 0).toString(36);
};

ItemSchema.methods.updateFavoriteCount = function() {
var item = this;

return User.count({ favorites: { $in: [item._id] } }).then(function(count) {
item.favoritesCount = count;

return item.save();
});
};

ItemSchema.methods.toJSONFor = function(user) {
return {
slug: this.slug,
title: this.title,
description: this.description,
image: this.image,
createdAt: this.createdAt,
updatedAt: this.updatedAt,
tagList: this.tagList,
favorited: user ? user.isFavorite(this._id) : false,
favoritesCount: this.favoritesCount,
seller: this.seller.toProfileJSONFor(user)
};
};

mongoose.model("Item", ItemSchema);
Loading

0 comments on commit c6d94c3

Please sign in to comment.