Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #1

Open
wants to merge 73 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
22ce80a
feat: update dependencies to latest
JoseAlbDR Oct 19, 2023
bf4585f
refactor: move global variables to config file
JoseAlbDR Oct 19, 2023
25edcaa
refactor: database logic into separate folder
JoseAlbDR Oct 19, 2023
8e6dc2f
refactor: authRoutes
JoseAlbDR Oct 19, 2023
4de3eb3
refactor: encryptPassword, getUsers, userExist into utils
JoseAlbDR Oct 19, 2023
a8eae89
refactor: authController from authRoutes
JoseAlbDR Oct 19, 2023
1e02e9f
feat: install eslint
JoseAlbDR Oct 19, 2023
2956bb9
refactor: create index for config variables
JoseAlbDR Oct 20, 2023
168aae6
refactor: verifyTokenMiddleware
JoseAlbDR Oct 20, 2023
d9f91a3
refactor: code refactor complete
JoseAlbDR Oct 20, 2023
597f980
feat: util to create files folder
JoseAlbDR Oct 20, 2023
beb9f2e
feat: util to create files folder
JoseAlbDR Oct 20, 2023
85b1e8c
fix: db and file folder paths
JoseAlbDR Oct 20, 2023
972c6ce
fix: upload route
JoseAlbDR Oct 20, 2023
b947a64
feat: use checkAuth and multer middlewares in upload route
JoseAlbDR Oct 20, 2023
5e3203b
refactor: rename checkAuth to checkAuthMiddleware
JoseAlbDR Oct 20, 2023
d2d9da3
feat: create user folder to upload files when a user is registered
JoseAlbDR Oct 20, 2023
42f88a5
feat: return file path in uploadController when a file is uploaded
JoseAlbDR Oct 20, 2023
82d5154
feat: create req.user object in checkAuthMiddleware
JoseAlbDR Oct 20, 2023
9568f96
refactor: rebuild multedMiddleware
JoseAlbDR Oct 20, 2023
c7f7dab
feat: end point to get infor about current logged in user
JoseAlbDR Oct 20, 2023
8f5d47c
Merge branch 'currentuser' into refactor
JoseAlbDR Oct 20, 2023
64419ae
feat: add notFoundMiddleware
JoseAlbDR Oct 20, 2023
5e1413c
feat: install swagger-ui-express and yamljs
JoseAlbDR Oct 20, 2023
a4555e5
feat: dinamically add port to swagger documentation server
JoseAlbDR Oct 20, 2023
d93635a
feat: remove js-yaml package
JoseAlbDR Oct 20, 2023
b7b3780
Update README.md
JoseAlbDR Oct 20, 2023
2c292c8
Update README.md
JoseAlbDR Oct 20, 2023
147c615
Update README.md
JoseAlbDR Oct 20, 2023
38b4e89
Update README.md
JoseAlbDR Oct 20, 2023
a66a4f4
Update README.md
JoseAlbDR Oct 20, 2023
c5314cc
fix: image relative path
JoseAlbDR Oct 20, 2023
14d088a
feat: fileValidationMiddleware to validate file upload
JoseAlbDR Oct 20, 2023
9397008
feat: use fileValidationMiddleware in uploadRouter
JoseAlbDR Oct 20, 2023
674b38d
Merge branch 'refactor' of github.com:JoseAlbDR/sparrest.js into refa…
JoseAlbDR Oct 20, 2023
635e740
feat: change route and documentation for upload
JoseAlbDR Oct 20, 2023
38b5cde
Update README.md
JoseAlbDR Oct 20, 2023
c6402d7
fix: problem with jsonServer and checkAuthMiddleware not working beca…
JoseAlbDR Oct 20, 2023
e40a372
Merge branch 'refactor' of github.com:JoseAlbDR/sparrest.js into refa…
JoseAlbDR Oct 20, 2023
08d347c
feat: export router in index and import en authController.2
JoseAlbDR Oct 20, 2023
a621ad0
fix: error with jsonServer router in authController, declare a new js…
JoseAlbDR Oct 20, 2023
50112c9
fix: error using diferent routers for users and rest of resources, ex…
JoseAlbDR Oct 20, 2023
6c693ef
fix: fileUpload url
JoseAlbDR Oct 23, 2023
a5cfbb1
fix: postman collection with correct routes
JoseAlbDR Oct 23, 2023
8a1578f
Update README.md
JoseAlbDR Oct 23, 2023
6d81077
Fix message for console.log (server running url)
jmacosta Oct 27, 2023
0067f9b
fix: multer fileUpload path with path.posix.join
JoseAlbDR Nov 3, 2023
064a157
Merge branch 'refactor' of github.com:JoseAlbDR/sparrest.js into refa…
JoseAlbDR Nov 3, 2023
7c3f65b
feat: new router for json server routes
JoseAlbDR Nov 3, 2023
8f61e83
refactor: move al json server router logic to jsonServerRoutes router…
JoseAlbDR Nov 3, 2023
1024247
feat: DB connection log
JoseAlbDR Nov 3, 2023
47db4a0
Merge branch 'refactor' into refactor
JoseAlbDR Nov 3, 2023
c8abcf6
Merge pull request #1 from jmacosta/refactor
JoseAlbDR Nov 3, 2023
25b53b1
fix: indentation in server logs
JoseAlbDR Nov 3, 2023
12f0efa
fix: dev script becaouse of nodemon restarting server in an infinite …
JoseAlbDR Nov 3, 2023
169fcde
fix: swagger documentation endpoint link
JoseAlbDR Nov 3, 2023
bdc182c
fix: remove Tweets from swagger.yaml tags
JoseAlbDR Nov 3, 2023
8536d02
fix: jsonServerRouter require in authController
JoseAlbDR Nov 3, 2023
46107b1
fix: error with jsonServerRouter not authenticating correctly, refact…
JoseAlbDR Nov 14, 2023
69c46b0
feat: app dockerized
JoseAlbDR Jan 15, 2024
dceefb7
Merge branch 'docker' into refactor
JoseAlbDR Jan 15, 2024
0ff8c6b
feat: add Docker instructions to README.md
JoseAlbDR Jan 15, 2024
6930bb8
feat: install env-var to manage environment variables
JoseAlbDR Jan 17, 2024
a0f4312
feat: update README.md
JoseAlbDR Jan 17, 2024
efbb73e
refactor: use env-var package to manage config/environment variables
JoseAlbDR Jan 17, 2024
1fb4d52
feat: remove unused variable
JoseAlbDR Jan 17, 2024
898ea99
fix: error with config import
JoseAlbDR Jan 17, 2024
3cefa10
feat: update README
JoseAlbDR Jan 17, 2024
86da06e
feat: guard clause in uploadController
JoseAlbDR Jan 17, 2024
e41b4db
fix: error with volumes bind
JoseAlbDR Jan 17, 2024
b043423
fix: error with notFoundMiddleware
JoseAlbDR Jan 17, 2024
8d1a4cd
refactor: multerMiddleware now returns multer({storage}) directly
JoseAlbDR Jan 17, 2024
c18ae2b
refactor: multerMiddleware now returns multer({storage}) directly
JoseAlbDR Jan 17, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
.dockerignore
.gitignore
.env.example
README.md
Sparrest.postman_collection.json
26 changes: 26 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = {
env: {
commonjs: true,
es2021: true,
node: true,
},
extends: 'standard',
overrides: [
{
env: {
node: true,
},
files: ['.eslintrc.{js,cjs}'],
parserOptions: {
sourceType: 'script',
},
},
],
parserOptions: {
ecmaVersion: 'latest',
},
rules: {
semi: [2, 'always'],
'comma-dangle': [2, 'only-multiline'],
},
};
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,6 @@ dist
.tern-port

