1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2025-12-17 14:37:02 +00:00

Create a Server structure and start organizing its configuration

This is in preparation for the tests, which will come soon.
This commit is contained in:
Alberto Bertogli
2015-10-26 00:54:58 +00:00
parent f5cab62c1e
commit 5978c96fd6

View File

@@ -13,21 +13,12 @@ import (
"net/mail" "net/mail"
"net/textproto" "net/textproto"
"strings" "strings"
"time"
"github.com/golang/glog" "github.com/golang/glog"
"golang.org/x/net/trace" "golang.org/x/net/trace"
) )
func main() {
flag.Parse()
monAddr := ":1099"
glog.Infof("Monitoring HTTP server listening on %s", monAddr)
go http.ListenAndServe(monAddr, nil)
ListenAndServe()
}
const ( const (
// TODO: get this via config/dynamically. It's only used for show. // TODO: get this via config/dynamically. It's only used for show.
hostname = "charqui.com.ar" hostname = "charqui.com.ar"
@@ -36,41 +27,95 @@ const (
maxDataSize = 52428800 maxDataSize = 52428800
) )
func getTLSConfig() (*tls.Config, error) { func main() {
flag.Parse()
monAddr := ":1099"
glog.Infof("Monitoring HTTP server listening on %s", monAddr)
go http.ListenAndServe(monAddr, nil)
s := NewServer(hostname)
s.AddCerts(".cert.pem", ".key.pem")
s.AddAddr(":1025")
s.ListenAndServe()
}
type Server struct {
// Certificate and key pairs.
certs, keys []string
// Addresses.
addrs []string
// Main hostname, used for display only.
hostname string
// TLS config.
tlsConfig *tls.Config
}
func NewServer(hostname string) *Server {
return &Server{
hostname: hostname,
}
}
func (s *Server) AddCerts(cert, key string) {
s.certs = append(s.certs, cert)
s.keys = append(s.keys, key)
}
func (s *Server) AddAddr(a string) {
s.addrs = append(s.addrs, a)
}
func (s *Server) getTLSConfig() (*tls.Config, error) {
var err error var err error
conf := &tls.Config{} conf := &tls.Config{}
// TODO: Get these from the configuration (we have to support many, not conf.Certificates = make([]tls.Certificate, len(s.certs))
// just 1 like here). for i := 0; i < len(s.certs); i++ {
conf.Certificates = make([]tls.Certificate, 1) conf.Certificates[i], err = tls.LoadX509KeyPair(s.certs[i], s.keys[i])
conf.Certificates[0], err = tls.LoadX509KeyPair(".cert.pem", ".key.pem") if err != nil {
if err != nil { return nil, fmt.Errorf("Error loading client certificate: %v", err)
return nil, fmt.Errorf("Error loading client certificate: %v", err) }
} }
conf.BuildNameToCertificate() conf.BuildNameToCertificate()
return conf, nil return conf, nil
} }
func (s *Server) ListenAndServe() {
var err error
func ListenAndServe() {
// Configure TLS. // Configure TLS.
tlsConfig, err := getTLSConfig() s.tlsConfig, err = s.getTLSConfig()
if err != nil { if err != nil {
glog.Fatalf("Error loading TLS config: %v", err) glog.Fatalf("Error loading TLS config: %v", err)
} }
// Listen. for _, addr := range s.addrs {
addr := ":1025" // Listen.
l, err := net.Listen("tcp", addr) l, err := net.Listen("tcp", addr)
if err != nil { if err != nil {
glog.Fatalf("Error listening: %v", err) glog.Fatalf("Error listening: %v", err)
}
defer l.Close()
glog.Infof("Server listening on %s", addr)
// Serve.
go s.serve(l)
} }
defer l.Close()
glog.Infof("Server listening on %s", addr) // Never return. If the serve goroutines have problems, they will abort
// execution.
for {
time.Sleep(24 * time.Hour)
}
}
// Serve. func (s *Server) serve(l net.Listener) {
for { for {
conn, err := l.Accept() conn, err := l.Accept()
if err != nil { if err != nil {
@@ -80,7 +125,7 @@ func ListenAndServe() {
sc := &Conn{ sc := &Conn{
netconn: conn, netconn: conn,
tc: textproto.NewConn(conn), tc: textproto.NewConn(conn),
tlsConfig: tlsConfig, tlsConfig: s.tlsConfig,
} }
go sc.Handle() go sc.Handle()
} }