Skip to content

Commit

Permalink
refactor backend to remove all storage logic. Just auth and metadata …
Browse files Browse the repository at this point in the history
…endpoints now.

Adding docker image for couchdb and addign docker-compose file.
  • Loading branch information
AnalogJ committed Oct 9, 2022
1 parent f6681a8 commit f2bb44e
Show file tree
Hide file tree
Showing 57 changed files with 264 additions and 249,285 deletions.
56 changes: 0 additions & 56 deletions backend/pkg/auth/utils.go

This file was deleted.

6 changes: 6 additions & 0 deletions backend/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ func (c *configuration) Init() error {
c.SetDefault("web.src.frontend.path", "/opt/fasten/web")
c.SetDefault("web.database.location", "/opt/fasten/db/fasten.db") //TODO: should be /opt/fasten/fasten.db

c.SetDefault("web.couchdb.scheme", "http")
c.SetDefault("web.couchdb.host", "localhost")
c.SetDefault("web.couchdb.port", "5984")
c.SetDefault("web.couchdb.admin_username", "admin")
c.SetDefault("web.couchdb.admin_password", "mysecretpassword")

c.SetDefault("log.level", "INFO")
c.SetDefault("log.file", "")

Expand Down
88 changes: 88 additions & 0 deletions backend/pkg/database/couchdb_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package database

import (
"context"
"fmt"
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/config"
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/models"
"github.com/go-kivik/couchdb/v3"
_ "github.com/go-kivik/couchdb/v3" // The CouchDB driver
"github.com/go-kivik/kivik/v3"
"github.com/sirupsen/logrus"
)

func NewRepository(appConfig config.Interface, globalLogger logrus.FieldLogger) (DatabaseRepository, error) {
//backgroundContext := context.Background()

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Gorm/SQLite setup
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
globalLogger.Infof("Trying to connect to sqlite db: %s\n", appConfig.GetString("web.database.location"))

// When a transaction cannot lock the database, because it is already locked by another one,
// SQLite by default throws an error: database is locked. This behavior is usually not appropriate when
// concurrent access is needed, typically when multiple processes write to the same database.
// PRAGMA busy_timeout lets you set a timeout or a handler for these events. When setting a timeout,
// SQLite will try the transaction multiple times within this timeout.
// fixes #341
// https://rsqlite.r-dbi.org/reference/sqlitesetbusyhandler
// retrying for 30000 milliseconds, 30seconds - this would be unreasonable for a distributed multi-tenant application,
// but should be fine for local usage.

couchdbUrl := fmt.Sprintf("%s:https://%s:%s", appConfig.GetString("web.couchdb.scheme"), appConfig.GetString("web.couchdb.host"), appConfig.GetString("web.couchdb.port"))
database, err := kivik.New("couch", couchdbUrl)
if err != nil {
return nil, fmt.Errorf("Failed to connect to database! - %v", err)
}

err = database.Authenticate(context.Background(),
couchdb.BasicAuth(
appConfig.GetString("web.couchdb.admin_username"),
appConfig.GetString("web.couchdb.admin_password")),
)

if err != nil {
return nil, fmt.Errorf("Failed to authenticate to database! - %v", err)
}
globalLogger.Infof("Successfully connected to coubdb: %s\n", couchdbUrl)

deviceRepo := couchdbRepository{
appConfig: appConfig,
logger: globalLogger,
client: database,
}
return &deviceRepo, nil
}

type couchdbRepository struct {
appConfig config.Interface
logger logrus.FieldLogger

client *kivik.Client
}

type couchDbUser struct {
ID string `json:"_id"`
Name string `json:"name"`
Type string `json:"type"`
Roles []string `json:"roles"`
Password string `json:"password"`
}

func (cr *couchdbRepository) CreateUser(ctx context.Context, user *models.User) error {

newUser := &couchDbUser{
ID: fmt.Sprintf("%s%s", kivik.UserPrefix, user.Username),
Name: user.Username,
Type: "user",
Roles: []string{},
Password: user.Password,
}
db := cr.client.DB(ctx, "_users")
_, err := db.Put(ctx, newUser.ID, newUser)
return err
}

func (cr *couchdbRepository) Close() error {
return nil
}
17 changes: 0 additions & 17 deletions backend/pkg/database/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,4 @@ type DatabaseRepository interface {
Close() error

CreateUser(context.Context, *models.User) error
GetUserByEmail(context.Context, string) (*models.User, error)
GetCurrentUser(context.Context) *models.User

GetSummary(ctx context.Context) (*models.Summary, error)

UpsertResource(context.Context, *models.ResourceFhir) error
GetResourceBySourceType(context.Context, string, string) (*models.ResourceFhir, error)
GetResourceBySourceId(context.Context, string, string) (*models.ResourceFhir, error)
ListResources(context.Context, models.ListResourceQueryOptions) ([]models.ResourceFhir, error)
GetPatientForSources(ctx context.Context) ([]models.ResourceFhir, error)
//UpsertProfile(context.Context, *models.Profile) error
//UpsertOrganziation(context.Context, *models.Organization) error

CreateSource(context.Context, *models.Source) error
GetSource(context.Context, string) (*models.Source, error)
GetSourceSummary(context.Context, string) (*models.SourceSummary, error)
GetSources(context.Context) ([]models.Source, error)
}
Loading

0 comments on commit f2bb44e

Please sign in to comment.