mirror of
https://github.com/oauth2-proxy/oauth2-proxy.git
synced 2026-09-30 03:31:27 +02:00
Integrate new server implementation into main OAuth2 Proxy
This commit is contained in:
@@ -1,20 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"runtime"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/ghodss/yaml"
|
||||
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
|
||||
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
|
||||
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/middleware"
|
||||
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/validation"
|
||||
"github.com/spf13/pflag"
|
||||
)
|
||||
@@ -67,54 +62,9 @@ func main() {
|
||||
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
oauthProxyStop := make(chan struct{}, 1)
|
||||
metricsStop := startMetricsServer(opts.MetricsAddress, oauthProxyStop)
|
||||
|
||||
s := &Server{
|
||||
Handler: oauthproxy,
|
||||
Opts: opts,
|
||||
stop: oauthProxyStop,
|
||||
if err := oauthproxy.Start(); err != nil {
|
||||
logger.Fatalf("ERROR: Failed to start OAuth2 Proxy: %v", err)
|
||||
}
|
||||
// Observe signals in background goroutine.
|
||||
go func() {
|
||||
sigint := make(chan os.Signal, 1)
|
||||
signal.Notify(sigint, os.Interrupt, syscall.SIGTERM)
|
||||
<-sigint
|
||||
s.stop <- struct{}{} // notify having caught signal stop oauthproxy
|
||||
close(metricsStop) // and the metrics endpoint
|
||||
}()
|
||||
s.ListenAndServe()
|
||||
}
|
||||
|
||||
// startMetricsServer will start the metrics server on the specified address.
|
||||
// It always return a channel to signal stop even when it does not run.
|
||||
func startMetricsServer(address string, oauthProxyStop chan struct{}) chan struct{} {
|
||||
stop := make(chan struct{}, 1)
|
||||
|
||||
// Attempt to setup the metrics endpoint if we have an address
|
||||
if address != "" {
|
||||
s := &http.Server{Addr: address, Handler: middleware.DefaultMetricsHandler}
|
||||
go func() {
|
||||
// ListenAndServe always returns a non-nil error. After Shutdown or
|
||||
// Close, the returned error is ErrServerClosed
|
||||
if err := s.ListenAndServe(); err != http.ErrServerClosed {
|
||||
logger.Println(err)
|
||||
// Stop the metrics shutdown go routine
|
||||
close(stop)
|
||||
// Stop the oauthproxy server, we have encounter an unexpected error
|
||||
close(oauthProxyStop)
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
<-stop
|
||||
if err := s.Shutdown(context.Background()); err != nil {
|
||||
logger.Print(err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
return stop
|
||||
}
|
||||
|
||||
// loadConfiguration will load in the user's configuration.
|
||||
|
||||
Reference in New Issue
Block a user