add daemon config
This commit is contained in:
@@ -40,7 +40,7 @@ func Load() ClientConfiguration {
|
||||
panic("failed to read the configuration file: " + err.Error())
|
||||
}
|
||||
|
||||
return c
|
||||
return fillDefault(c)
|
||||
}
|
||||
|
||||
func Default() ClientConfiguration {
|
||||
@@ -50,3 +50,11 @@ func Default() ClientConfiguration {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func fillDefault(c ClientConfiguration) ClientConfiguration {
|
||||
if len(c.Deamon.URL) == 0 {
|
||||
c.Deamon.URL = Default().Deamon.URL
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
func NewServer(data *storage.Repository, scheduler *cronruntime.Scheduler, port int) *HTTPServer {
|
||||
func NewServer(data *storage.Repository, scheduler *cronruntime.Scheduler, addr string, port int) *HTTPServer {
|
||||
s := &HTTPServer{
|
||||
data: data,
|
||||
scheduler: scheduler,
|
||||
@@ -57,7 +57,7 @@ func NewServer(data *storage.Repository, scheduler *cronruntime.Scheduler, port
|
||||
})
|
||||
})
|
||||
s.Server = &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", port),
|
||||
Addr: fmt.Sprintf("%s:%d", addr, port),
|
||||
Handler: router,
|
||||
}
|
||||
return s
|
||||
|
||||
71
cmd/server/core/config/config.go
Normal file
71
cmd/server/core/config/config.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type (
|
||||
DaemonConfiguration struct {
|
||||
Server ServerConfiguration `json:"server"`
|
||||
Database DatabaseConfiguration `json:"database"`
|
||||
}
|
||||
|
||||
ServerConfiguration struct {
|
||||
Address string `json:"addr"`
|
||||
Port uint16 `json:"port"`
|
||||
}
|
||||
|
||||
DatabaseConfiguration struct {
|
||||
Path string `json:"path"`
|
||||
}
|
||||
)
|
||||
|
||||
func Load(path string) (DaemonConfiguration, error) {
|
||||
path = filepath.Join(path, "config.json")
|
||||
|
||||
f, err := os.OpenFile(path, os.O_RDONLY, 0)
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return Default(), nil
|
||||
}
|
||||
return DaemonConfiguration{}, fmt.Errorf("failed to load configuration file (%s): %s", path, err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var c DaemonConfiguration
|
||||
d := json.NewDecoder(f)
|
||||
if err := d.Decode(&c); err != nil {
|
||||
return DaemonConfiguration{}, fmt.Errorf("failed to read configuration file (%s): %s", path, err)
|
||||
}
|
||||
|
||||
return fillDefault(c), nil
|
||||
}
|
||||
|
||||
func Default() DaemonConfiguration {
|
||||
return DaemonConfiguration{
|
||||
Server: ServerConfiguration{
|
||||
Address: "127.0.0.1",
|
||||
Port: 25697,
|
||||
},
|
||||
Database: DatabaseConfiguration{
|
||||
Path: "/var/lib/mirror-sync/data.db",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func fillDefault(c DaemonConfiguration) DaemonConfiguration {
|
||||
if len(c.Database.Path) == 0 {
|
||||
c.Database.Path = Default().Database.Path
|
||||
}
|
||||
if len(c.Server.Address) == 0 {
|
||||
c.Server.Address = Default().Server.Address
|
||||
}
|
||||
if c.Server.Port == 0 {
|
||||
c.Server.Port = Default().Server.Port
|
||||
}
|
||||
return c
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"mirror-sync/cmd/server/api"
|
||||
"mirror-sync/cmd/server/core/config"
|
||||
cronruntime "mirror-sync/cmd/server/core/runtime"
|
||||
"mirror-sync/cmd/server/core/storage"
|
||||
"mirror-sync/pkg/constants"
|
||||
@@ -13,14 +15,29 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
ecpath := os.Getenv("MIRRORSYNC_CONFIG_PATH")
|
||||
if len(ecpath) == 0 {
|
||||
ecpath = "/etc/mirror-sync"
|
||||
}
|
||||
|
||||
var configPath string
|
||||
flag.StringVar(&configPath, "config", ecpath, "path to the configuration folder")
|
||||
flag.Parse()
|
||||
|
||||
c, err := config.Load(configPath)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "failed to start server:", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
dbPath := os.Getenv("MIRRORSYNC_DB_PATH")
|
||||
if len(dbPath) == 0 {
|
||||
dbPath = "/var/lib/mirror-sync/data.db"
|
||||
dbPath = c.Database.Path
|
||||
}
|
||||
|
||||
p := os.Getenv("MIRRORSYNC_PORT")
|
||||
if len(p) == 0 {
|
||||
p = "25697"
|
||||
p = fmt.Sprintf("%d", c.Server.Port)
|
||||
}
|
||||
|
||||
port, err := strconv.Atoi(p)
|
||||
@@ -59,9 +76,9 @@ func main() {
|
||||
slog.Info("daemon scheduler is running")
|
||||
|
||||
// api
|
||||
s := api.NewServer(data, scheduler, port)
|
||||
s := api.NewServer(data, scheduler, c.Server.Address, port)
|
||||
|
||||
slog.Info("daemon listening to :" + p)
|
||||
slog.Info(fmt.Sprintf("daemon listening to %s:%s", c.Server.Address, p))
|
||||
if err := s.Server.ListenAndServe(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "failed to start server:", err.Error())
|
||||
os.Exit(1)
|
||||
|
||||
Reference in New Issue
Block a user