more work done

This commit is contained in:
ston1th 2021-03-06 16:51:36 +01:00
commit 6fda229a8e
23 changed files with 384 additions and 177 deletions

View file

@ -3,15 +3,15 @@ package api
import (
"context"
stdtls "crypto/tls"
"io/ioutil"
"log"
"net"
"net/http"
"path/filepath"
"time"
"git.giftfish.de/ston1th/haproxy-lb/pkg/alloc"
"git.giftfish.de/ston1th/haproxy-lb/pkg/api/types"
serverv1 "git.giftfish.de/ston1th/haproxy-lb/pkg/api/v1/server"
"git.giftfish.de/ston1th/haproxy-lb/pkg/config"
"git.giftfish.de/ston1th/haproxy-lb/pkg/db"
"github.com/go-logr/logr"
"github.com/gorilla/mux"
@ -23,14 +23,19 @@ type Server struct {
mux *mux.Router
log logr.Logger
Alloc *alloc.Alloc
DB *db.DB
cidrs []string
gw string
Auth []config.BasicAuth
init chan struct{}
stop chan struct{}
stopKeyReset chan struct{}
cert string
key string
laddr string
debug bool
}
type notFoundHandler struct {
@ -42,19 +47,21 @@ func (nf *notFoundHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
// NewHTTPServer returns a new HTTPServer
func NewServer(log logr.Logger) *Server {
func NewServer(c *config.Config, log logr.Logger) *Server {
s := &Server{
mux: mux.NewRouter(),
log: log.WithName("api"),
mux: mux.NewRouter(),
log: log.WithName("api"),
cidrs: c.VIP.VirtualIPs,
gw: c.VIP.Gateway,
Auth: c.Server.BasicAuth,
init: make(chan struct{}),
stop: make(chan struct{}),
stopKeyReset: make(chan struct{}),
cert: filepath.Join(core.C.DataDir, core.C.HTTP.Cert),
key: filepath.Join(core.C.DataDir, core.C.HTTP.Key),
laddr: net.JoinHostPort(core.C.HTTP.Address, core.C.HTTP.Port),
debug: core.C.Debug,
cert: c.Server.TLS.Cert,
key: c.Server.TLS.Key,
laddr: c.Server.Listen,
}
s.mux.NotFoundHandler = &notFoundHandler{s}
for _, v := range serverv1.Routes {
@ -64,11 +71,28 @@ func NewServer(log logr.Logger) *Server {
return s
}
func (s *Server) UpdateDB(db *db.DB) error {
s.db = db
if s.Alloc == nil {
alloc, err := alloc.NewAlloc(db, s.cidrs, s.gw)
if err != nil {
return err
}
s.alloc = alloc
close(s.init)
} else {
s.Alloc.UpdateDB(db)
}
return nil
}
func (s *Server) start() error {
l, err := tls.Listen("tcp", h.laddr, cfg)
<-s.init
l, err := net.Listen("tcp", h.laddr)
if err != nil {
return err
}
// TODO tls config
h.srv = &http.Server{
Handler: h.mux,
TLSConfig: cfg,