working commit
This commit is contained in:
+11
-11
@@ -18,7 +18,7 @@ import (
|
||||
|
||||
"mstore/pkg/auxx509"
|
||||
|
||||
"sigs.k8s.io/yaml"
|
||||
yaml "go.yaml.in/yaml/v4"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
@@ -35,18 +35,18 @@ type Storage struct {
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Service Service `json:"service"`
|
||||
Database Database `json:"database" yaml:"database"`
|
||||
Storage Storage `json:"storage" yaml:"storage"`
|
||||
AsDaemon bool `json:"asDaemon" yaml:"asDaemon"`
|
||||
Logpath string `json:"logpath" yaml:"logpath"`
|
||||
Runpath string `json:"runpath" yaml:"runpath"`
|
||||
Version string `json:"version" yaml:"version"`
|
||||
Service Service `json:"service" yaml:"service"`
|
||||
Database Database `json:"database" yaml:"database"`
|
||||
Storage Storage `json:"storage" yaml:"storage"`
|
||||
AsDaemon bool `json:"asDaemon" yaml:"asDaemon"`
|
||||
Logpath string `json:"logpath" yaml:"logpath"`
|
||||
Runpath string `json:"runpath" yaml:"runpath"`
|
||||
Version string `json:"version" yaml:"version"`
|
||||
Certpath string `json:"certpath,omitempty" yaml:"certath"`
|
||||
Keypath string `json:"keypath,omitempty" yaml:"keypath"`
|
||||
X509Cert string `json:"-" yaml:"-"`
|
||||
X509Key string `json:"-" yaml:"-"`
|
||||
Datadir string `json:"datadir"`
|
||||
X509Cert string `json:"-" yaml:"-"`
|
||||
X509Key string `json:"-" yaml:"-"`
|
||||
Datadir string `json:"datadir" yaml:datadir`
|
||||
}
|
||||
|
||||
func NewConfig() *Config {
|
||||
|
||||
+7
-4
@@ -10,8 +10,11 @@
|
||||
package descr
|
||||
|
||||
type Server struct {
|
||||
SchemeCreated bool `json:"SchemeCreated"`
|
||||
AnonymousCreated bool `json:"AnonymousCreated"`
|
||||
InituserCreated bool `json:"inituserCreated"`
|
||||
SchemeCreatedAt string `json:"inituserCreatedAt"`
|
||||
SchemeCreated bool `yaml:"schemeCreated"`
|
||||
AnonymousCreated bool `yaml:"anonymousCreated"`
|
||||
InituserCreated bool `yaml:"inituserCreated"`
|
||||
|
||||
SchemeCreatedAt string `yaml:"schemeCreatedAt"`
|
||||
AnonymousCreatedAt string `yaml:"anonymousCreatedAt"`
|
||||
InituserCreatedAt string `yaml:"inituserCreatedAt"`
|
||||
}
|
||||
|
||||
+4
-4
@@ -26,7 +26,7 @@ func (db *Database) WriteAnonymous(ctx context.Context) error {
|
||||
password := auxtool.RandomString(64)
|
||||
passhash := auxpwd.MakeSHA256Hash([]byte(password))
|
||||
accountDescr := &descr.Account{
|
||||
ID: auxuuid.NewUUID(),
|
||||
ID: descr.AnonymousID,
|
||||
Username: descr.AnonimousUsername,
|
||||
Passhash: passhash,
|
||||
Disabled: false,
|
||||
@@ -56,7 +56,7 @@ func (db *Database) WriteAnonymous(ctx context.Context) error {
|
||||
grantDescr = &descr.Grant{
|
||||
ID: auxuuid.NewUUID(),
|
||||
AccountID: accountDescr.ID,
|
||||
Right: descr.RightReadFiles,
|
||||
Right: descr.RightReadImages,
|
||||
Pattern: "*",
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
@@ -78,8 +78,8 @@ func (db *Database) WriteInituser(ctx context.Context) error {
|
||||
now := auxtool.TimeNow()
|
||||
passhash := auxpwd.MakeSHA256Hash([]byte(descr.InitUsername))
|
||||
accountDescr := &descr.Account{
|
||||
ID: auxuuid.NewUUID(),
|
||||
Username: descr.AnonimousUsername,
|
||||
ID: descr.InitID,
|
||||
Username: descr.InitUsername,
|
||||
Passhash: passhash,
|
||||
Disabled: false,
|
||||
CreatedAt: now,
|
||||
|
||||
@@ -76,7 +76,7 @@ const schema = `
|
||||
CREATE TABLE IF NOT EXISTS grants (
|
||||
id TEXT NOT NULL,
|
||||
account_id INT NOT NULL,
|
||||
operation TEXT NOT NULL,
|
||||
right TEXT NOT NULL,
|
||||
pattern TEXT NOT NULL,
|
||||
created_at TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL,
|
||||
@@ -88,8 +88,8 @@ const schema = `
|
||||
CREATE INDEX IF NOT EXISTS grants_index01
|
||||
ON grants(account_id);
|
||||
CREATE INDEX IF NOT EXISTS grants_index02
|
||||
ON grants(account_id, operation);
|
||||
ON grants(account_id, right);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS grants_index03
|
||||
ON grants(account_id, operation, pattern);
|
||||
ON grants(account_id, right, pattern);
|
||||
|
||||
`
|
||||
|
||||
+61
-24
@@ -29,8 +29,9 @@ import (
|
||||
"mstore/app/operator"
|
||||
"mstore/app/service"
|
||||
"mstore/app/storage"
|
||||
"mstore/pkg/auxtool"
|
||||
|
||||
"sigs.k8s.io/yaml"
|
||||
yaml "go.yaml.in/yaml/v4"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
@@ -100,6 +101,37 @@ func (srv *Server) Configure() error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (srv *Server) WriteStat() error {
|
||||
// Write status file
|
||||
var err error
|
||||
statefilePath := filepath.Join(srv.conf.Datadir, "server.yaml")
|
||||
stateData, err := yaml.Marshal(srv.stat)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ioutil.WriteFile(statefilePath, stateData, 0640)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (srv *Server) ReadStat() error {
|
||||
var err error
|
||||
// Read state file
|
||||
statefilePath := filepath.Join(srv.conf.Datadir, "server.yaml")
|
||||
stateData, err := ioutil.ReadFile(statefilePath)
|
||||
if err == nil {
|
||||
stat := descr.Server{}
|
||||
err = yaml.Unmarshal(stateData, &stat)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
srv.stat = stat
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (srv *Server) Build() error {
|
||||
var err error
|
||||
srv.logg.Infof("Server building")
|
||||
@@ -131,23 +163,20 @@ func (srv *Server) Build() error {
|
||||
}
|
||||
|
||||
// Read state file
|
||||
statefilePath := filepath.Join(srv.conf.Datadir, "server.yaml")
|
||||
stateData, err := ioutil.ReadFile(statefilePath)
|
||||
srv.logg.Infof("Reading server status")
|
||||
err = srv.ReadStat()
|
||||
if err != nil {
|
||||
err = yaml.Unmarshal(stateData, &srv.stat)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Creating database
|
||||
// Creating database dir
|
||||
dbdir := srv.conf.Database.Basepath
|
||||
srv.logg.Infof("Creating database directory %s ", dbdir)
|
||||
err = os.MkdirAll(dbdir, 0750)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Creating database
|
||||
mdb := maindb.NewDatabase(dbdir)
|
||||
srv.logg.Infof("Opening main database")
|
||||
err = mdb.OpenDatabase()
|
||||
@@ -165,34 +194,42 @@ func (srv *Server) Build() error {
|
||||
return err
|
||||
}
|
||||
srv.stat.SchemeCreated = true
|
||||
srv.stat.SchemeCreatedAt = auxtool.TimeNow()
|
||||
srv.logg.Infof("Writing server status")
|
||||
err = srv.WriteStat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// Created anonymous user
|
||||
if !srv.stat.AnonymousCreated {
|
||||
srv.logg.Infof("Creating anonimous user")
|
||||
srv.logg.Infof("Creating anonymous user")
|
||||
err = mdb.WriteAnonymous(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
srv.stat.AnonymousCreated = true
|
||||
}
|
||||
if !srv.stat.InituserCreated {
|
||||
srv.logg.Infof("Creating init user")
|
||||
err = srv.mdb.WriteAnonymous(ctx)
|
||||
srv.stat.AnonymousCreatedAt = auxtool.TimeNow()
|
||||
srv.logg.Infof("Writing server status")
|
||||
err = srv.WriteStat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if !srv.stat.InituserCreated {
|
||||
srv.logg.Infof("Creating init user")
|
||||
err = srv.mdb.WriteInituser(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
srv.stat.InituserCreatedAt = auxtool.TimeNow()
|
||||
srv.stat.InituserCreated = true
|
||||
}
|
||||
|
||||
// Write status file
|
||||
stateData, err = yaml.Marshal(srv.stat)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(statefilePath, stateData, 0640)
|
||||
if err != nil {
|
||||
return err
|
||||
srv.logg.Infof("Writing server status")
|
||||
err = srv.WriteStat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Creating storage
|
||||
|
||||
Reference in New Issue
Block a user