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

test backend Alex #102

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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 .codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
coverage:
status:
default:
target: 90%
threshold: 3%
if_ci_failed: error
21 changes: 21 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
logs/
npm-debug.log
yarn-error.log
node_modules/
package-lock.json
yarn.lock
coverage/
dist/
.idea/
run/
.DS_Store
*.sw*
*.un~
.tsbuildinfo
.tsbuildinfo.*
docs
.docker/
.serverless
.git/
database/
template.yaml
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# 🎨 editorconfig.org

root = true

[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
insert_final_newline = true
46 changes: 46 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"extends": ["./node_modules/mwts/"],
"plugins": ["unicorn", "import"],
"ignorePatterns": [
"node_modules",
"dist",
"test",
"jest.config.js",
"typings"
],
"env": {
"jest": true
},
"rules": {
"node/no-extraneous-import": [
"error",
{
"allowModules": ["typeorm", "ioredis", "@midwayjs/core"]
}
],
"unicorn/filename-case": 2,
"import/order": [
1,
{
"groups": [
"builtin",
"external",
"internal",
"parent",
"sibling",
"index"
],
"pathGroups": [
{
"pattern": "@/**",
"group": "external",
"position": "after"
}
],
"pathGroupsExcludedImportTypes": ["builtin"],
"newlines-between": "always"
}
],
"no-console": [2, { "allow": ["warn", "error", "info"] } ]
}
}
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
logs/
npm-debug.log
yarn-error.log
node_modules/
database/
package-lock.json
yarn.lock
coverage/
dist/
.idea/
run/
.DS_Store
*.sw*
*.un~
.tsbuildinfo
.tsbuildinfo.*
docs
.docker/
.serverless
.fun
14 changes: 14 additions & 0 deletions .mocharc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

bail: true
full-trace: true
require:
- intelli-espower-loader
- espower-typescript/guess
- test/root.hooks.ts
spec:
- test/**/*.test.ts
timeout: 60000
ui: bdd
parallel: true
exit: true

26 changes: 26 additions & 0 deletions .nycrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"include": [
"src/*.ts",
"src/**/*.ts"
],
"exclude": [
".githooks",
"node_modules*",
"**/src/bin",
"**/src/domain.ts",
"**/src/interface.ts",
"**/dist",
"**/node_modules*",
"**/test",
"**/test*",
"**/*.d.ts",
"**/*.js"
],
"reporter": [
"html",
"json",
"text",
"text-summary"
],
"all": true
}
3 changes: 3 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
...require('mwts/.prettierrc.json')
}
19 changes: 19 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM node:14.17.3-buster-slim AS BUILD_IMAGE

WORKDIR /home

COPY . .

RUN npm install --registry=https://registry.npm.taobao.org && \
npm run build && \
npm prune --production

FROM node:14.17.3-alpine3.14

WORKDIR /home

COPY --from=BUILD_IMAGE /home /home

EXPOSE 9000

ENTRYPOINT ["node", "bootstrap.js"]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 FSD Node.js

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
154 changes: 99 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,73 +1,117 @@
# Backend Developer (Node.js) test

Make sure you read the whole document carefully and follow the guidelines in it.

## Context

Build a RESTful API that can `get/create/update/delete` user data from a persistence database

### User Model

# TEST Service

## Project guide
In this project, you'll see the following Midway based practice case (the upper layer uses egg.js)
Midway is based on TypeScript development and combines OOP + Class + IoC. midway is similar to nestjs in its design concept

