soju/cmd/jounce/main.go

69 lines
1.4 KiB
Go
Raw Normal View History

2020-02-04 09:46:22 +00:00
package main
import (
2020-02-18 13:10:23 +00:00
"crypto/tls"
"flag"
2020-02-04 09:46:22 +00:00
"log"
"net"
"git.sr.ht/~emersion/jounce"
2020-02-18 13:10:23 +00:00
"git.sr.ht/~emersion/jounce/config"
2020-02-04 09:46:22 +00:00
)
func main() {
2020-02-18 13:10:23 +00:00
var addr, configPath string
flag.StringVar(&addr, "listen", "", "listening address")
flag.StringVar(&configPath, "config", "", "path to configuration file")
flag.Parse()
2020-02-04 09:46:22 +00:00
2020-02-18 13:10:23 +00:00
var cfg *config.Server
if configPath != "" {
var err error
cfg, err = config.Load(configPath)
if err != nil {
log.Fatalf("failed to load config file: %v", err)
}
} else {
cfg = config.Defaults()
}
if addr != "" {
cfg.Addr = addr
}
var ln net.Listener
if cfg.TLS != nil {
cert, err := tls.LoadX509KeyPair(cfg.TLS.CertPath, cfg.TLS.KeyPath)
if err != nil {
log.Fatalf("failed to load TLS certificate and key: %v", err)
}
tlsCfg := &tls.Config{Certificates: []tls.Certificate{cert}}
ln, err = tls.Listen("tcp", cfg.Addr, tlsCfg)
if err != nil {
log.Fatalf("failed to start TLS listener: %v", err)
}
} else {
var err error
ln, err = net.Listen("tcp", addr)
if err != nil {
log.Fatalf("failed to start listener: %v", err)
}
2020-02-04 09:46:22 +00:00
}
2020-02-07 10:36:42 +00:00
srv := jounce.NewServer()
2020-02-06 15:03:07 +00:00
// TODO: load from config/DB
2020-02-18 13:10:23 +00:00
srv.Hostname = cfg.Hostname
2020-02-07 10:36:42 +00:00
srv.Upstreams = []jounce.Upstream{{
Addr: "chat.freenode.net:6697",
Nick: "jounce",
Username: "jounce",
Realname: "jounce",
Channels: []string{"#jounce"},
}}
2020-02-04 10:25:53 +00:00
2020-02-18 13:10:23 +00:00
log.Printf("server listening on %q", cfg.Addr)
2020-02-07 10:36:42 +00:00
go srv.Run()
log.Fatal(srv.Serve(ln))
2020-02-04 09:46:22 +00:00
}