Skip to content

Commit

Permalink
Add auto-server-restart after error, paste webhook payload log
Browse files Browse the repository at this point in the history
  • Loading branch information
yash22arora committed May 28, 2024
1 parent 11b6253 commit a66f482
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 13 deletions.
35 changes: 35 additions & 0 deletions server/src/api/routes/plan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,41 @@ router.post("/webhook", (req: Request, res: Response) => {
if (req.body.event === "subscription.charged") {
const subscriptionEntity = req.body.payload.subscription.entity;
console.log({ ...subscriptionEntity });

// log:
// {
// id: 'sub_OG2chJoM9DRdum',
// entity: 'subscription',
// plan_id: 'plan_OEj5fUJpj4yfEs',
// customer_id: 'cust_OG1uBmOH673KZI',
// status: 'active',
// current_start: 1716927176,
// current_end: 1719599400,
// ended_at: null,
// quantity: 1,
// notes: {
// ownerID: '664bb289832407580571bf7b',
// domainID: '663be1d6162c5e1e2fa35985',
// planLabel: 'hbd1.663be1d6162c5e1e2fa35985',
// planType: 'vercel'
// },
// charge_at: 1719599400,
// start_at: 1716927176,
// end_at: 1745865000,
// auth_attempts: 0,
// total_count: 12,
// paid_count: 1,
// customer_notify: true,
// created_at: 1716927160,
// expire_by: null,
// short_url: null,
// has_scheduled_changes: false,
// change_scheduled_at: null,
// source: 'api',
// payment_method: 'card',
// offer_id: null,
// remaining_count: 11
// }
}
res.status(200).send("ok");
});
Expand Down
8 changes: 0 additions & 8 deletions server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ import {
} from "./api/routes";
import { CustomError } from "./utils/types";

mongoose
.connect(
`mongodb+srv://${process.env.MONGODB_USERNAME}:${process.env.MONGODB_PASSWD}@${process.env.MONGODB_CLUSTER}.mongodb.net/?retryWrites=true&w=majority`
)
.then(() => {
console.log("Connected to database 🚀");
});

const app = express();

app.use(morgan("dev")); // middleware for logging requests
Expand Down
31 changes: 26 additions & 5 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,34 @@
import http from "http";
import app from "./app";
import dotenv from "dotenv";
import mongoose, { set } from "mongoose";

dotenv.config();

const port = Number(process.env.PORT) || 5000;

const server = http.createServer(app);

server.listen(port, "0.0.0.0", 100, () => {
console.log(`Server running on port ${port}`);
});
async function startServer() {
try {
await mongoose
.connect(
`mongodb+srv://${process.env.MONGODB_USERNAME}:${process.env.MONGODB_PASSWD}@${process.env.MONGODB_CLUSTER}.mongodb.net/?retryWrites=true&w=majority`
)
.then(() => {
console.log("Connected to database 🚀");
});

server.listen(port, "0.0.0.0", 100, () => {
console.log(`Server running on port ${port}`);
});
} catch (err) {
console.log(err);
setTimeout(startServer, 1500);
}

mongoose.connection.on("error", (err) => {
console.error("MongoDB Error", err);
process.exit(1);
});
}

startServer();

0 comments on commit a66f482

Please sign in to comment.