aboutsummaryrefslogtreecommitdiff
path: root/server.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-03-31 14:43:29 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-03-31 14:43:29 +0200
commit7cb7f7d4d90714d50331c68e97fc5169c4f67991 (patch)
tree37f04d81512323ee4aa20262396b7dfe5d6f4c58 /server.go
parent56b132f7dbd9668426d934a6feaa70f3252ed040 (diff)
Rewrite Test, pass HTTP
Diffstat (limited to 'server.go')
-rw-r--r--server.go52
1 files changed, 22 insertions, 30 deletions
diff --git a/server.go b/server.go
index e396d76..9ef2204 100644
--- a/server.go
+++ b/server.go
@@ -4,8 +4,6 @@ import (
"crypto/tls"
"net/http"
"net/http/httputil"
- "net/url"
- "strings"
)
type Server struct {
@@ -17,20 +15,18 @@ type Server struct {
rpcServer http.Server
}
-func NewServer(dataFile, listen, listenTLS, listenRPC string) (*Server, error) {
- sni := make(SNI)
-
+func NewServer(dataFile, listenWWW, listenTLS, listenRPC string) (*Server, error) {
+ r := make(Route)
server := &Server{
DataFile: dataFile,
- SNI: sni,
- Route: make(Route),
+ Route: r,
wwwServer: http.Server{
- Addr: listen,
+ Addr: listenWWW,
},
tlsServer: http.Server{
Addr: listenTLS,
TLSConfig: &tls.Config{
- GetCertificate: sni.GetCertificate,
+ GetCertificate: r.GetCertificate,
},
},
rpcServer: http.Server{
@@ -47,30 +43,26 @@ func NewServer(dataFile, listen, listenTLS, listenRPC string) (*Server, error) {
// Update routes from in-memory state
func (s *Server) Update() error {
- httpMux := http.NewServeMux()
- for k, v := range s.Route {
- if v.Cert != nil && v.Key != nil {
- cert, err := tls.X509KeyPair(v.Cert, v.Key)
- if err != nil {
- return err
- }
- s.SNI[k] = &cert
- }
- up, err := url.Parse(v.Upstream)
- if err != nil {
- return err
- }
- if !strings.Contains(v.Host, "/") {
- v.Host += "/"
- }
- switch up.Scheme {
+ wwwMux := http.NewServeMux()
+ tlsMux := http.NewServeMux()
+ for _, v := range s.Route {
+ host := v.ServerName.Host + v.ServerName.Path
+ up := v.Upstream
+ switch v.ServerName.Scheme {
+ case "http", "":
+ wwwMux.Handle(host, httputil.NewSingleHostReverseProxy(up))
+ case "https":
+ wwwMux.Handle(host, http.RedirectHandler("https://"+host, http.StatusMovedPermanently))
+ tlsMux.Handle(host, httputil.NewSingleHostReverseProxy(up))
case "ws":
- httpMux.Handle(v.Host, NewWebSocketProxy(up))
- default:
- httpMux.Handle(v.Host, httputil.NewSingleHostReverseProxy(up))
+ wwwMux.Handle(host, http.RedirectHandler("wss://"+host, http.StatusMovedPermanently))
+ wwwMux.Handle(host, NewWebSocketProxy(up))
+ case "wss":
+ tlsMux.Handle(host, NewWebSocketProxy(up))
}
}
- s.wwwServer.Handler = httpMux
+ s.wwwServer.Handler = wwwMux
+ s.tlsServer.Handler = tlsMux
return nil
}