Initialise logger in main not server
This commit is contained in:
parent
6c853bbaaa
commit
3c06b3c741
3 changed files with 59 additions and 26 deletions
|
@ -24,9 +24,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Host string
|
LogLevel string
|
||||||
Plain PlainConfig
|
Host string
|
||||||
TLS TLSConfig
|
Plain PlainConfig
|
||||||
|
TLS TLSConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
type PlainConfig struct {
|
type PlainConfig struct {
|
||||||
|
|
|
@ -19,28 +19,69 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
"forge.monodon.me/Gnarwhal/diodemail/smtp"
|
"forge.monodon.me/Gnarwhal/diodemail/smtp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const Version = "1.0.0"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var cert_path string
|
log.Logger = zerolog.
|
||||||
var version bool
|
New(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339}).
|
||||||
flag.StringVar(&cert_path, "config", "/etc/diodemail/config.json", "Path to config file")
|
With().
|
||||||
flag.BoolVar(&version, "version", false, "Print version")
|
Timestamp().
|
||||||
|
Logger().
|
||||||
|
Level(zerolog.ErrorLevel)
|
||||||
|
|
||||||
|
var config_path string
|
||||||
|
var log_level string
|
||||||
|
|
||||||
|
flag.StringVar(
|
||||||
|
&config_path,
|
||||||
|
"config-file",
|
||||||
|
"/etc/diodemail/config.json",
|
||||||
|
"Path to config file",
|
||||||
|
)
|
||||||
|
flag.StringVar(
|
||||||
|
&log_level,
|
||||||
|
"log-level",
|
||||||
|
"",
|
||||||
|
"Maximum log level to log",
|
||||||
|
)
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
if version {
|
config, err := LoadConfig(config_path)
|
||||||
fmt.Println("diodemail version 1.0.0")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
config, err := LoadConfig(cert_path)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal().Msgf("%v", err)
|
||||||
}
|
}
|
||||||
|
if log_level == "" {
|
||||||
|
if config.LogLevel != "" {
|
||||||
|
log_level = config.LogLevel
|
||||||
|
} else {
|
||||||
|
log_level = "info"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
parsed_log_level, err := zerolog.ParseLevel(log_level)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal().Msgf("%v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Logger = zerolog.
|
||||||
|
New(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339}).
|
||||||
|
With().
|
||||||
|
Timestamp().
|
||||||
|
Logger().
|
||||||
|
Level(parsed_log_level)
|
||||||
|
log.Info().Msgf("Starting diodemail v%v", Version)
|
||||||
|
log.Info().Msgf("Loaded config from: %v", config_path)
|
||||||
|
|
||||||
var plain_config *smtp.PlainConfig
|
var plain_config *smtp.PlainConfig
|
||||||
if config.Plain.Enabled {
|
if config.Plain.Enabled {
|
||||||
|
@ -56,7 +97,7 @@ func main() {
|
||||||
config.TLS.PrivateKeyPath,
|
config.TLS.PrivateKeyPath,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Error().Msgf("Failed to load TLS config: %v", err)
|
||||||
} else {
|
} else {
|
||||||
tls_config = &smtp.TLSConfig {
|
tls_config = &smtp.TLSConfig {
|
||||||
config.TLS.Port,
|
config.TLS.Port,
|
||||||
|
@ -71,6 +112,6 @@ func main() {
|
||||||
tls_config,
|
tls_config,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal().Msgf("%v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,10 +22,8 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"os"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -62,13 +60,6 @@ func handle(connection net.Conn, host string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Run(host string, plain_config *PlainConfig, tls_config *TLSConfig) error {
|
func Run(host string, plain_config *PlainConfig, tls_config *TLSConfig) error {
|
||||||
log.Logger = zerolog.
|
|
||||||
New(zerolog.ConsoleWriter{Out: os.Stderr}).
|
|
||||||
With().
|
|
||||||
Timestamp().
|
|
||||||
Logger().
|
|
||||||
Level(zerolog.TraceLevel)
|
|
||||||
|
|
||||||
var wait_group sync.WaitGroup
|
var wait_group sync.WaitGroup
|
||||||
if plain_config != nil {
|
if plain_config != nil {
|
||||||
listener, err := net.Listen("tcp", fmt.Sprintf("%v:%v", host, plain_config.Port))
|
listener, err := net.Listen("tcp", fmt.Sprintf("%v:%v", host, plain_config.Port))
|
||||||
|
|
Loading…
Reference in a new issue