summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2025-03-04 14:35:24 -0700
committermo khan <mo@mokhan.ca>2025-03-04 14:35:24 -0700
commit8e5dfb96dca08ff8bd5b2f7ad47dd22ac2f799c5 (patch)
treefe5fe4d72006850977b37f1d105d2fbc4cc75dd9
parent8abcc2c29e75ed7417422929446266ed5abd9c3b (diff)
feat: add API gateway to reverse proxy requests in front of two different services
-rwxr-xr-xbin/idp2
-rwxr-xr-xbin/sp2
-rw-r--r--cmd/gtwy/main.go48
-rw-r--r--magefile.go14
4 files changed, 60 insertions, 6 deletions
diff --git a/bin/idp b/bin/idp
index 4df50157..fc276bb5 100755
--- a/bin/idp
+++ b/bin/idp
@@ -286,7 +286,7 @@ class IdentityProvider
end
def not_found
- [404, {}, []]
+ [404, { 'X-Backend-Server' => 'IDP' }, []]
end
def saml_params_from(request)
diff --git a/bin/sp b/bin/sp
index 11812367..8fe7472e 100755
--- a/bin/sp
+++ b/bin/sp
@@ -81,7 +81,7 @@ class ServiceProvider
private
def not_found
- [404, {}, []]
+ [404, { 'X-Backend-Server' => 'SP' }, []]
end
def redirect_to(location)
diff --git a/cmd/gtwy/main.go b/cmd/gtwy/main.go
index eb6a1b5d..986741b0 100644
--- a/cmd/gtwy/main.go
+++ b/cmd/gtwy/main.go
@@ -1,7 +1,51 @@
package main
-import "fmt"
+import (
+ "fmt"
+ "log"
+ "net/http"
+ "net/http/httputil"
+ "strings"
+ "time"
+)
+
+func NewProxy(from, to string) http.Handler {
+ director := func(r *http.Request) {
+ log.Printf("%v (from: %v to: %v)\n", r.URL, from, to)
+ r.URL.Scheme = "http"
+ r.Host = to
+ r.URL.Host = to
+ r.URL.Path = strings.TrimPrefix(r.URL.Path, strings.TrimSuffix(from, "/*"))
+ r.URL.RawPath = strings.TrimPrefix(r.URL.RawPath, strings.TrimSuffix(from, "/*"))
+ }
+ return &httputil.ReverseProxy{
+ Director: director,
+ Transport: http.DefaultTransport,
+ FlushInterval: -1,
+ ErrorLog: nil,
+ ModifyResponse: func(r *http.Response) error {
+ r.Header.Add("Via", fmt.Sprintf("%v gateway", r.Proto))
+ return nil
+ },
+ ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
+ log.Println(err)
+ },
+ }
+}
func main() {
- fmt.Println("hello, world!")
+ mux := http.NewServeMux()
+ mux.Handle("/idp/", NewProxy("/idp", "localhost:8282"))
+ mux.Handle("/sp/", NewProxy("/sp", "localhost:8283"))
+
+ srv := &http.Server{
+ Addr: ":8080",
+ Handler: mux,
+ ReadHeaderTimeout: 10 * time.Second,
+ ReadTimeout: 30 * time.Second,
+ WriteTimeout: 2 * time.Minute,
+ IdleTimeout: 5 * time.Minute,
+ ErrorLog: log.Default(),
+ }
+ log.Fatal(srv.ListenAndServe())
}
diff --git a/magefile.go b/magefile.go
index 9c4a01bc..39afbf93 100644
--- a/magefile.go
+++ b/magefile.go
@@ -5,6 +5,7 @@ package main
import (
"context"
+ "runtime"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
@@ -24,12 +25,21 @@ func RunSp() error {
return sh.RunV("ruby", "./bin/sp")
}
+// Run the API Gateway
+func RunGateway() error {
+ return sh.RunV("go", "run", "./cmd/gtwy/main.go")
+}
+
// Open a web browser to the login page
func Browser() error {
- return sh.RunV("xdg-open", "http://localhost:8283/sessions/new")
+ if runtime.GOOS == "linux" {
+ return sh.RunV("xdg-open", "http://localhost:8080/sp/sessions/new")
+ } else {
+ return sh.RunV("open", "http://localhost:8080/sp/sessions/new")
+ }
}
// Run All the servers
func Run(ctx context.Context) {
- mg.CtxDeps(ctx, RunIdp, RunSp, Browser)
+ mg.CtxDeps(ctx, RunIdp, RunSp, RunGateway, Browser)
}