Merge pull request #8 from luclu7/add-socket-support

Add socket support
This commit is contained in:
Maël 2020-12-26 12:08:53 -04:00 committed by GitHub
commit 6f8f0ce7d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 22 deletions

3
.gitignore vendored
View file

@ -12,3 +12,6 @@ Icon
config.ini config.ini
log/* log/*
dist/* dist/*
# binary
sacrebleu-dns

View file

@ -38,24 +38,24 @@ You can show theses informations using ``./sacrebleu-dns -h``.
Variables names are case sensitives. Variables names are case sensitives.
|Variable name|Type|Example|Informations| |Variable name|Type|Example|Informations|
|--|--|--|--| |--|--|--|--|
| AppMode | string|``"production"``|Anything different than ``production`` will show debug messages | app_mode | string|``"production"``|Anything different than ``production`` will show debug messages
| App | Section | | App | Section |
|IP|string|``":"``|IP address on which the DNS server must listen. Blank to listen on all IPs |IP|string|``":"``|IP address on which the DNS server must listen. Blank to listen on all IPs
|Port|int|``5353``|Port on which the DNS server must listen |Port|int|``5353``|Port on which the DNS server must listen
|Logfile|bool|``true``|Enable or disable file logs. |Logfile|bool|``true``|Enable or disable file logs.
|Database|Section| |Database|Section|
|IP|string|``"127.0.0.1"``|SQL Database IP |Type|string|``"postgresql"``|SQL Database type. ``"postgresql"`` or ``"mysql"`` (anything different than ``"postgresql"`` will rollback to ``"mysql"``)
|Host|string|``"127.0.0.1"`` ``"/var/run/postgres"``|Can be either an IP or a path to a socket for Postgres
|Username|string|``"sacrebleu"``|SQL Database Username |Username|string|``"sacrebleu"``|SQL Database Username
|Password|string|``"superSecretPassword"``|SQL Database Password |Password|string|``"superSecretPassword"``|SQL Database Password (optional)
|Port|string|``"5432"``|SQL Database port (``"5432"`` for postgres or ``"3306"`` for MySQL by default) |Port|string|``"5432"``|SQL Database port (``"5432"`` for postgres or ``"3306"`` for MySQL by default)
|Db|string|``"sacrebleudatabase"``|SQL Database Name |Db|string|``"sacrebleudatabase"``|SQL Database Name
|Type|string|``"postgresql"``|SQL Database type. ``"postgresql"`` or ``"mysql"`` (anything different than ``"postgresql"`` will rollback to ``"mysql"``
|Redis|Section |Redis|Section
|IP|string|``"127.0.0.1"``|Redis Database IP |IP|string|``"127.0.0.1"``|Redis Database IP
|Password|string|``""``|Redis Database Password |Password|string|``""``|Redis Database Password
|Port|int|``6379``|Redis Database port |Port|int|``6379``|Redis Database port
|Db|int|``0``|Redis Database ID |DB|int|``0``|Redis Database ID
|Ttl|int|``10``|Redis Time To Live (in seconds) |TTL|int|``10``|Redis Time To Live (in seconds)
## What is working ## What is working
- Read records (stricts & wildcard) from MySQL - Read records (stricts & wildcard) from MySQL

View file

@ -1,4 +1,4 @@
AppMode = "production" #Anything != production will show DEBUG messages app_mode = "production" #Anything != production will show DEBUG messages
[App] [App]
IP = "" IP = ""
@ -7,16 +7,18 @@ Logfile = true
Logdir = "/var/log/" Logdir = "/var/log/"
[Database] [Database]
IP = "127.0.0.1" # Type can be either postgresql or mysql
Type = "postgresql"
# if type if postgres, you can also connect to the DB with a socket file
Host = "127.0.0.1" # can be either an IP address or a socket, it's often /var/run/postgresql/
Username = "sacrebleu" Username = "sacrebleu"
Password = "superSecretPassword" Password = "superSecretPassword"
Port = "3306" Port = "3306"
Db = "sacrebleudatabase" DB = "sacrebleudatabase"
Type = "mysql" #postgresql or mysql
[Redis] [Redis]
IP = "127.0.0.1" IP = "127.0.0.1"
Port = 6379 Port = 6379
Password = "" Password = ""
Db = 0 DB = 0
Ttl = 10 #In seconds TTL = 10 #In seconds

View file

@ -32,13 +32,13 @@ func SQLDatabase(conf *Conf) {
} }
if conf.Database.Type == "postgresql" { if conf.Database.Type == "postgresql" {
dsn := fmt.Sprintf("user=%s password=%s host=%s port=%s database=%s sslmode=disable", conf.Database.Username, conf.Database.Password, conf.Database.IP, conf.Database.Port, conf.Database.Db) dsn := fmt.Sprintf("user=%s password=%s host=%s port=%s database=%s sslmode=disable", conf.Database.Username, conf.Database.Password, conf.Database.Host, conf.Database.Port, conf.Database.Db)
db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{ db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{
Logger: logger.Default.LogMode(gormLogLevel), Logger: logger.Default.LogMode(gormLogLevel),
}) })
} else { } else {
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", conf.Database.Username, conf.Database.Password, conf.Database.IP, conf.Database.Port, conf.Database.Db) dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", conf.Database.Username, conf.Database.Password, conf.Database.Host, conf.Database.Port, conf.Database.Db)
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{ db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
Logger: logger.Default.LogMode(gormLogLevel), Logger: logger.Default.LogMode(gormLogLevel),

View file

@ -3,33 +3,33 @@ package utils
//App : Struct for App (dns server) configuration in the config.ini file //App : Struct for App (dns server) configuration in the config.ini file
type App struct { type App struct {
Port int Port int
IP string IP string `ini:"IP"`
Logdir string Logdir string
Logfile bool Logfile bool
} }
//Database : Struct for SQL Database configuration in the config.ini file //Database : Struct for SQL Database configuration in the config.ini file
type Database struct { type Database struct {
IP string Host string `ini:"Host"`
Port string Port string
Username string Username string
Password string Password string
Db string Db string `ini:"DB"`
Type string Type string
} }
//Redis : Struct for Redis Database configuration in the config.ini file //Redis : Struct for Redis Database configuration in the config.ini file
type Redis struct { type Redis struct {
IP string IP string `ini:"IP"`
Port int Port int
Password string Password string
Db int Db int `ini:"DB"`
TTL int TTL int `ini:"TTL"`
} }
//Conf : Struct for the whole config.ini file when it will be parsed by go-ini //Conf : Struct for the whole config.ini file when it will be parsed by go-ini
type Conf struct { type Conf struct {
AppMode string AppMode string `ini:"app_mode"`
App App
Database Database
Redis Redis