some work done
This commit is contained in:
parent
a735b9cc5e
commit
09fe1cd163
13 changed files with 174 additions and 45 deletions
|
|
@ -1,4 +1,4 @@
|
|||
package server
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
|
@ -10,6 +10,9 @@ import (
|
|||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"git.giftfish.de/ston1th/haproxy-lb/pkg/api/types"
|
||||
serverv1 "git.giftfish.de/ston1th/haproxy-lb/pkg/api/v1/server"
|
||||
|
||||
"github.com/go-logr/logr"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
|
@ -30,6 +33,14 @@ type Server struct {
|
|||
debug bool
|
||||
}
|
||||
|
||||
type notFoundHandler struct {
|
||||
s *Server
|
||||
}
|
||||
|
||||
func (nf *notFoundHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
types.NewContext(w, r, nf.s).NotFound()
|
||||
}
|
||||
|
||||
// NewHTTPServer returns a new HTTPServer
|
||||
func NewServer(log logr.Logger) *Server {
|
||||
s := &Server{
|
||||
|
|
@ -46,7 +57,7 @@ func NewServer(log logr.Logger) *Server {
|
|||
debug: core.C.Debug,
|
||||
}
|
||||
s.mux.NotFoundHandler = ¬FoundHandler{s}
|
||||
for _, v := range routes {
|
||||
for _, v := range serverv1.Routes {
|
||||
s.mux.HandleFunc(v.Path, s.contextWrapper(v.Handler)).Methods(v.Methods...)
|
||||
}
|
||||
s.start()
|
||||
|
|
@ -73,9 +84,9 @@ func (s *Server) start() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) contextWrapper(h ctxHandler) http.HandlerFunc {
|
||||
func (s *Server) contextWrapper(h types.CtxHandler) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
h(newContext(w, r, s))
|
||||
h(types.NewContext(w, r, s))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1,21 +1,22 @@
|
|||
package server
|
||||
package types
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-logr/logr"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
func newContext(w http.ResponseWriter, r *http.Request, srv *Server) *Context {
|
||||
func NewContext(w http.ResponseWriter, r *http.Request, log logr.Logger) *Context {
|
||||
h := w.Header()
|
||||
h.Set("Content-Type", "application/json")
|
||||
return &Context{
|
||||
Status: http.StatusOK,
|
||||
Request: r,
|
||||
Response: w,
|
||||
s: srv,
|
||||
log: log,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -26,7 +27,7 @@ type Context struct {
|
|||
|
||||
Request *http.Request
|
||||
Response http.ResponseWriter
|
||||
s *Server
|
||||
log logr.Logger
|
||||
}
|
||||
|
||||
// Method returns the request method
|
||||
|
|
@ -57,7 +58,7 @@ func (c *Context) Var(name string) (ret string) {
|
|||
}
|
||||
|
||||
func (c *Context) log() {
|
||||
c.s.log.Info("access",
|
||||
c.log.Info("access",
|
||||
"addr", c.Request.RemoteAddr,
|
||||
"method", c.Request.Method,
|
||||
"url", c.Request.URL,
|
||||
|
|
@ -100,4 +101,4 @@ func (c *Context) HTTPErr(err string, status int) {
|
|||
c.log()
|
||||
}
|
||||
|
||||
type ctxHandler func(*Context)
|
||||
type CtxHandler func(*Context)
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package api
|
||||
package types
|
||||
|
||||
type Route struct {
|
||||
Path string
|
||||
|
|
@ -5,13 +5,9 @@ healthCheckNodePort
|
|||
|
||||
*/
|
||||
|
||||
type LBConfig struct {
|
||||
LoadBalancers []LoadBalancer
|
||||
}
|
||||
|
||||
type LoadBalancer struct {
|
||||
Name string `json:"name"`
|
||||
IP string `json:"ip"`
|
||||
IP string `json:"ip,omitempty"`
|
||||
Options Options `json:"options,omitempty"`
|
||||
Ports []Port `json:"ports,omitempty"`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,18 +3,12 @@ package server
|
|||
import (
|
||||
//"golang.org/x/crypto/bcrypt"
|
||||
"net/http"
|
||||
|
||||
"git.giftfish.de/ston1th/haproxy-lb/pkg/api/types"
|
||||
)
|
||||
|
||||
type notFoundHandler struct {
|
||||
s *Server
|
||||
}
|
||||
|
||||
func (nf *notFoundHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
newContext(w, r, nf.s).NotFound()
|
||||
}
|
||||
|
||||
func authHandler(h ctxHandler) ctxHandler {
|
||||
return func(ctx *Context) {
|
||||
func authHandler(h types.CtxHandler) types.CtxHandler {
|
||||
return func(ctx *types.Context) {
|
||||
// TODO
|
||||
// test:123456
|
||||
// test:$2b$10$zNfGTAQ94vifj2wtGsv1W.yZRe4/rDBzQixpB6FbrTed4BnHuNqBS
|
||||
|
|
@ -23,7 +17,7 @@ func authHandler(h ctxHandler) ctxHandler {
|
|||
}
|
||||
}
|
||||
|
||||
func healthzHandler(ctx *Context) {
|
||||
func healthzHandler(ctx *types.Context) {
|
||||
// TODO maybe report etcd/raft stats
|
||||
ctx.OK()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,13 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"git.giftfish.de/ston1th/haproxy-lb/pkg/api"
|
||||
"git.giftfish.de/ston1th/haproxy-lb/pkg/api/types"
|
||||
schemav1 "git.giftfish.de/ston1th/haproxy-lb/pkg/api/v1/schema"
|
||||
)
|
||||
|
||||
const v1 = "/" + schemav1.Version
|
||||
|
||||
var routes = []api.Route{
|
||||
var routes = []types.Route{
|
||||
{
|
||||
"/healthz",
|
||||
healthzHandler,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue