Improve configuration, including storage configuration and selection

This commit is contained in:
nemunaire 2020-07-07 20:13:50 +02:00
parent bb642a2e14
commit 2c0b5ddabe
6 changed files with 143 additions and 44 deletions

View File

@ -33,16 +33,24 @@ package config // import "happydns.org/config"
import (
"flag"
"fmt"
"git.happydns.org/happydns/storage"
)
func (o *Options) parseCLI() error {
func (o *Options) declareFlags() {
flag.StringVar(&o.DevProxy, "dev", o.DevProxy, "Proxify traffic to this host for static assets")
flag.StringVar(&o.AdminBind, "adminbind", o.AdminBind, "Bind port/socket for administration interface")
flag.StringVar(&o.AdminBind, "admin-bind", o.AdminBind, "Bind port/socket for administration interface")
flag.StringVar(&o.Bind, "bind", ":8081", "Bind port/socket")
flag.StringVar(&o.DSN, "dsn", o.DSN, "DSN to connect to the MySQL server")
flag.StringVar(&o.ExternalURL, "exernalurl", o.ExternalURL, "Begining of the URL, before the base, that should be used eg. in mails")
flag.StringVar(&o.ExternalURL, "externalurl", o.ExternalURL, "Begining of the URL, before the base, that should be used eg. in mails")
flag.StringVar(&o.BaseURL, "baseurl", o.BaseURL, "URL prepended to each URL")
flag.StringVar(&o.DefaultNameServer, "defaultns", o.DefaultNameServer, "Adress to the default name server")
flag.StringVar(&o.DefaultNameServer, "default-ns", o.DefaultNameServer, "Adress to the default name server")
flag.StringVar(&o.StorageEngine, "storage-engine", "leveldb", fmt.Sprintf("Select the storage engine between %v", storage.GetStorageEngines()))
// Others flags are declared in some other files likes sources, storages, ... when they need specials configurations
}
func (o *Options) parseCLI() error {
flag.Parse()
for _, conf := range flag.Args() {

View File

@ -32,13 +32,11 @@
package config // import "happydns.org/config"
import (
"fmt"
"flag"
"log"
"os"
"path"
"strings"
"git.happydns.org/happydns/storage/mysql"
)
type Options struct {
@ -47,8 +45,8 @@ type Options struct {
ExternalURL string
BaseURL string
DevProxy string
DSN string
DefaultNameServer string
StorageEngine string
}
func ConsolidateConfig() (opts *Options, err error) {
@ -58,10 +56,12 @@ func ConsolidateConfig() (opts *Options, err error) {
AdminBind: "./happydns.sock",
ExternalURL: "http://localhost:8081",
BaseURL: "/",
DSN: database.DSNGenerator(),
DefaultNameServer: "127.0.0.1:53",
StorageEngine: "leveldb",
}
opts.declareFlags()
// Establish a list of possible configuration file locations
configLocations := []string{
"happydns.conf",
@ -109,36 +109,14 @@ func ConsolidateConfig() (opts *Options, err error) {
func (o *Options) parseLine(line string) (err error) {
fields := strings.SplitN(line, "=", 2)
key := strings.TrimSpace(fields[0])
orig_key := strings.TrimSpace(fields[0])
value := strings.TrimSpace(fields[1])
key = strings.TrimPrefix(key, "HAPPYDNS_")
key = strings.Replace(key, "_", "", -1)
key = strings.ToUpper(key)
key := strings.TrimPrefix(orig_key, "HAPPYDNS_")
key = strings.Replace(key, "_", "-", -1)
key = strings.ToLower(key)
switch key {
case "DEVPROXY":
err = parseString(&o.DevProxy, value)
}
err = flag.Set(key, value)
return
}
func parseString(store *string, value string) error {
*store = value
return nil
}
func parseBool(store *bool, value string) error {
value = strings.ToLower(value)
if value == "1" || value == "yes" || value == "true" || value == "on" {
*store = true
} else if value == "" || value == "0" || value == "no" || value == "false" || value == "off" {
*store = false
} else {
return fmt.Errorf("%s is not a valid bool value", value)
}
return nil
}

19
main.go
View File

@ -49,10 +49,11 @@ import (
"git.happydns.org/happydns/api"
"git.happydns.org/happydns/config"
"git.happydns.org/happydns/storage"
leveldb "git.happydns.org/happydns/storage/leveldb"
_ "git.happydns.org/happydns/sources/ddns"
_ "git.happydns.org/happydns/sources/ovh"
_ "git.happydns.org/happydns/storage/leveldb"
)
type ResponseWriterPrefix struct {
@ -108,13 +109,17 @@ func main() {
log.Fatal(err)
}
// Initialize contents
log.Println("Opening database...")
if store, err := leveldb.NewLevelDBStorage("happydns.db"); err != nil {
log.Fatal("Cannot open the database: ", err)
// Initialize storage
if s, ok := storage.StorageEngines[opts.StorageEngine]; !ok {
log.Fatal(fmt.Sprintf("Unexistant storage engine: %q, please select one between: %v", opts.StorageEngine, storage.GetStorageEngines()))
} else {
defer store.Close()
storage.MainStore = store
log.Println("Opening database...")
if store, err := s(); err != nil {
log.Fatal("Cannot open the database: ", err)
} else {
defer store.Close()
storage.MainStore = store
}
}
log.Println("Do database migrations...")

50
storage/leveldb/config.go Normal file
View File

@ -0,0 +1,50 @@
// Copyright or © or Copr. happyDNS (2020)
//
// contact@happydns.org
//
// This software is a computer program whose purpose is to provide a modern
// interface to interact with DNS systems.
//
// This software is governed by the CeCILL license under French law and abiding
// by the rules of distribution of free software. You can use, modify and/or
// redistribute the software under the terms of the CeCILL license as
// circulated by CEA, CNRS and INRIA at the following URL
// "http://www.cecill.info".
//
// As a counterpart to the access to the source code and rights to copy, modify
// and redistribute granted by the license, users are provided only with a
// limited warranty and the software's author, the holder of the economic
// rights, and the successive licensors have only limited liability.
//
// In this respect, the user's attention is drawn to the risks associated with
// loading, using, modifying and/or developing or reproducing the software by
// the user in light of its specific status of free software, that may mean
// that it is complicated to manipulate, and that also therefore means that it
// is reserved for developers and experienced professionals having in-depth
// computer knowledge. Users are therefore encouraged to load and test the
// software's suitability as regards their requirements in conditions enabling
// the security of their systems and/or data to be ensured and, more generally,
// to use and operate it in the same conditions as regards security.
//
// The fact that you are presently reading this means that you have had
// knowledge of the CeCILL license and that you accept its terms.
package database
import (
"flag"
"git.happydns.org/happydns/storage"
)
var path string
func init() {
storage.StorageEngines["leveldb"] = Instantiate
flag.StringVar(&path, "leveldb-path", "happydns.db", "Path to the LevelDB Database")
}
func Instantiate() (storage.Storage, error) {
return NewLevelDBStorage(path)
}

View File

@ -32,9 +32,20 @@
package database
import (
"flag"
"os"
"git.happydns.org/happydns/storage"
)
var dsn string
func init() {
storage.StorageEngines["mysql"] = Instantiate
flag.StringVar(&dsn, "mysql-dsn", DSNGenerator(), "DSN to connect to the MySQL server")
}
// DSNGenerator returns DSN filed with values from environment
func DSNGenerator() string {
db_user := "happydns"
@ -60,3 +71,7 @@ func DSNGenerator() string {
return db_user + ":" + db_password + "@" + db_host + "/" + db_db
}
func Instantiate() (storage.Storage, error) {
return NewMySQLStorage(dsn)
}

View File

@ -1,3 +1,46 @@
// Copyright or © or Copr. happyDNS (2020)
//
// contact@happydns.org
//
// This software is a computer program whose purpose is to provide a modern
// interface to interact with DNS systems.
//
// This software is governed by the CeCILL license under French law and abiding
// by the rules of distribution of free software. You can use, modify and/or
// redistribute the software under the terms of the CeCILL license as
// circulated by CEA, CNRS and INRIA at the following URL
// "http://www.cecill.info".
//
// As a counterpart to the access to the source code and rights to copy, modify
// and redistribute granted by the license, users are provided only with a
// limited warranty and the software's author, the holder of the economic
// rights, and the successive licensors have only limited liability.
//
// In this respect, the user's attention is drawn to the risks associated with
// loading, using, modifying and/or developing or reproducing the software by
// the user in light of its specific status of free software, that may mean
// that it is complicated to manipulate, and that also therefore means that it
// is reserved for developers and experienced professionals having in-depth
// computer knowledge. Users are therefore encouraged to load and test the
// software's suitability as regards their requirements in conditions enabling
// the security of their systems and/or data to be ensured and, more generally,
// to use and operate it in the same conditions as regards security.
//
// The fact that you are presently reading this means that you have had
// knowledge of the CeCILL license and that you accept its terms.
package storage
var MainStore Storage
type StorageInstanciation func() (Storage, error)
var StorageEngines = map[string]StorageInstanciation{}
func GetStorageEngines() (se []string) {
for k := range StorageEngines {
se = append(se, k)
}
return
}