Skip to content

Commit

Permalink
other changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jonkirathe committed Sep 5, 2024
1 parent a687403 commit 125b9a6
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 157 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ Thumbs.db

/dev
.vercel

# Local Netlify folder
.netlify
30 changes: 30 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 20 additions & 17 deletions netlify/functions/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,24 @@ export const handler = serverless(api);*/
import serverless from "serverless-http";
import jwt from 'jsonwebtoken';
import swaggerJsdoc from 'swagger-jsdoc';
import {swaggerOptions} from './swaggerConfig.js';
// import {swaggerOptions} from './swaggerConfig.js';
import morgan from 'morgan';
import cors from 'cors';
import swaggerUi from 'swagger-ui-express';
import express, {Router} from "express";
// import {io} from "socket.io-client";
import spec from '@netlify/open-api'


const api = express();

const router = Router();

const PORT = process.env.PORT || 3500;
api.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
// const PORT = process.env.PORT || 3600;
// api.listen(PORT, () => {
// console.log(`Server is running on http://localhost:${PORT}`);
// });


api.use(express.json());
api.use(morgan('combined', {
Expand All @@ -150,6 +153,9 @@ api.use(morgan('combined', {
// api.use(morgan('combined')); // Log all requests
api.use(cors()); // Enable CORS for all routes

// Serve static files from the "public" directory
api.use(express.static('public'));

const users = [
{ id: 1, email: 'user@example.com', password: 'password', role: 'user' },
];
Expand All @@ -164,8 +170,8 @@ const generateTokens = (user) => {
};

// Swagger setup
const specs = swaggerJsdoc(swaggerOptions);
api.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs));
// const specs = swaggerJsdoc(swaggerOptions);
router.use('/api-docs', swaggerUi.serve, swaggerUi.setup(spec));

/**
* @swagger
Expand All @@ -189,7 +195,7 @@ api.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs));
* 401:
* description: Invalid email or password
*/
api.post('/api/signin', (req, res) => {
router.post('/signin', (req, res) => {
const { email, password } = req.body;
const user = users.find((u) => u.email === email && u.password === password);

Expand Down Expand Up @@ -223,7 +229,7 @@ api.post('/api/signin', (req, res) => {
* 409:
* description: User already exists
*/
api.post('/api/signup', (req, res) => {
router.post('/signup', (req, res) => {
const { email, password } = req.body;
const userExists = users.some((u) => u.email === email);

Expand Down Expand Up @@ -252,7 +258,7 @@ api.post('/api/signup', (req, res) => {
* 404:
* description: User not found
*/
api.get('/api/user', (req, res) => {
router.get('/user', (req, res) => {
const token = req.headers.authorization.split(' ')[1];
try {
const decoded = jwt.verify(token, SECRET_KEY);
Expand Down Expand Up @@ -287,7 +293,7 @@ api.get('/api/user', (req, res) => {
* 401:
* description: Invalid refresh token
*/
api.post('/api/refresh', (req, res) => {
router.post('/refresh', (req, res) => {
const { refreshToken } = req.body;
try {
const decoded = jwt.verify(refreshToken, REFRESH_SECRET_KEY);
Expand All @@ -312,7 +318,7 @@ api.post('/api/refresh', (req, res) => {
* 200:
* description: Server is working
*/
api.get('/api/check', (req, res) => {
router.get('/check', (req, res) => {
res.status(200).json({ message: 'All working' });
});

Expand All @@ -325,13 +331,10 @@ api.get('/api/check', (req, res) => {
* 200:
* description: List of users retrieved successfully
*/
api.get('/api/users', (req, res) => {
router.get('/users', (req, res) => {
res.status(200).json({ users });
});

api.get("/hello", (req, res) =>
res.send("Hello World!"));

// api.use("/api/", router);
api.use("/api/", router);

export const handler = serverless(api);
15 changes: 2 additions & 13 deletions netlify/functions/swaggerConfig.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
// import path from 'path';
// import { fileURLToPath } from 'url';

// console.log('import.meta.url:', import.meta.url);

// const __filename = fileURLToPath(import.meta.url);
// const __dirname = path.dirname(__filename);

// console.log('__filename:', __filename);
// console.log('__dirname:', __dirname);

// swaggerConfig.js
export const swaggerOptions = {
swaggerDefinition: {
openapi: '3.0.0',
Expand Down Expand Up @@ -37,6 +27,5 @@ export const swaggerOptions = {
},
],
},
apis: ['./api.js'], // Path to the API docs
apis: ['./netlify/functions/api.js'],
};

132 changes: 10 additions & 122 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
{
"name": "mockit",
"version": "1.0.0",
"type": "module",
"main": "netlify/functions/api.js",
"type": "module",
"scripts": {
"start": "node netlify/functions/api.js",
"build": "npm install && npm run start"
},
"dependencies": {
"@netlify/functions": "^2.8.1",
"@netlify/open-api": "^2.34.0",
"@types/express": "^4.17.21",
"axios": "^1.7.7",
"axios-mock-adapter": "^1.19.0",
"cors": "^2.8.5",
"express": "^4.19.2",
"jsonwebtoken": "^9.0.2",
Expand Down
Loading

0 comments on commit 125b9a6

Please sign in to comment.