diodemail/cmd/server/main.go

113 lines
2.5 KiB
Go
Raw Normal View History

/* diodemail - send-only smtp server
* Copyright (c) 2024 Gnarwhal
*
2024-10-03 15:12:20 +00:00
* This file is part of diodemail.
*
2024-10-03 15:12:20 +00:00
* diodemail is free software: you can redistribute it and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
2024-10-03 15:12:20 +00:00
* diodemail is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
2024-10-03 15:12:20 +00:00
* diodemail. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"flag"
"crypto/tls"
2024-10-02 03:29:19 +00:00
"os"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"forge.monodon.me/Gnarwhal/diodemail/smtp"
)
2024-10-02 03:29:19 +00:00
const Version = "1.0.0"
func main() {
2024-10-02 03:29:19 +00:00
log.Logger = zerolog.
2024-10-02 03:35:30 +00:00
New(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.DateTime}).
2024-10-02 03:29:19 +00:00
With().
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()
2024-10-02 03:29:19 +00:00
config, err := LoadConfig(config_path)
if err != nil {
log.Fatal().Msgf("%v", err)
}
if log_level == "" {
if config.LogLevel != "" {
log_level = config.LogLevel
2024-10-02 03:29:19 +00:00
} else {
log_level = "info"
}
2024-10-02 02:51:32 +00:00
}
2024-10-02 03:29:19 +00:00
parsed_log_level, err := zerolog.ParseLevel(log_level)
if err != nil {
2024-10-02 03:29:19 +00:00
log.Fatal().Msgf("%v", err)
}
2024-10-02 03:29:19 +00:00
log.Logger = zerolog.
2024-10-02 03:35:30 +00:00
New(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.DateTime}).
2024-10-02 03:29:19 +00:00
With().
Timestamp().
Logger().
Level(parsed_log_level)
log.Info().Msgf("Starting diodemail v%v", Version)
log.Info().Msgf("Loaded config from: %v", config_path)
if config.CertPath == "" || config.PrivateKeyPath == "" {
log.Fatal().Msgf(
"Must provide CertPath (got '%v') and PrivateKeyPath (got '%v')",
config.CertPath,
config.PrivateKeyPath,
)
}
certificate, err := tls.LoadX509KeyPair(
config.CertPath,
config.PrivateKeyPath,
)
if err != nil {
log.Fatal().Msgf("Failed to load TLS config: %v", err)
}
err = smtp.Run(
config.Host,
config.PasswordHash,
config.Plain,
config.TLS,
tls.Config{Certificates: []tls.Certificate{certificate}},
2024-10-02 00:28:14 +00:00
)
if err != nil {
2024-10-02 03:29:19 +00:00
log.Fatal().Msgf("%v", err)
}
}