Adapt to new module structure with base and core modules

This commit is contained in:
Daniel 2019-08-09 16:46:37 +02:00
parent 856b69829f
commit 4b2ff39246
7 changed files with 35 additions and 92 deletions

View file

@ -5,6 +5,7 @@ import (
"flag"
"github.com/safing/portbase/api"
"github.com/safing/portbase/database/dbmodule"
"github.com/safing/portbase/modules"
"github.com/safing/portbase/notifications"
@ -15,19 +16,17 @@ var (
dataDir string
databaseDir string
shuttingDown = make(chan struct{})
baseModule = modules.Register("base", prepBase, nil, nil)
)
func init() {
flag.StringVar(&dataDir, "data", "", "set data directory")
flag.StringVar(&databaseDir, "db", "", "alias to --data (deprecated)")
modules.Register("core", prep, start, stop)
notifications.SetPersistenceBasePath("core:notifications")
}
func prep() error {
func prepBase() error {
// backwards compatibility
if dataDir == "" {
dataDir = databaseDir
@ -38,33 +37,24 @@ func prep() error {
return errors.New("please set the data directory using --data=/path/to/data/dir")
}
// initialize structure
err := structure.Initialize(dataDir, 0755)
if err != nil {
return err
}
// set database location
dbmodule.SetDatabaseLocation("", structure.Root())
// init config
logFlagOverrides()
err = registerConfig()
if err != nil {
return err
}
// set api listen address
api.SetDefaultAPIListenAddress("127.0.0.1:817")
// init config
err := registerConfig()
if err != nil {
return err
}
// initialize structure
return structure.Initialize(dataDir, 0755)
}
func start() error {
logFlagOverrides()
// init DB
err := startDB()
if err != nil {
return err
}
// register DBs
return registerDatabases()
}
func stop() error {
close(shuttingDown)
return stopDB()
return nil
}

11
core/core.go Normal file
View file

@ -0,0 +1,11 @@
package core
import "github.com/safing/portbase/modules"
var (
coreModule = modules.Register("core", nil, startCore, nil, "base", "database", "config", "api", "random")
)
func startCore() error {
return registerDatabases()
}

View file

@ -1,58 +0,0 @@
package core
import (
"sync"
"time"
"github.com/safing/portbase/database"
"github.com/safing/portbase/log"
"github.com/safing/portmaster/core/structure"
)
var (
maintenanceWg sync.WaitGroup
maintenanceShortTickDuration = 10 * time.Minute
maintenanceLongTickDuration = 1 * time.Hour
)
func startDB() error {
err := database.Initialize(dataDir, structure.Root())
if err == nil {
maintenanceWg.Add(1)
go maintenanceWorker()
}
return err
}
func stopDB() error {
maintenanceWg.Wait()
return database.Shutdown()
}
func maintenanceWorker() {
ticker := time.NewTicker(maintenanceShortTickDuration)
longTicker := time.NewTicker(maintenanceLongTickDuration)
for {
select {
case <-ticker.C:
err := database.Maintain()
if err != nil {
log.Errorf("database: maintenance error: %s", err)
}
case <-longTicker.C:
err := database.MaintainRecordStates()
if err != nil {
log.Errorf("database: record states maintenance error: %s", err)
}
err = database.MaintainThorough()
if err != nil {
log.Errorf("database: thorough maintenance error: %s", err)
}
case <-shuttingDown:
maintenanceWg.Done()
return
}
}
}

View file

@ -27,7 +27,7 @@ var (
)
func init() {
modules.Register("nameserver", prep, start, nil, "intel")
modules.Register("nameserver", prep, start, nil, "core", "intel")
if runtime.GOOS == "windows" {
listenAddress = "0.0.0.0:53"

View file

@ -21,7 +21,7 @@ var (
)
func init() {
modules.Register("nameserver", prep, start, nil, "intel")
modules.Register("nameserver", prep, start, nil, "core", "intel")
}
func prep() error {

View file

@ -23,7 +23,7 @@ var (
)
func init() {
modules.Register("profile:index", nil, start, stop, "profile", "database")
modules.Register("profile:index", nil, start, stop, "core", "profile")
}
func start() (err error) {

View file

@ -5,7 +5,7 @@ import (
)
func init() {
modules.Register("ui", prep, nil, nil, "updates", "api")
modules.Register("ui", prep, nil, nil, "core", "updates")
}
func prep() error {