aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-04-03 21:13:26 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-04-03 21:13:26 +0200
commit0fd9f4442d683944fda5d3ec2204fdd432371e82 (patch)
treebe179e1f071a36128d71763a2fd3b3823b4cbc6f
parent566adfae24bc26a444918df570f90a0ba63c669a (diff)
Tests passed
-rw-r--r--rpc.go20
-rw-r--r--server.go71
2 files changed, 47 insertions, 44 deletions
diff --git a/rpc.go b/rpc.go
index 29a5a93..dc41be4 100644
--- a/rpc.go
+++ b/rpc.go
@@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"net/rpc"
- "net/url"
"os"
)
@@ -45,27 +44,12 @@ func DialRPC(server string) (*rpc.Client, error) {
// Add adds a new route
func (s *GoXY) Add(e Entry, _ *struct{}) error {
- r, err := NewRoute(e)
- if err != nil {
- return err
- }
- defer s.server.Save(s.server.DataFile)
- s.server.Routes[r.ServerName.Host] = r
- return s.server.Update()
+ return s.server.AddEntry(e)
}
// Del removes a route
func (s *GoXY) Del(host string, _ *struct{}) error {
- h, err := url.Parse(host)
- if err != nil {
- return err
- }
- if h.Host == "" {
- return ErrEmptyHost
- }
- delete(s.server.Routes, h.Host)
- s.server.Save(s.server.DataFile)
- return s.server.Update()
+ return s.server.DelEntry(host)
}
// List routes
diff --git a/server.go b/server.go
index 271edfa..e686fe4 100644
--- a/server.go
+++ b/server.go
@@ -20,29 +20,23 @@ func NewServer(dataFile, listenWWW, listenTLS, listenRPC string) (*Server, error
if listenRPC == "" {
listenRPC = RPCPort
}
- r := make(Routes)
server := &Server{
- DataFile: dataFile,
- Routes: r,
- wwwServer: http.Server{
- Addr: listenWWW,
- },
- tlsServer: http.Server{
- Addr: listenTLS,
- TLSConfig: &tls.Config{
- GetCertificate: r.GetCertificate,
- },
- },
- rpcServer: http.Server{
- Addr: listenRPC,
- },
+ DataFile: dataFile,
+ Routes: make(Routes),
+ Entries: make(Entries),
+ wwwServer: http.Server{Addr: listenWWW},
+ tlsServer: http.Server{Addr: listenTLS},
+ rpcServer: http.Server{Addr: listenRPC},
+ }
+ server.tlsServer.TLSConfig = &tls.Config{
+ GetCertificate: server.GetCertificate,
}
if dataFile != "" {
server.Load(dataFile)
}
RegisterRPC(server)
- http.Handle("/debug/route", server.Routes)
- return server, server.Update()
+ http.Handle("/debug/route", server)
+ return server, server.UpdateMux()
}
func NewRedirect(host string) http.Handler {
@@ -54,23 +48,21 @@ func NewReverseProxy(target *url.URL) *httputil.ReverseProxy {
}
// Update routes from in-memory state
-func (s *Server) Update() error {
+func (s *Server) UpdateMux() error {
wwwMux := http.NewServeMux()
tlsMux := http.NewServeMux()
- for _, v := range s.Routes {
- host := v.ServerName.Host + v.ServerName.Path
- up := v.Upstream
- switch v.ServerName.Scheme {
+ for host, r := range s.Routes {
+ switch r.ServerName.Scheme {
case "http", "":
- wwwMux.Handle(host, NewReverseProxy(up))
+ wwwMux.Handle(host, NewReverseProxy(r.Upstream))
case "https":
wwwMux.Handle(host, NewRedirect("https://"+host))
- tlsMux.Handle(host, NewReverseProxy(up))
+ tlsMux.Handle(host, NewReverseProxy(r.Upstream))
case "ws":
- wwwMux.Handle(host, NewWebSocketProxy(up))
+ wwwMux.Handle(host, NewWebSocketProxy(r.Upstream))
case "wss":
wwwMux.Handle(host, NewRedirect("wss://"+host))
- tlsMux.Handle(host, NewWebSocketProxy(up))
+ tlsMux.Handle(host, NewWebSocketProxy(r.Upstream))
}
}
s.wwwServer.Handler = wwwMux
@@ -78,6 +70,33 @@ func (s *Server) Update() error {
return nil
}
+func (s *Server) AddEntry(e Entry) error {
+ r, err := NewRoute(e)
+ if err != nil {
+ return err
+ }
+ defer s.Save(s.DataFile)
+ host := r.ServerName.Host + r.ServerName.Path
+ s.Entries[host] = e
+ s.Routes[host] = r
+ return s.UpdateMux()
+}
+
+func (s *Server) DelEntry(host string) error {
+ h, err := url.Parse(host)
+ if err != nil {
+ return err
+ }
+ if h.Path == "" {
+ h.Path = "/"
+ }
+ host = h.Host + h.Path
+ defer s.Save(s.DataFile)
+ delete(s.Entries, host)
+ delete(s.Routes, host)
+ return s.UpdateMux()
+}
+
func (s *Server) Start() error {
errc := make(chan error)
go func() { errc <- s.wwwServer.ListenAndServe() }()