Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
feat: add couchcb basic routes
Browse files Browse the repository at this point in the history
  • Loading branch information
fox1t committed Feb 7, 2020
1 parent 41aaa22 commit 6d378c3
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 83 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
COUCHDB_URL="http:https://localhost:5984"
10 changes: 5 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
logs
*.log
npm-debug.log*
.idea

# Runtime data
pids
Expand Down Expand Up @@ -46,9 +45,6 @@ profile-*
# vim swap files
*.swp

# webstorm
.idea

*code-workspace

# clinic
Expand All @@ -57,8 +53,12 @@ profile*
*flamegraph*

# generated code
examples/typescript-server.js
package-lock.json
dist
yarn.lock
.coveralls.yml

#misc
db/**
!db/config.json
.env
21 changes: 21 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
// Use IntelliSense to learn about possible Node.js debug attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Attach",
"type": "node",
"protocol": "inspector",
"request": "attach",
"restart": true,
"address": "localhost",
"sourceMaps": true,
"skipFiles": [
"node_modules"
],
"port": 9229
}
]
}
17 changes: 7 additions & 10 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
{
"typescript.tsdk": "node_modules/typescript/lib",
"eslint.autoFixOnSave": true,
"eslint.enable": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"eslint.validate": [
"javascript",
"javascriptreact",
{
"autoFix": true,
"language": "typescript"
},
{
"autoFix": true,
"language": "typescriptreact"
}
]
"typescript",
"typescriptreact"
],
}
37 changes: 22 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,38 @@

</div>

Node.js backend for [HospitalRun](http:https://hospitalrun.io/): free software for developing world hospitals.
Node.js backend for [HospitalRun](http:https://hospitalrun.io/): free software for developing world hospitals. To contribute, follow the guidelines in this readme or alternatively ask for details on the community Slack channel: [#contributors](https://hospitalrun-slack.herokuapp.com).

---

**Version 1.0.0-beta is no longer supported. Version 2 is currently under development.**

- To contribute, follow the guidelines in the readme or alternatively ask for details on Slack channel [#contributors](https://hospitalrun-slack.herokuapp.com).
- To use version 1.0.0-beta (not production ready) in a hospital facility, ask for support on Slack channel [#troubleshooting](https://hospitalrun-slack.herokuapp.com) or Spectrum channel [#support](https://spectrum.chat/hospitalrun).
**This repository is for the HospitalRun v2 and it is currently under heavy development. If you are searching for the no longer supported version 1.0.0-beta, you can find it [here](https://github.com/HospitalRun/hospitalrun-server/tree/1.0.0-beta).**

# Contributing

Contributions are always welcome. Before contributing please read our [contributor guide](https://github.com/HospitalRun/hospitalrun-server/blob/master/.github/CONTRIBUTING.md).
Contributions are always welcome. Before contributing please read our [contributor guide](https://github.com/HospitalRun/hospitalrun-server/blob/master/.github/CONTRIBUTING.md). Then follow these steps:

1. Fork this repository to your own GitHub account and then clone it to your local device
2. Navigate to the cloned folder: `cd hospitalrun-server`
3. Install the dependencies: `yarn`
4. Run `yarn run dev` to build and watch for code changes
1. [Fork](https://github.com/HospitalRun/hospitalrun-server/fork) this repository to your own GitHub account
2. Clone it to your local machine
3. Navigate to the cloned folder: `cd hospitalrun-server`
4. Install the dependencies: `npm install` or `yarn install`
5. Run `npm run dev` or `yarn dev` to build and watch for code changes:
- a development database will start on http:https://localhost:5984
- you can access its Admin interface on http:https://localhost:5984/_utils, `username: dev` and `password: dev`

## How to commit
## Environment
In order to run `hospitalrun-server` you need to set the correct environment variables. Since [dotenv](https://www.npmjs.com/package/dotenv) is already included, it is just matter of renaming `.env.example` file to `.env`: this file include all of the mandatory defaults.

## Database
This project uses [pouchdb-server](https://www.npmjs.com/package/pouchdb-server) for development and you, as contributor, don't need to provide your own CouchDB instance. Upon first run of the `dev` script (`npm run dev` or `yarn dev`), a new `data` folder will be created inside the `./db` folder. The database credentials are: `username: dev` and `password: dev`. The file `./db/config.json` contains the DB's configuration: you can change it if you want, but please don't commit any changes to it.

This repo uses [Conventional Commits](https://www.conventionalcommits.org/). [Commitizen](https://github.com/commitizen/cz-cli) is recommended for development. Once you have changes staged
you can run `git cz` from the root directory in order to commit to the proper standards.
**Note: PouchDB-server is meant to be use only during development. Please don't deploy any production/testing HospitalRun instances on it. For production deployments please follow the deployment guide.**

## Tests
Every code additions or fixs on the existing code, has to be tested. This project uses [node-tap](https://node-tap.org/) as test runner. To run all tests use `npm run test` or `yarn test`.

## How to commit

Alternatively, if you are using NPM 5.2+ you can use [npx](https://medium.com/@maybekatz/introducing-npx-an-npm-package-runner-55f7d4bd282b) instead of installing globally:
`npx git-cz`
This repo uses [Conventional Commits](https://www.conventionalcommits.org/). [Commitizen](https://github.com/commitizen/cz-cli) is mandatory for making proper commits. Once you have staged your changes, can run `npm run commit` or `yarn commit` from the root directory in order to commit following our standards.

# Documentations
## Plugins
Expand Down
18 changes: 18 additions & 0 deletions db/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"httpd": {
"port": 5984
},
"log": {
"file": "./db/pouchdb.log"
},
"couchdb": {
"database_dir": "./db/data",
"uuid": "ae0918af-6019-4679-bbde-0f774c342d8b"
},
"pouchdb_server": {
"no-stdout-logs": true
},
"admins": {
"dev": "-pbkdf2-f7588c0cc574a0f3d428bf9f1ab9f96a593313f7,13019f18c1801df1c01bb17e1dd15b1871ab1f3104151125,10"
}
}
40 changes: 0 additions & 40 deletions src/app.ts

This file was deleted.

55 changes: 42 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,53 @@
// we need this file because of this issue: https://github.com/fastify/fastify-cli/issues/131
import 'make-promises-safe'
import blipp from 'fastify-blipp'

import { join } from 'path'
import qs from 'qs'
import Fastify from 'fastify'
import cors from 'fastify-cors'
import helmet from 'fastify-helmet'
import noIcon from 'fastify-no-icon'
import AutoLoad from 'fastify-autoload'

import app from './app'
const port = Number(process.env.PORT) || 3000
const ip = process.env.IP || '0.0.0.0'

console.time('Boot Time')
const fastify = Fastify(app.options)
const fastify = Fastify({
querystringParser: (str: string) => qs.parse(str),
logger: true,
ignoreTrailingSlash: true,
})
fastify.register(cors, {
allowedHeaders: ['Content-Type', 'Authorization'],
})
fastify.register(helmet)
fastify.register(noIcon)

fastify.register(blipp)
fastify.register(app)
// This loads all application wide plugins defined in plugins folder
fastify.register(AutoLoad, {
dir: join(__dirname, 'plugins'),
options: {},
})

fastify.listen((process.env.PORT as any) || 3000, '0.0.0.0', err => {
// This loads all routes and services defined in services folder
fastify.register(AutoLoad, {
dir: join(__dirname, 'services'),
options: {},
})

if (process.env.NODE_ENV !== 'production') {
const blipp = require('fastify-blipp')
fastify.register(blipp)
}

fastify.listen(port, ip, err => {
if (err) {
console.log(err)
fastify.log.error(err)
process.exit(1)
}
fastify.blipp()

console.timeEnd('Boot Time')
console.log(`Server listening on port ${(fastify.server as any).address().port}`)
if (process.env.NODE_ENV !== 'production') {
fastify.blipp()
fastify.log.info(
`Database username 'dev', password 'dev, GUI running on: http:https://localhost:5984/_utils`,
)
}
})
29 changes: 29 additions & 0 deletions src/plugins/couchdb/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { FastifyInstance, FastifyError } from 'fastify'
import fp from 'fastify-plugin'
import nano, { ServerScope, Configuration } from 'nano'
import proxy from './proxy'

const COUCHDB_URL = String(process.env.COUCHDB_URL)

function couchDB(
fastify: FastifyInstance,
options: Configuration,
next: (err?: FastifyError | undefined) => void,
) {
const couch = nano(options)
fastify.decorate('couch', couch)
fastify.register(proxy, { url: COUCHDB_URL })
next()
}

couchDB.autoConfig = {
url: COUCHDB_URL,
}

export = fp(couchDB)

declare module 'fastify' {
interface FastifyInstance {
couch: ServerScope
}
}
24 changes: 24 additions & 0 deletions src/plugins/couchdb/proxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { FastifyInstance, FastifyError } from 'fastify'
import proxy from 'fastify-http-proxy'

interface Options {
url: string
}

function couchDBProxy(
fastify: FastifyInstance,
options: Options,
next: (err?: FastifyError | undefined) => void,
) {
fastify.register(proxy, {
upstream: options.url,
prefix: '/_db',
})
next()
}

couchDBProxy.autoConfig = {
url: process.env.COUCHDB_URL,
}

export = couchDBProxy

0 comments on commit 6d378c3

Please sign in to comment.