The capabilities supported by Test Service
| ability | Name | progress |
| :------------- | -------------------------------------- | :---: |
| **overview** | | |
| | Controller | ✓ |
| | Services and Refilling | ✓ |
| | Request, response, and application | ✓ |
| | Web middleware | ✓ |
| | start and deploy | ✓ |
| **Basic ability** | | |
| | Dependency injection | ✓ |
| | Operating environment | ✓ |
| | Multi-environment configuration | ✓ |
| | Parameter checksum conversion | ✓ |
| | Life cycle | ✓ |
| | Component | ✓ |
| | Logger | ✓ |
| | Debug | ✓ |
| | Test | ✓ |
| **enhance** | | |
| | Cache (Redis) | ✓ |
| | Database(Typegoose) | ✓ |
| | MongoDB | ✓ |
| | Swagger | ✓ |
| **Web** | | |
| | CORS | ✓ |
| **microservice** | | |
| | RabbitMQ | ✓ |
| **Common ability** | | |
| | Token Authentication | ✓ |
| | Authentication middleware | ✓ |
| | Interface response statistics middleware| ✓ |
| | Unified error handling | ✓ |
| | SnowFlake | ✓ |
| | Jaeger | ✓ |



## Usage

The following environmental support is required to run the project
- Mongo
- Redis
- Jeager

Installation dependency by docker
```
{
"id": "xxx", // user ID
"name": "test", // user name
"dob": "", // date of birth
"address": "", // user address
"description": "", // user description
"createdAt": "" // user created date
}
docker-compose up -d
docker-compose down
```

## Requirements

### Functionality

- The API should follow typical RESTful API design pattern.
- The data should be saved in the DB.
- Provide proper unit test.
- Provide proper API document.

### Tech stack

- Use Node.js and any framework.
- Use any DB. NoSQL DB is preferred.
Initializing db
```
docker exec -it mongo mongosh admin
use admin
db.auth('admin','admin')
use test
db.createUser({user:'admin',pwd:'123456',roles:[{role:'readWrite',db:'test'}]})

### Bonus
```

- Write clear documentation on how it's designed and how to run the code.
- Write good in-code comments.
- Write good commit messages.
- An online demo is always welcome.
### Development

### Advanced requirements
```bash
$ npm i
$ npm run dev
$ open http://localhost:7001/
```

*These are used for some further challenges. You can safely skip them if you are not asked to do any, but feel free to try out.*
### Deploy
```
$ npm start
$ npm stop
```

- Provide a complete user auth (authentication/authorization/etc.) strategy, such as OAuth.
- Provide a complete logging (when/how/etc.) strategy.
- Imagine we have a new requirement right now that the user instances need to link to each other, i.e., a list of "followers/following" or "friends". Can you find out how you would design the model structure and what API you would build for querying or modifying it?
- Related to the requirement above, suppose the address of user now includes a geographic coordinate(i.e., latitude and longitude), can you build an API that,
- given a user name
- return the nearby friends
### npm scripts
* Use `npm run lint` to check code style.
* Use `npm test` to run unit test

## Redis
- Access using Redis as user login credentials

## What We Care About
### Redis partition

Feel free to use any open-source library as you see fit, but remember that we are evaluating your coding skills and problem solving skills.
- `user:accessToken:${id}` Cache user Token information
- `user:userinfo:${id}` Cache user basic information

Here's what you should aim for:

- Good use of current Node.js & API design best practices.
- Good testing approach.
- Extensible code.
## Jaeger

## FAQ
Jaeger [OpenTracing] (https://opentracing.io/docs/)

> Where should I send back the result when I'm done?
This implementation is based on the ctx mechanism and combined with midway's dependency injection to realize non-intrusive spanContext delivery
- Implements interface-level sampling by default
- span can be manually managed if small particle size sampling is required
```ts
ctx.tracerManager.startSpan('SpanName1')
await doSomethine()
ctx.tracerManager.finishSpan()
```

Fork this repo and send us a pull request when you think it's ready for review. You don't have to finish everything prior and you can continue to work on it. We don't have a deadline for the task.
## Interface document
```bash
open http://127.0.0.1:7001/swagger-ui/index.html#/
```

> What if I have a question?

Create a new issue in the repo and we will get back to you shortly.
## Unit testing
The unit test framework is [Mocha](https://mochajs.org/).
Support the case for `. Skip () ` `. Only () ` combination of optional * * * * and * * * * to achieve fast,
Suitable for scenarios involving complex, coupled business projects
Loading