Supervisor gains proxy ability

This commit is contained in:
Radon Rosborough 2021-07-03 18:43:10 +00:00
parent 41a14ac1ae
commit 22dcdee871
5 changed files with 79 additions and 4 deletions

View File

@ -162,7 +162,7 @@ server-dev: # Run and restart server for development
build: frontend system supervisor # Compile all artifacts for production
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

View File

@ -39,6 +39,7 @@ g++
git
golang
htop
httpie
jq
less
make

View File

@ -48,7 +48,9 @@ dctrl-tools
bind9-dnsutils
less
git
golang
htop
httpie
jq
make
man

View File

@ -2,6 +2,11 @@
set -euo pipefail
function verbosely {
echo "$@"
"$@"
}
cd supervisor
mkdir -p out
go build -o out/riju-supervisor ./src
verbosely go build -o out/riju-supervisor ./src

View File

@ -2,8 +2,75 @@ package main
import (
"fmt"
"log"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"sync"
)
func main() {
fmt.Println("Hello, world!")
type supervisor struct {
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))
}