Skip to content

Commit

Permalink
Merge pull request #18 from hvuhsg/development
Browse files Browse the repository at this point in the history
Move to internal and make multimux work concurantlry
  • Loading branch information
hvuhsg authored Oct 25, 2024
2 parents e4684e2 + 28ca575 commit 93f909d
Show file tree
Hide file tree
Showing 41 changed files with 33 additions and 34 deletions.
2 changes: 1 addition & 1 deletion checks.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gatego

import (
"github.com/hvuhsg/gatego/config"
"github.com/hvuhsg/gatego/internal/config"
"github.com/hvuhsg/gatego/pkg/monitor"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"os/signal"

"github.com/hvuhsg/gatego"
"github.com/hvuhsg/gatego/config"
"github.com/hvuhsg/gatego/internal/config"
)

const version = "0.0.1"
Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 4 additions & 2 deletions gatego.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package gatego

import (
"context"
"fmt"
"time"

"github.com/hvuhsg/gatego/config"
"github.com/hvuhsg/gatego/contextvalues"
"github.com/hvuhsg/gatego/internal/config"
"github.com/hvuhsg/gatego/internal/contextvalues"
"github.com/hvuhsg/gatego/pkg/monitor"
)

Expand Down Expand Up @@ -61,6 +62,7 @@ func (gg GateGo) Run() error {
case err = <-serveErrChan:
return err
case <-gg.ctx.Done():
fmt.Println("\nShutting down...")
return server.Shutdown(context.Background())
}
}
6 changes: 3 additions & 3 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"os"
"slices"

"github.com/hvuhsg/gatego/config"
"github.com/hvuhsg/gatego/handlers"
"github.com/hvuhsg/gatego/middlewares"
"github.com/hvuhsg/gatego/internal/config"
"github.com/hvuhsg/gatego/internal/handlers"
"github.com/hvuhsg/gatego/internal/middlewares"
)

var ErrUnsupportedBaseHandler = errors.New("base handler unsupported")
Expand Down
2 changes: 1 addition & 1 deletion config/config.go → internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"time"

"github.com/hashicorp/go-version"
"github.com/hvuhsg/gatego/middlewares"
"github.com/hvuhsg/gatego/internal/middlewares"
"github.com/hvuhsg/gatego/pkg/cron"
"gopkg.in/yaml.v3"
)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions handlers/balancer.go → internal/handlers/balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"net/url"
"time"

"github.com/hvuhsg/gatego/config"
"github.com/hvuhsg/gatego/contextvalues"
"github.com/hvuhsg/gatego/internal/config"
"github.com/hvuhsg/gatego/internal/contextvalues"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"testing"
"time"

"github.com/hvuhsg/gatego/config"
"github.com/hvuhsg/gatego/internal/config"
)

func TestNewBalancer(t *testing.T) {
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions handlers/proxy.go → internal/handlers/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"net/http/httputil"
"net/url"

"github.com/hvuhsg/gatego/config"
"github.com/hvuhsg/gatego/contextvalues"
"github.com/hvuhsg/gatego/internal/config"
"github.com/hvuhsg/gatego/internal/contextvalues"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"net/http/httptest"
"testing"

"github.com/hvuhsg/gatego/middlewares"
"github.com/hvuhsg/gatego/internal/middlewares"
)

// Helper to decode gzip data
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net/http/httptest"
"testing"

"github.com/hvuhsg/gatego/middlewares"
"github.com/hvuhsg/gatego/internal/middlewares"
)

// Helper function to create a basic next handler that returns content with a specific content type
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net/http/httptest"
"testing"

"github.com/hvuhsg/gatego/middlewares"
"github.com/hvuhsg/gatego/internal/middlewares"
)

// TestOmitHeadersMiddleware_OmitResponseHeaders tests that headers are omitted from the response
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"os"
"testing"

"github.com/hvuhsg/gatego/middlewares"
"github.com/hvuhsg/gatego/internal/middlewares"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down
2 changes: 1 addition & 1 deletion middlewares/otel.go → internal/middlewares/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"net/http"

"github.com/hvuhsg/gatego/contextvalues"
"github.com/hvuhsg/gatego/internal/contextvalues"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"sync"
"testing"

"github.com/hvuhsg/gatego/middlewares"
"github.com/hvuhsg/gatego/internal/middlewares"
)

func TestRateLimitExceeded(t *testing.T) {
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"net/http/httptest"
"testing"

"github.com/hvuhsg/gatego/middlewares"
"github.com/hvuhsg/gatego/internal/middlewares"
)

// TestRequestSizeLimitMiddleware tests the RequestSizeLimitMiddleware
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"
"time"

"github.com/hvuhsg/gatego/middlewares"
"github.com/hvuhsg/gatego/internal/middlewares"
)

func TestTimeoutMiddleware(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"errors"
"time"

"github.com/hvuhsg/gatego/contextvalues"
"github.com/hvuhsg/gatego/internal/contextvalues"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc"
Expand Down
17 changes: 7 additions & 10 deletions pkg/multimux/multimux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,21 @@ package multimux
import (
"net/http"
"strings"
"sync"
)

type MultiMux struct {
Hosts map[string]*http.ServeMux
Hosts sync.Map
}

func NewMultiMux() *MultiMux {
hosts := make(map[string]*http.ServeMux)
return &MultiMux{Hosts: hosts}
return &MultiMux{Hosts: sync.Map{}}
}

func (mm *MultiMux) RegisterHandler(host string, pattern string, handler http.Handler) {
cleanedHost := cleanHost(host)
mux, exists := mm.Hosts[cleanedHost]

if !exists {
mux = http.NewServeMux()
mm.Hosts[cleanedHost] = mux
}
muxAny, _ := mm.Hosts.LoadOrStore(cleanedHost, http.NewServeMux())
mux := muxAny.(*http.ServeMux)

cleanedPattern := strings.ToLower(pattern)

Expand All @@ -34,13 +30,14 @@ func (mm *MultiMux) RegisterHandler(host string, pattern string, handler http.Ha
func (mm *MultiMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
host := r.Host
cleanedHost := cleanHost(host)
mux, exists := mm.Hosts[cleanedHost]
muxAny, exists := mm.Hosts.Load(cleanedHost)

if !exists {
w.WriteHeader(http.StatusNotFound)
return
}

mux := muxAny.(*http.ServeMux)
mux.ServeHTTP(w, r)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/multimux/multimux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestRegisterHandler(t *testing.T) {
mm.RegisterHandler(tt.host, tt.pattern, handler)

cleanedHost := cleanHost(tt.host)
mux, exists := mm.Hosts[cleanedHost]
mux, exists := mm.Hosts.Load(cleanedHost)
if !exists {
t.Errorf("Host %s was not registered", cleanedHost)
}
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"os"
"time"

"github.com/hvuhsg/gatego/config"
"github.com/hvuhsg/gatego/internal/config"
"github.com/hvuhsg/gatego/pkg/multimux"
)

Expand Down

0 comments on commit 93f909d

Please sign in to comment.