forked from shihjay2/nosh3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
184 lines (170 loc) · 5.13 KB
/
index.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import dotenv from 'dotenv'
dotenv.config()
import express from 'express'
import axios from 'axios'
import cors from 'cors'
import cookieParser from 'cookie-parser'
import fs from 'fs'
import isReachable from 'is-reachable'
import morgan from 'morgan'
import objectPath from 'object-path'
import path from 'path'
import {fileURLToPath} from 'url'
import PouchDB from 'pouchdb'
import PouchDBFind from 'pouchdb-find'
PouchDB.plugin(PouchDBFind)
import fhir from './fhir.mjs'
import auth from './auth.mjs'
import api from './api.mjs'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const client = __dirname + '/nosh3-client/dist/'
import { couchdbDatabase, couchdbInstall, couchdbUpdate, urlFix, userAdd } from './core.mjs'
import settings from './settings.mjs'
const app = express()
app.enable('trust proxy')
app.use(morgan('tiny'))
app.use(cors())
app.use(express.json({limit: '50mb'}))
app.use(express.urlencoded({ extended: true, limit: '50mb' }))
app.use(cookieParser())
app.use('/fhir', fhir)
app.use('/auth', auth)
app.use('/api', api)
app.use('/app', express.static(client))
app.use('/app/chart', (req, res) => {
res.sendFile(client + "index.html")
})
app.get('/app/dashboard', (req, res) => {
res.sendFile(client + "index.html")
})
app.get('/app/login', (req, res) => {
res.sendFile(client + "index.html")
})
app.get('/app/verify', (req, res) => {
res.sendFile(client + "index.html")
})
app.get('/app/error', (req, res) => {
res.sendFile(client + "index.html")
})
// app.get('/fetch/:file/:type?', (req, res) => {
// let type = 'json'
// if (req.params.type == 'txt') {
// type = 'txt'
// }
// let raw = {}
// if (type === 'json') {
// raw = fs.readFileSync('./assets/' + req.params.file + '.' + type)
// res.status(200).json(JSON.parse(raw))
// } else {
// raw = fs.readFileSync('./assets/' + req.params.file + '.' + type, 'utf-8')
// res.status(200).send(raw)
// }
// })
app.post('/fetch', (req, res) => {
let type = 'json'
if (req.body.type == 'txt') {
type = 'txt'
}
let raw = {}
if (type === 'json') {
raw = fs.readFileSync('./assets/' + req.body.file + '.' + type)
res.status(200).json(JSON.parse(raw))
} else {
raw = fs.readFileSync('./assets/' + req.body.file + '.' + type, 'utf-8')
res.status(200).send(raw)
}
})
app.post('/oidc', async(req, res) => {
if (objectPath.has(req, 'body.body')) {
try {
const result = await axios.post(req.body.url, req.body.body, {headers: {'Content-Type': 'application/json'}})
res.status(200).json(result.data)
} catch (err) {
res.status(200).json(err)
}
} else {
try {
const result = await axios.get(req.body.url, {headers: {'Content-Type': 'application/json'}})
res.status(200).json(result.data)
} catch (err) {
res.status(200).json(err)
}
}
})
app.get('/presentation/:pid/:id', async(req, res) => {
let prefix = ''
if (process.env.INSTANCE === 'digitalocean' && process.env.NOSH_ROLE === 'patient') {
prefix = req.params.pid + '_'
}
await sync('presentations', req.params.pid)
const db = new PouchDB(prefix + 'presentations')
try {
const doc = await db.get(req.params.id, {revs_info: true})
res.status(200).json(doc)
} catch(err) {
res.status(200).json(err)
}
})
app.get('/ready', async(req, res) => {
let b = false
for (let a = 0; a < 20; a++) {
if (!b) {
b = await isReachable(settings.couchdb_uri)
}
}
res.status(200).send('OK')
})
app.get('/start', async(req, res) => {
if (process.env.INSTANCE === 'digitalocean' && process.env.NOSH_ROLE === 'patient') {
res.status(200).send('Invalid URL for this instance')
}
const opts = JSON.parse(JSON.stringify(settings.couchdb_auth))
objectPath.set(opts, 'skip_setup', true)
const check = new PouchDB(urlFix(settings.couchdb_uri) + 'users', opts)
try {
const info = await check.info()
if (objectPath.has(info, 'error')) {
let b = false
if (info.error == 'not_found') {
await couchdbInstall()
let c = 0
while (!b && c < 40) {
b = await isReachable(settings.couchdb_uri)
if (b || c === 39) {
break
} else {
c++
}
}
} else {
b = true
}
if (b) {
await couchdbDatabase()
const db_users = new PouchDB(urlFix(settings.couchdb_uri) + 'users', settings.couchdb_auth)
const result = await db_users.find({selector: {_id: {$regex: "^nosh_*"}}})
if (result.docs.length === 0) {
await userAdd()
}
res.redirect(urlFix(req.protocol + '://' + req.hostname + '/') + 'app/login')
} else {
res.status(200).send('CouchDB is not restarting for some reason; try again')
}
} else {
await couchdbUpdate('', req.protocol, req.hostname)
res.redirect(urlFix(req.protocol + '://' + req.hostname + '/') + 'app/login')
}
} catch (e) {
const err = {
message: 'error with info call',
error: e
}
res.status(200).json(err)
}
})
const port = process.env.PORT || 4000
app.listen(port, () => {
console.log(`listening on ${port}`)
})
export default app