db.json
dbbackup.json
public/*
!public/.gitkeep
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM node:21-alpine as prod-deps
WORKDIR /app
COPY package.json ./
RUN npm install --omit=dev

FROM node:21-alpine as prod
WORKDIR /app
COPY --from=prod-deps /app/node_modules ./node_modules
COPY . .
CMD ["npm", "start"]


102 changes: 96 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,105 @@
# Sparrest.js
# Sparrest.js 2.0

A [json-server](https://github.com/typicode/json-server) fork to enjoy developing frontend apps without a real backend, but with JWT auth.

## Setup

1. Create a `db.json` file with the entities of your DB
2. Run the server with `npm start`
3. Register a user with `POST /auth/register { username: "luke", password: "skywalker" }`
4. Login to obtain your JWT token: `POST /auth/login { username: "luke", password: "skywalker" }`
5. Start using `json-server` routes in `/api/<json-server routes>`. You'll need to auth every request by adding an HTTP header: `Authorization: Bearer <JWT token>`
1. Create a `/database/db.json` file with the entities of your DB

- Example with Tweets:
![dbjson](https://github.com/JoseAlbDR/sparrest.js/assets/128265706/36b90cd7-666c-48ba-b5e7-7c2d003b4762)

2. Create a .env file or rename the .env.example file provided to configure environment variables, if not set all variables will be created with default values as seen in `/config/index.js`file

- Example .env file:

```text
UPLOAD_FOLDER=../public/uploads
DB_FILE=../database/db.json
AUTH_READ=yes
AUTH_WRITE=no
SECRET_KEY=Annie is Vader
JWT_EXPIRATION=24h
PORT=8000
SALT=10
```

3. Install dependencies:

```
npm i
```

4. Run the server with `npm start`

5. Register a user with `POST /auth/register { username: "luke", password: "skywalker" }`

6. Login to obtain your JWT token: `POST /auth/login { username: "luke", password: "skywalker" }`

7. Start using `json-server` routes in `/api/<resource-name>`. You'll need to auth every request by adding an HTTP header: `Authorization: Bearer <JWT token>`

## Docker

1. Create a .env file or rename the .env.example file provided to configure environment variables.

2. Run the following command to run the app in the root directory

```
docker compose up
```

**NOTE**: folders database/db.json and public/uploads are bound to the container filesystem, any changes in the container will be reflected in the local folders but
if you want changes done in the local folders to be reflected in the container filesystem you have to run the following command

```
docker compose down
docker compose up
```

## Uploading files

You can upload files by making a multipart POST request with a file field (with file contents) to `/upload`.

## Documentation

- You can checout the API Documentation in `http://localhost:8000` and following the link provided or directly in `http://localhost:8000/docs`. Note that if you set a different port in .env or config files you will need to change the port accordingly.
- In order to unlock protected routes (shown with a open padlock) you must login, copy accessToken value, click in `Authorize` button and paste it.
- Documentation examples are done with a sample tweet Schema.

## Postman

- In the repo there is a file called `Sparrest.postman_collection.json` with a sample collenction wich you can directly import in Postmand and start using it right away.
- To import the collection follow the next steps:

1. Click in the top left Import button next to your workspace name:

![import](https://github.com/JoseAlbDR/sparrest.js/assets/128265706/72eea39e-7b2b-49b2-9edd-1e55daad498c)

2. Drop or select `Sparrest.postman_collection.json` and import it

- Note that the collection is made with a resourche called `tweets` you can change the resource name at any time to suit your needs.

## How to Use Postman Collection

1. Create a user:

- POST /auth/register
![register](https://github.com/JoseAlbDR/sparrest.js/assets/128265706/e70a6074-7da6-45b2-821e-7ab42f4e5981)

2. Login with same credentials:

- POST /auth/login
![login](https://github.com/JoseAlbDR/sparrest.js/assets/128265706/02c13071-b2ae-480f-8f96-3982cc797c07)

3. Copy given accessToken and set and Authorization => Type: Bearer Token in protected routes:

- POST /api/{resource}
- PATCH /api/{resource}
- DELETE /api/{resource}
- POST /upload
- GET /users/me
![auth](https://github.com/JoseAlbDR/sparrest.js/assets/128265706/f376f2c5-a22f-4931-ace4-358698fe8626)

- Optionally you can create a new Global/Environment variable, paste the token value inside and set it in all protected endpoints: Authorization => type: Bearer Token {{variableName}}

Change {resource} for your resource name, per example tweets or products and freely test the API with Postman.
Loading