github.com/safing/portbase@v0.19.5/database/dbmodule/db.go (about)

     1  package dbmodule
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/safing/portbase/database"
     7  	"github.com/safing/portbase/dataroot"
     8  	"github.com/safing/portbase/modules"
     9  	"github.com/safing/portbase/utils"
    10  )
    11  
    12  var (
    13  	databaseStructureRoot *utils.DirStructure
    14  
    15  	module *modules.Module
    16  )
    17  
    18  func init() {
    19  	module = modules.Register("database", prep, start, stop)
    20  }
    21  
    22  // SetDatabaseLocation sets the location of the database for initialization. Supply either a path or dir structure.
    23  func SetDatabaseLocation(dirStructureRoot *utils.DirStructure) {
    24  	if databaseStructureRoot == nil {
    25  		databaseStructureRoot = dirStructureRoot
    26  	}
    27  }
    28  
    29  func prep() error {
    30  	SetDatabaseLocation(dataroot.Root())
    31  	if databaseStructureRoot == nil {
    32  		return errors.New("database location not specified")
    33  	}
    34  
    35  	return nil
    36  }
    37  
    38  func start() error {
    39  	err := database.Initialize(databaseStructureRoot)
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	startMaintenanceTasks()
    45  	return nil
    46  }
    47  
    48  func stop() error {
    49  	return database.Shutdown()
    50  }