Supervisor gains proxy ability
This commit is contained in:
parent
41a14ac1ae
commit
22dcdee871
2
Makefile
2
Makefile
|
@ -162,7 +162,7 @@ server-dev: # Run and restart server for development
|
||||||
build: frontend system supervisor # Compile all artifacts for production
|
build: frontend system supervisor # Compile all artifacts for production
|
||||||
|
|
||||||
dev: # Compile, run, and watch all artifacts and server for development
|
dev: # Compile, run, and watch all artifacts and server for development
|
||||||
$(MAKE_QUIETLY) -j3 frontend-dev system-dev supervisor-dev server-dev
|
$(MAKE_QUIETLY) -j4 frontend-dev system-dev supervisor-dev server-dev
|
||||||
|
|
||||||
### Application tools
|
### Application tools
|
||||||
|
|
||||||
|
|
|
@ -39,6 +39,7 @@ g++
|
||||||
git
|
git
|
||||||
golang
|
golang
|
||||||
htop
|
htop
|
||||||
|
httpie
|
||||||
jq
|
jq
|
||||||
less
|
less
|
||||||
make
|
make
|
||||||
|
|
|
@ -48,7 +48,9 @@ dctrl-tools
|
||||||
bind9-dnsutils
|
bind9-dnsutils
|
||||||
less
|
less
|
||||||
git
|
git
|
||||||
|
golang
|
||||||
htop
|
htop
|
||||||
|
httpie
|
||||||
jq
|
jq
|
||||||
make
|
make
|
||||||
man
|
man
|
||||||
|
|
|
@ -2,6 +2,11 @@
|
||||||
|
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
|
function verbosely {
|
||||||
|
echo "$@"
|
||||||
|
"$@"
|
||||||
|
}
|
||||||
|
|
||||||
cd supervisor
|
cd supervisor
|
||||||
mkdir -p out
|
mkdir -p out
|
||||||
go build -o out/riju-supervisor ./src
|
verbosely go build -o out/riju-supervisor ./src
|
||||||
|
|
|
@ -2,8 +2,75 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httputil"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
type supervisor struct {
|
||||||
fmt.Println("Hello, world!")
|
proxyHandler http.Handler
|
||||||
|
|
||||||
|
reloadLock sync.Mutex
|
||||||
|
reloadInProgress bool
|
||||||
|
reloadNeeded bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sv *supervisor) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if strings.HasPrefix(r.URL.Path, "/api/supervisor") {
|
||||||
|
if r.URL.Path == "/api/supervisor/v1/reload" {
|
||||||
|
resp := ""
|
||||||
|
sv.reloadLock.Lock()
|
||||||
|
if !sv.reloadInProgress {
|
||||||
|
sv.reloadInProgress = true
|
||||||
|
go sv.reload()
|
||||||
|
resp = "Triggered reload."
|
||||||
|
} else {
|
||||||
|
sv.reloadNeeded = true
|
||||||
|
resp = "Reload already in progress, scheduling another one."
|
||||||
|
}
|
||||||
|
sv.reloadLock.Unlock()
|
||||||
|
fmt.Fprintln(w, resp)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
http.NotFound(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
sv.proxyHandler.ServeHTTP(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sv *supervisor) reloadWithScheduling() {
|
||||||
|
err := sv.reload()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("failed to reload: %s\n", err.Error())
|
||||||
|
} else {
|
||||||
|
log.Println("successfully reloaded")
|
||||||
|
}
|
||||||
|
sv.reloadLock.Lock()
|
||||||
|
sv.reloadInProgress = false
|
||||||
|
if sv.reloadNeeded {
|
||||||
|
sv.reloadInProgress = true
|
||||||
|
go sv.reloadWithScheduling()
|
||||||
|
}
|
||||||
|
sv.reloadLock.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sv *supervisor) reload() error {
|
||||||
|
fmt.Println("starting reload")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
url, err := url.Parse("http://localhost:6119")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
sv := &supervisor{
|
||||||
|
proxyHandler: httputil.NewSingleHostReverseProxy(url),
|
||||||
|
}
|
||||||
|
log.Println("listening on http://0.0.0.0:80")
|
||||||
|
log.Fatalln(http.ListenAndServe("0.0.0.0:80", sv))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue