summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2016-10-13 22:29:08 +0200
committerChristian Pointner <equinox@spreadspace.org>2016-10-13 22:40:37 +0200
commit0829a3d91131fa57df68b4881731ccd2314ab372 (patch)
treedadeb6c167022b394d3ef17e8613fc3173e48882
parentsome fixes in new config file format (diff)
refactored basic infrastructure
-rw-r--r--src/daq/s5proxy/src/s5proxy/proxy.go51
-rw-r--r--src/daq/s5proxy/src/s5proxy/s5proxy.go29
2 files changed, 52 insertions, 28 deletions
diff --git a/src/daq/s5proxy/src/s5proxy/proxy.go b/src/daq/s5proxy/src/s5proxy/proxy.go
index 282d502..09b6bd5 100644
--- a/src/daq/s5proxy/src/s5proxy/proxy.go
+++ b/src/daq/s5proxy/src/s5proxy/proxy.go
@@ -66,20 +66,31 @@ func (r s5proxyResponseWriter) WriteHeader(status int) {
r.wrapped.WriteHeader(status)
}
-func proxyHandler(c *Config, p *httputil.ReverseProxy, w http.ResponseWriter, r *http.Request) {
- pw := s5proxyResponseWriter{wrapped: w, conf: c}
- p.ServeHTTP(pw, r)
+func proxyHandler(p *Proxy, w http.ResponseWriter, r *http.Request) {
+ pw := s5proxyResponseWriter{wrapped: w, conf: p.conf}
+ p.proxy.ServeHTTP(pw, r)
}
-func s5proxyInit(conf *Config) (m *http.ServeMux, err error) {
- remote, err := url.Parse(conf.ConnectAddr)
+type Proxy struct {
+ conf *Config
+ proxy *httputil.ReverseProxy
+ mux *http.ServeMux
+ srv *http.Server
+}
+
+func NewProxy(conf *Config) (p *Proxy, err error) {
+
+ p = &Proxy{conf: conf}
+
+ var remote *url.URL
+ remote, err = url.Parse(conf.ConnectAddr)
if err != nil {
return
}
- p := httputil.NewSingleHostReverseProxy(remote)
- origDir := p.Director
+ p.proxy = httputil.NewSingleHostReverseProxy(remote)
+ origDir := p.proxy.Director
- p.Director = func(req *http.Request) {
+ p.proxy.Director = func(req *http.Request) {
origDir(req)
for _, h := range conf.RequestHeader {
switch h.Operation {
@@ -93,19 +104,10 @@ func s5proxyInit(conf *Config) (m *http.ServeMux, err error) {
}
}
- m = http.NewServeMux()
- m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
- proxyHandler(conf, p, w, r)
+ p.mux = http.NewServeMux()
+ p.mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
+ proxyHandler(p, w, r)
})
- return
-}
-
-func runProxy(conf *Config) error {
-
- mux, err := s5proxyInit(conf)
- if err != nil {
- return err
- }
// TODO: make this configurable
cfg := &tls.Config{
@@ -120,12 +122,15 @@ func runProxy(conf *Config) error {
},
}
- srv := &http.Server{
+ p.srv = &http.Server{
Addr: conf.ListenAddr,
- Handler: mux,
+ Handler: p.mux,
TLSConfig: cfg,
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler), 0),
}
+ return
+}
- return srv.ListenAndServeTLS(conf.CertFile, conf.KeyFile)
+func (p *Proxy) Run() error {
+ return p.srv.ListenAndServeTLS(p.conf.CertFile, p.conf.KeyFile)
}
diff --git a/src/daq/s5proxy/src/s5proxy/s5proxy.go b/src/daq/s5proxy/src/s5proxy/s5proxy.go
index f180138..47ba8b7 100644
--- a/src/daq/s5proxy/src/s5proxy/s5proxy.go
+++ b/src/daq/s5proxy/src/s5proxy/s5proxy.go
@@ -36,9 +36,10 @@ import (
"flag"
"log"
"os"
+ "os/signal"
)
-var s5l = log.New(os.Stderr, "[s5]\t", log.LstdFlags)
+var s5l = log.New(os.Stderr, "[s5proxy]\t", log.LstdFlags)
func main() {
cf := flag.String("config", "/etc/s5proxy/default.json", "path to the config file")
@@ -59,9 +60,27 @@ func main() {
os.Exit(1)
}
- err = runProxy(conf)
- if err != nil {
- s5l.Printf("Error: %+v\n", err)
+ var proxy *Proxy
+ proxy, err = NewProxy(conf)
+
+ done := make(chan bool)
+
+ go func() {
+ defer func() { done <- true }()
+ s5l.Println("start proxy")
+ proxy.Run()
+ s5l.Println("ERROR: proxy stopped")
+ }()
+
+ c := make(chan os.Signal, 1)
+ signal.Notify(c, os.Interrupt)
+
+ select {
+ case <-c:
+ s5l.Println("received interrupt, shutdown")
+ return
+ case <-done:
+ return
}
- s5l.Println("shuting down!")
+
}