-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
360 lines (318 loc) · 11.2 KB
/
main.go
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package main
import (
"context"
"crypto/elliptic"
"crypto/sha1"
"crypto/tls"
"embed"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/nuts-foundation/nuts-demo-ehr/domain"
"github.com/nuts-foundation/nuts-demo-ehr/domain/acl"
"github.com/nuts-foundation/nuts-demo-ehr/domain/sharedcareplan"
nutspxp "github.com/nuts-foundation/nuts-demo-ehr/nutspxp/client"
openapiTypes "github.com/oapi-codegen/runtime/types"
"io"
"io/fs"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/jmoiron/sqlx"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
_ "github.com/mattn/go-sqlite3"
"github.com/nuts-foundation/nuts-demo-ehr/api"
"github.com/nuts-foundation/nuts-demo-ehr/domain/customers"
"github.com/nuts-foundation/nuts-demo-ehr/domain/dossier"
"github.com/nuts-foundation/nuts-demo-ehr/domain/episode"
"github.com/nuts-foundation/nuts-demo-ehr/domain/fhir"
"github.com/nuts-foundation/nuts-demo-ehr/domain/notification"
"github.com/nuts-foundation/nuts-demo-ehr/domain/patients"
"github.com/nuts-foundation/nuts-demo-ehr/domain/reports"
"github.com/nuts-foundation/nuts-demo-ehr/domain/transfer"
"github.com/nuts-foundation/nuts-demo-ehr/domain/transfer/receiver"
"github.com/nuts-foundation/nuts-demo-ehr/domain/transfer/sender"
"github.com/nuts-foundation/nuts-demo-ehr/domain/types"
"github.com/nuts-foundation/nuts-demo-ehr/internal/keyring"
nutsClient "github.com/nuts-foundation/nuts-demo-ehr/nuts/client"
"github.com/nuts-foundation/nuts-demo-ehr/nuts/registry"
"github.com/nuts-foundation/nuts-demo-ehr/sql"
"github.com/sirupsen/logrus"
)
const assetPath = "web/dist"
//go:embed web/dist/*
var embeddedFiles embed.FS
func getFileSystem(useFS bool) http.FileSystem {
if useFS {
logrus.Info("using live mode")
return http.FS(os.DirFS(assetPath))
}
logrus.Info("using embed mode")
fsys, err := fs.Sub(embeddedFiles, assetPath)
if err != nil {
panic(err)
}
return http.FS(fsys)
}
func main() {
// config stuff
config := loadConfig()
config.Print(log.Writer())
logrusLevel, err := logrus.ParseLevel(config.Verbosity)
if err != nil {
panic(err)
}
logrus.SetLevel(logrusLevel)
if config.FHIR.Server.Type == "" {
logrus.Fatal("Invalid FHIR server type, valid options are: 'hapi-multi-tenant', 'hapi' or 'other'")
}
// Read the authentication key
var authorizer *nutsClient.Authorizer
if keyPath := config.NutsNodeKeyPath; keyPath != "" {
key, err := keyring.Open(keyPath)
if err != nil {
logrus.Fatalf("Failed to open nuts-node key: %v", err)
}
authorizer = &nutsClient.Authorizer{Key: key, Audience: config.NutsNodeAPIAudience}
}
// init node API nutsClient
nodeClient := nutsClient.HTTPClient{NutsNodeAddress: config.NutsNodeAddress, Authorizer: authorizer}
pipClient := nutspxp.HTTPClient{PIPAddress: config.NutsPIPAddress}
customerRepository := customers.NewJsonFileRepository(config.CustomersFile)
server := createServer()
registerEHR(server, config, customerRepository, &nodeClient, pipClient)
// Start server
server.Logger.Fatal(server.Start(fmt.Sprintf(":%d", config.HTTPPort)))
}
func createServer() *echo.Echo {
server := echo.New()
server.HideBanner = true
// Register Echo logger middleware but do not log calls to the status endpoint,
// since that gets called by the Docker healthcheck very, very often which leads to lots of clutter in the log.
server.GET("/status", func(c echo.Context) error {
c.Response().WriteHeader(http.StatusNoContent)
return nil
})
loggerConfig := middleware.DefaultLoggerConfig
loggerConfig.Skipper = func(ctx echo.Context) bool {
return ctx.Request().RequestURI == "/status"
}
server.Use(middleware.LoggerWithConfig(loggerConfig))
server.Logger.SetLevel(1)
server.HTTPErrorHandler = func(err error, ctx echo.Context) {
if !ctx.Response().Committed {
_, _ = ctx.Response().Write([]byte(err.Error()))
ctx.Echo().Logger.Error(err)
}
}
server.Binder = &fhirBinder{}
server.HTTPErrorHandler = httpErrorHandler
return server
}
func registerEHR(server *echo.Echo, config Config, customerRepository customers.Repository, nodeClient *nutsClient.HTTPClient, pipClient nutspxp.Client) {
var passwd string
if config.Credentials.Empty() {
passwd = generateAuthenticationPassword(config)
logrus.Infof("Authentication credentials not configured, so they were generated (password=%s)", passwd)
} else {
passwd = config.Credentials.Password
}
// Initialize services
sqlDB := sqlx.MustConnect("sqlite3", config.DBConnectionString)
sqlDB.SetMaxOpenConns(1)
fhirNotifier := transfer.FireAndForgetNotifier{}
var tlsClientConfig *tls.Config
var err error
if config.TLS.Client.IsConfigured() {
log.Println("Configuring TLS client certificate for calls to remote Nuts Nodes and FHIR servers.")
if tlsClientConfig, err = config.TLS.Client.Load(); err != nil {
log.Fatal(err)
}
fhirNotifier.TLSConfig = tlsClientConfig
}
fhirClientFactory := fhir.NewFactory(
fhir.WithURL(config.FHIR.Server.Address),
fhir.WithMultiTenancyEnabled(config.FHIR.Server.SupportsMultiTenancy()),
fhir.WithTLS(tlsClientConfig),
)
patientRepository := patients.NewFHIRPatientRepository(patients.Factory{}, fhirClientFactory)
reportRepository := reports.NewFHIRRepository(fhirClientFactory)
orgRegistry := registry.NewOrganizationRegistry(nodeClient)
dossierRepository := dossier.NewSQLiteDossierRepository(dossier.Factory{}, sqlDB)
transferSenderRepo := sender.NewTransferRepository(sqlDB)
transferReceiverRepo := receiver.NewTransferRepository(sqlDB)
transferSenderService := sender.NewTransferService(nodeClient, pipClient, fhirClientFactory, transferSenderRepo, customerRepository, dossierRepository, patientRepository, orgRegistry, fhirNotifier)
transferReceiverService := receiver.NewTransferService(nodeClient, fhirClientFactory, transferReceiverRepo, customerRepository, orgRegistry, fhirNotifier)
tenantInitializer := func(tenant string) error {
if !config.FHIR.Server.SupportsMultiTenancy() {
return nil
}
return fhir.InitializeTenant(config.FHIR.Server.Address, tenant)
}
// Shared Care Plan
var scpService *sharedcareplan.Service
if config.SharedCarePlanning.Enabled() {
scpRepository, err := sharedcareplan.NewRepository(sqlDB)
if err != nil {
log.Fatal(err)
}
scpFHIRClient := fhir.NewFactory(fhir.WithURL(config.SharedCarePlanning.CarePlanService.FHIRBaseURL))()
scpService = &sharedcareplan.Service{DossierRepository: dossierRepository, PatientRepository: patientRepository, Repository: scpRepository, FHIRClient: scpFHIRClient}
}
if config.LoadTestPatients {
allCustomers, err := customerRepository.All()
if err != nil {
log.Fatal(err)
}
for _, customer := range allCustomers {
if err := tenantInitializer(customer.Id); err != nil {
log.Fatal(err)
}
registerPatients(patientRepository, sqlDB, customer.Id)
}
}
auth := api.NewAuth(config.sessionKey, customerRepository, passwd)
aclRepository, err := acl.NewRepository(sqlDB)
if err != nil {
log.Fatal(err)
}
// Initialize wrapper
apiWrapper := api.Wrapper{
APIAuth: auth,
ACL: aclRepository,
NutsClient: nodeClient,
CustomerRepository: customerRepository,
PatientRepository: patientRepository,
ReportRepository: reportRepository,
DossierRepository: dossier.NewSQLiteDossierRepository(dossier.Factory{}, sqlDB),
TransferSenderRepo: transferSenderRepo,
OrganizationRegistry: orgRegistry,
TransferSenderService: transferSenderService,
TransferReceiverService: transferReceiverService,
TransferReceiverRepo: transferReceiverRepo,
ZorginzageService: domain.ZorginzageService{NutsClient: nodeClient},
SharedCarePlanService: scpService,
FHIRService: fhir.Service{ClientFactory: fhirClientFactory},
EpisodeService: episode.NewService(fhirClientFactory, nodeClient, orgRegistry, aclRepository),
TenantInitializer: tenantInitializer,
NotificationHandler: notification.NewHandler(nodeClient, fhirClientFactory, transferReceiverService, orgRegistry),
}
// JWT checking for correct claims
server.Use(auth.JWTHandler)
server.Use(sql.Transactional(sqlDB))
api.RegisterHandlersWithBaseURL(server, apiWrapper, "/web")
// Setup asset serving:
// Check if we use live mode from the file system or using embedded files
useFS := len(os.Args) > 1 && os.Args[1] == "live"
assetHandler := http.FileServer(getFileSystem(useFS))
server.GET("/*", echo.WrapHandler(assetHandler))
}
func registerPatients(repository patients.Repository, db *sqlx.DB, customerID string) {
pdate := func(value time.Time) *openapiTypes.Date {
val := openapiTypes.Date{Time: value}
return &val
}
pstring := func(value string) *string {
return &value
}
props := []types.PatientProperties{
{
Ssn: pstring("1234567890"),
Dob: pdate(time.Date(1980, 10, 10, 0, 0, 0, 0, time.UTC)),
FirstName: "Henk",
Surname: "de Vries",
Gender: types.Male,
Zipcode: "6825AX",
},
{
Ssn: pstring("1234567891"),
Dob: pdate(time.Date(1939, 1, 5, 0, 0, 0, 0, time.UTC)),
FirstName: "Grepelsteeltje",
Surname: "Grouw",
Gender: types.Female,
Zipcode: "9999AA",
},
{
Ssn: pstring("1234567892"),
Dob: pdate(time.Date(1972, 1, 10, 0, 0, 0, 0, time.UTC)),
FirstName: "Dibbes",
Surname: "Bouwman",
Gender: types.Male,
Zipcode: "1234ZZ",
},
{
Ssn: pstring("1234567893"),
Dob: pdate(time.Date(2001, 2, 27, 0, 0, 0, 0, time.UTC)),
FirstName: "Anne",
Surname: "von Oben",
Gender: types.Other,
Zipcode: "7777AX",
},
}
if err := sql.ExecuteTransactional(db, func(ctx context.Context) error {
for _, prop := range props {
if _, err := repository.NewPatient(ctx, customerID, prop); err != nil {
return fmt.Errorf("unable to register test patient: %w", err)
}
}
return nil
}); err != nil {
log.Fatal(err)
}
}
func generateAuthenticationPassword(config Config) string {
pkHashBytes := sha1.Sum(elliptic.Marshal(config.sessionKey.Curve, config.sessionKey.X, config.sessionKey.Y))
return hex.EncodeToString(pkHashBytes[:])
}
// httpErrorHandler includes the err.Err() string in a { "error": "msg" } json hash
func httpErrorHandler(err error, c echo.Context) {
var (
code = http.StatusInternalServerError
msg interface{}
)
type Map map[string]interface{}
if he, ok := err.(*echo.HTTPError); ok {
code = he.Code
msg = he.Message
if he.Internal != nil {
msg = fmt.Sprintf("%v, %v", err, he.Internal)
}
} else {
msg = err.Error()
}
if _, ok := msg.(string); ok {
msg = Map{"error": msg}
}
// Send response
if !c.Response().Committed {
if c.Request().Method == http.MethodHead {
err = c.NoContent(code)
} else {
err = c.JSON(code, msg)
}
if err != nil {
c.Logger().Error(err)
}
}
}
type fhirBinder struct{}
func (cb *fhirBinder) Bind(i interface{}, c echo.Context) (err error) {
// You may use default binder
db := new(echo.DefaultBinder)
if err = db.Bind(i, c); err != echo.ErrUnsupportedMediaType {
return
}
if strings.Contains(c.Request().Header.Get("Content-Type"), "application/fhir+json") {
var bytes []byte
if bytes, err = io.ReadAll(c.Request().Body); err != nil {
return
}
if err = json.Unmarshal(bytes, i); err != nil {
return
}
}
return
}