dim/main.go

73 lines
1.5 KiB
Go
Raw Normal View History

2021-04-21 21:40:55 +02:00
package main
import (
"database/sql"
"flag"
"log"
"net/http"
"strings"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
"github.com/pelletier/go-toml"
)
var (
configPath = flag.String("config", "dim.conf", "path to the config file")
2021-04-21 21:40:55 +02:00
)
type (
Config struct {
Listen string `toml:"listen"`
DB struct {
Type string `toml:"type"`
Connection string `toml:"conn"`
} `toml:"db"`
}
)
func main() {
flag.Parse()
cfg := Config{}
raw, err := toml.LoadFile(*configPath)
if err != nil {
log.Fatalf("could not load config file '%s': %s", *configPath, err)
return
}
if err := raw.Unmarshal(&cfg); err != nil {
log.Fatalf("could not parse config file '%s': %s", *configPath, err)
return
}
if cfg.DB.Type != "mysql" && cfg.DB.Type != "postgres" {
log.Fatalf(
"unknown database type '%s' in config '%s'. Allowed is 'mysql' and 'postgres'",
2021-04-21 21:40:55 +02:00
cfg.DB.Type,
*configPath,
)
return
}
if strings.Trim(cfg.DB.Connection, " \t\n\r") == "" {
log.Fatalf("no database connection string in config '%s' set", *configPath)
return
}
db, err := sql.Open(cfg.DB.Type, cfg.DB.Connection)
if err != nil {
log.Fatalf("could not open database connection: %s", err)
return
}
s, err := NewServer(db)
if err != nil {
log.Fatalf("could not create server instance: %s", err)
return
}
s.Register("zone_create", zoneCreate)
s.Register("zone_list", zoneList)
2021-04-21 21:40:55 +02:00
http.HandleFunc("/dim", s.Handle)
log.Fatal(http.ListenAndServe(cfg.Listen, nil))
}