Adapt to new module structure with base and core modules
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
|
|
||||||
"github.com/safing/portbase/api"
|
"github.com/safing/portbase/api"
|
||||||
|
"github.com/safing/portbase/database/dbmodule"
|
||||||
"github.com/safing/portbase/modules"
|
"github.com/safing/portbase/modules"
|
||||||
"github.com/safing/portbase/notifications"
|
"github.com/safing/portbase/notifications"
|
||||||
|
|
||||||
@@ -15,19 +16,17 @@ var (
|
|||||||
dataDir string
|
dataDir string
|
||||||
databaseDir string
|
databaseDir string
|
||||||
|
|
||||||
shuttingDown = make(chan struct{})
|
baseModule = modules.Register("base", prepBase, nil, nil)
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
flag.StringVar(&dataDir, "data", "", "set data directory")
|
flag.StringVar(&dataDir, "data", "", "set data directory")
|
||||||
flag.StringVar(&databaseDir, "db", "", "alias to --data (deprecated)")
|
flag.StringVar(&databaseDir, "db", "", "alias to --data (deprecated)")
|
||||||
|
|
||||||
modules.Register("core", prep, start, stop)
|
|
||||||
|
|
||||||
notifications.SetPersistenceBasePath("core:notifications")
|
notifications.SetPersistenceBasePath("core:notifications")
|
||||||
}
|
}
|
||||||
|
|
||||||
func prep() error {
|
func prepBase() error {
|
||||||
// backwards compatibility
|
// backwards compatibility
|
||||||
if dataDir == "" {
|
if dataDir == "" {
|
||||||
dataDir = databaseDir
|
dataDir = databaseDir
|
||||||
@@ -38,33 +37,24 @@ func prep() error {
|
|||||||
return errors.New("please set the data directory using --data=/path/to/data/dir")
|
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
|
// set api listen address
|
||||||
api.SetDefaultAPIListenAddress("127.0.0.1:817")
|
api.SetDefaultAPIListenAddress("127.0.0.1:817")
|
||||||
|
|
||||||
// init config
|
return nil
|
||||||
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()
|
|
||||||
}
|
}
|
||||||
11
core/core.go
Normal file
11
core/core.go
Normal 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()
|
||||||
|
}
|
||||||
58
core/db.go
58
core/db.go
@@ -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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -27,7 +27,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
modules.Register("nameserver", prep, start, nil, "intel")
|
modules.Register("nameserver", prep, start, nil, "core", "intel")
|
||||||
|
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
listenAddress = "0.0.0.0:53"
|
listenAddress = "0.0.0.0:53"
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
modules.Register("nameserver", prep, start, nil, "intel")
|
modules.Register("nameserver", prep, start, nil, "core", "intel")
|
||||||
}
|
}
|
||||||
|
|
||||||
func prep() error {
|
func prep() error {
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
modules.Register("profile:index", nil, start, stop, "profile", "database")
|
modules.Register("profile:index", nil, start, stop, "core", "profile")
|
||||||
}
|
}
|
||||||
|
|
||||||
func start() (err error) {
|
func start() (err error) {
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
modules.Register("ui", prep, nil, nil, "updates", "api")
|
modules.Register("ui", prep, nil, nil, "core", "updates")
|
||||||
}
|
}
|
||||||
|
|
||||||
func prep() error {
|
func prep() error {
|
||||||
|
|||||||
Reference in New Issue
Block a user