Listen in TCP & UDP.

This commit is contained in:
Mael GRAMAIN 2021-02-25 17:26:52 -04:00
parent 8e5bf6ebdb
commit dcd5da2cfb
1 changed files with 24 additions and 7 deletions

31
main.go
View File

@ -3,7 +3,10 @@ package main
import ( import (
"database/sql" "database/sql"
"flag" "flag"
"os"
"os/signal"
"strconv" "strconv"
"syscall"
"github.com/miekg/dns" "github.com/miekg/dns"
"github.com/outout14/sacrebleu-api/api/types" "github.com/outout14/sacrebleu-api/api/types"
@ -46,13 +49,27 @@ func main() {
types.SQLMigrate(db) types.SQLMigrate(db)
} }
//Start the DNS server //Start the UDP listener
server := &dns.Server{Addr: conf.App.IP + ":" + strconv.Itoa(conf.App.Port), Net: "tcp"} //define the server go func() {
logrus.WithFields(logrus.Fields{"ip": conf.App.IP, "port": conf.App.Port}).Infof("SERVER : Started") //log server := &dns.Server{Addr: conf.App.IP + ":" + strconv.Itoa(conf.App.Port), Net: "udp"} //define the server
logrus.WithFields(logrus.Fields{"XfrIPs": conf.DNS.XfrIPs}).Debug("") logrus.WithFields(logrus.Fields{"ip": conf.App.IP, "port": conf.App.Port}).Infof("SERVER : Started UDP listener") //log
if err := server.ListenAndServe(); err != nil {
logrus.Fatalf("Failed to set udp listener %s\n", err.Error())
}
}()
err = server.ListenAndServe() //start it //Start the TCP listener
utils.CheckErr(err) go func() {
server := &dns.Server{Addr: conf.App.IP + ":" + strconv.Itoa(conf.App.Port), Net: "tcp"} //define the server
logrus.WithFields(logrus.Fields{"ip": conf.App.IP, "port": conf.App.Port}).Infof("SERVER : Started TCP listener") //log
if err := server.ListenAndServe(); err != nil {
logrus.Fatalf("Failed to set udp listener %s\n", err.Error())
}
}()
sig := make(chan os.Signal)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
s := <-sig
logrus.Infof("Signal (%v) received, stopping\n", s)
defer server.Shutdown() //shut down on application closing
} }