Coder Social home page Coder Social logo

demo-maivan's Introduction

What is this

/cc Maika and Ivan

This project is a demo, it is not meant to be a production solution but only a fun demo to implement a generic web server and show some fun stuff available in javascript/node/typescript in April 2022.

Recipe

  • Zero-production dependencies
  • An homemade solution to test suites
  • An web server implementing middleware and routes using the standard HTTP package (don't get excited too fast, it lacks tons of details)

Web and Test

Express JS and Jest are de facto standards. They allow for quick prototypes up to huge applications. However, in this test, we are implementing a web server for fun to understand if we can have something similar and ergonomic as ExpressJS in a few lines.

export const addRoute =
  (method: Method) =>
  (path: string, ...handlers: RouteHandler[]) => {
    routers.push({ method, path, handlers })
  }

export const get = addRoute('GET')

export const post = addRoute('POST')

export const patch = addRoute('PATCH')

Volatile Database

The volatile database is an example of managing controllers, testing them, and implementing them in the routers.

No matter how exemplary the Model's implementation within the volatile storage is, it can constantly be refactored or re-implemented without touching the test or the webserver.

What about time

This system is not responsible for the time zone, as this service is supposed to be used from multiple locations, we only store UTC time using Epoch format (in milliseconds).

The given date times (to create new entries or to search by dates) are normalized so that provided date times are set at midnight. The normalization is done at the middleware level. Check the function normalizeEpoch for more details.

Hack on it

⚠️ This software requires NodeJS >= v12

  1. clone the repository using [email protected]:kedoska/demo-maivan.git
  2. change directory cd demo-maivan
  3. install dev dependencies npm i
  4. run tests npm test
  5. run the server npm start

Add Data

POST cities creates a new city in the database.

curl -X POST "http://localhost:3000/cities?name=Barcelona&country=SPA&region=Catalunia&population=1000"

PATCH cities:name adds incidence information to a specific city.

# 3 of april 2022
curl -X PATCH "http://localhost:3000/cities/Barcelona?cases=3000&epoch=1649001840000"
# 4 of april 2022
curl -X PATCH "http://localhost:3000/cities/Barcelona?cases=3000&epoch=1649079340000"

Get by date

GET /reports/:segment/:name returns the report for the required segment

A segment represents a city, a region or a country.

curl "http://localhost:3000/reports/city/Barcelona?epoch=1649079340000"
curl "http://localhost:3000/reports/region/Catalunia?epoch=1649079340000"
curl "http://localhost:3000/reports/country/SPA?epoch=1649079340000"
{
  "location": { "city": "Barcelona", "country": "SPA", "population": 1000, "region": "Catalunia" },
  "fromEpoch": 1649023200000,
  "toEpoch": 1649023200000,
  "totalDays": 1,
  "totalCases": 3000,
  "incidenceRate": 300000
}

Get by range

curl "http://localhost:3000/reports/city/Barcelona?fromEpoch=1649001840000&toEpoch=1649079340000"
curl "http://localhost:3000/reports/region/Catalunia?fromEpoch=1649001840000&toEpoch=1649079340000"
curl "http://localhost:3000/reports/country/SPA?fromEpoch=1649001840000&toEpoch=1649079340000"
{
  "location": { "city": "Barcelona", "country": "SPA", "population": 1000, "region": "Catalunia" },
  "fromEpoch": 1648936800000,
  "toEpoch": 1649023200000,
  "totalDays": 2,
  "totalCases": 6000,
  "incidenceRate": 600000
}

License

Copyright © 2022 Marco Casula[email protected]

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.

demo-maivan's People

Contributors

kedoska avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.