Procházet zdrojové kódy

restructuring stuff

svcs před 1 rokem
rodič
revize
f0b988194f
6 změnil soubory, kde provedl 115 přidání a 111 odebrání
  1. 81 0
      pkg/cache.go
  2. 0 31
      pkg/client.go
  3. 0 78
      pkg/configuration.go
  4. 1 1
      pkg/controller.go
  5. 32 0
      pkg/pagemod.go
  6. 1 1
      pkg/routing.go

+ 81 - 0
pkg/cache.go

@@ -27,8 +27,12 @@
 package httpserver
 
 import (
+	"encoding/json"
 	"fmt"
+	"log"
 	"net/http"
+	"os"
+	"strings"
 
 	"github.com/patrickmn/go-cache"
 )
@@ -61,3 +65,80 @@ func (c *Controller) GetResource(key string) *CachedResource {
 	}
 	return nil
 }
+
+type RouteMapping struct {
+	DomainName string   `json:"domain_name"`
+	UriPaths   []string `json:"uri_paths"`
+	RouteSet   map[string]struct{}
+}
+
+type RouteMap struct {
+	Mappings map[string]string `json:"mappings"`
+	Shotgun  map[string]string `json:"shotgun"`
+	MapCache *cache.Cache
+}
+
+type RouteMapper interface {
+	mapUriToDomain(string, string)
+	GetMappedDomain(string) (string, bool)
+	ExportRouteMaps(string)
+}
+
+/*
+Set a route to exist for the URI to the specific domain
+
+	:param uri: the URI to set the route for
+	:param domain: the domain name to resolve the uri to
+*/
+func (r *RouteMap) MapUriToDomain(uri string, domain string) {
+	r.MapCache.Set(uri, domain, cache.DefaultExpiration)
+
+}
+
+// returns the domain/url that the uri belongs to as defined in the routemap
+func (r *RouteMap) GetMappedDomain(uri string) (string, bool) {
+	dname, ok := r.MapCache.Get(uri)
+	if ok {
+		return fmt.Sprint(dname), true
+
+	}
+
+	for k, v := range r.Shotgun {
+
+		if strings.Contains(uri, k) {
+			return v, true
+		}
+	}
+
+	return "", false
+}
+
+// This populates the cache in a RouteMap with the data from the config file
+func (r *RouteMap) populateRouteMaps() {
+	for k, v := range r.Mappings {
+		r.MapUriToDomain(k, v)
+	}
+}
+
+// Exports the cache into a JSON-friendly data structure (so that it can be written to the file system)
+func (r *RouteMap) ExportRouteMap(loc string) {
+	routeMapOut := &RouteMap{
+		Mappings: map[string]string{},
+		Shotgun:  map[string]string{},
+	}
+
+	cachedRoutes := r.MapCache.Items()
+	for k, v := range cachedRoutes {
+		routeMapOut.Mappings[k] = fmt.Sprint(v.Object)
+	}
+	for k, v := range r.Shotgun {
+		routeMapOut.Shotgun[k] = v
+	}
+
+	b, err := json.Marshal(routeMapOut)
+	if err != nil {
+		log.Fatal("failed to marshal struct: ", err)
+	}
+	os.WriteFile(loc, b, os.ModePerm)
+
+}

+ 0 - 31
pkg/client.go

@@ -27,7 +27,6 @@
 package httpserver
 
 import (
-	"bytes"
 	"fmt"
 	"io"
 	"log"
@@ -115,24 +114,6 @@ func (c *Controller) TryHosts(method string, path string, hdrs *http.Header, bod
 
 }
 
-/*
-perform any request body rewrites as per described in the pagemod config
-
-	:param data: a byte array to modify
-*/
-func (c *Controller) requestBodyRewrites(data io.Reader) io.Reader {
-	b, err := io.ReadAll(data)
-	if err != nil {
-		log.Fatal("couldnt read POST body data: ", err)
-	}
-	for idx := range c.PageMods.Content {
-		if c.PageMods.Content[idx].Target == "body" {
-			b = bytes.ReplaceAll(b, []byte(c.PageMods.Content[idx].Search), []byte(c.PageMods.Content[idx].Sub))
-		}
-	}
-	return bytes.NewReader(b)
-}
-
 /*
 Sets the request headers to whatever is defined in this private method
 
@@ -153,15 +134,3 @@ func (c *Controller) setHeaders(req *http.Request, hdrs *http.Header) {
 	}
 
 }
-
-/*
-Rewrite all occurences of these values into the response body
-*/
-func (c *Controller) pageMod(data []byte) []byte {
-	for idx := range c.PageMods.Content {
-		if c.PageMods.Content[idx].Target == "content" {
-			data = bytes.ReplaceAll(data, []byte(c.PageMods.Content[idx].Search), []byte(c.PageMods.Content[idx].Sub))
-		}
-	}
-	return data
-}

+ 0 - 78
pkg/include.go → pkg/configuration.go

@@ -32,7 +32,6 @@ import (
 	"log"
 	"net/http"
 	"os"
-	"strings"
 	"time"
 
 	"github.com/patrickmn/go-cache"
@@ -76,83 +75,6 @@ type Cookie struct {
 	IncludeSub bool   `json:"include_sub"`
 }
 
-type RouteMapping struct {
-	DomainName string   `json:"domain_name"`
-	UriPaths   []string `json:"uri_paths"`
-	RouteSet   map[string]struct{}
-}
-
-type RouteMap struct {
-	Mappings map[string]string `json:"mappings"`
-	Shotgun  map[string]string `json:"shotgun"`
-	MapCache *cache.Cache
-}
-
-type RouteMapper interface {
-	mapUriToDomain(string, string)
-	GetMappedDomain(string) (string, bool)
-	ExportRouteMaps(string)
-}
-
-/*
-Set a route to exist for the URI to the specific domain
-
-	:param uri: the URI to set the route for
-	:param domain: the domain name to resolve the uri to
-*/
-func (r *RouteMap) MapUriToDomain(uri string, domain string) {
-	r.MapCache.Set(uri, domain, cache.DefaultExpiration)
-
-}
-
-// returns the domain/url that the uri belongs to as defined in the routemap
-func (r *RouteMap) GetMappedDomain(uri string) (string, bool) {
-	dname, ok := r.MapCache.Get(uri)
-	if ok {
-		return fmt.Sprint(dname), true
-
-	}
-
-	for k, v := range r.Shotgun {
-
-		if strings.Contains(uri, k) {
-			return v, true
-		}
-	}
-
-	return "", false
-}
-
-// This populates the cache in a RouteMap with the data from the config file
-func (r *RouteMap) populateRouteMaps() {
-	for k, v := range r.Mappings {
-		r.MapUriToDomain(k, v)
-	}
-}
-
-// Exports the cache into a JSON-friendly data structure (so that it can be written to the file system)
-func (r *RouteMap) ExportRouteMap(loc string) {
-	routeMapOut := &RouteMap{
-		Mappings: map[string]string{},
-		Shotgun:  map[string]string{},
-	}
-
-	cachedRoutes := r.MapCache.Items()
-	for k, v := range cachedRoutes {
-		routeMapOut.Mappings[k] = fmt.Sprint(v.Object)
-	}
-	for k, v := range r.Shotgun {
-		routeMapOut.Shotgun[k] = v
-	}
-
-	b, err := json.Marshal(routeMapOut)
-	if err != nil {
-		log.Fatal("failed to marshal struct: ", err)
-	}
-	os.WriteFile(loc, b, os.ModePerm)
-
-}
-
 /*
 Reads the server configuration file, along with the cookie file so that the correlated account can be
 accessed through the proxy

+ 1 - 1
pkg/controller.go

@@ -91,7 +91,7 @@ func NewController(cfg *HttpServerConfig, routeMap *RouteMap) *Controller {
 /*
 This handler will be responsible for proxying out the GET requests that the server recieves
 */
-func (c *Controller) Get(ctx *gin.Context) {
+func (c *Controller) HandleAny(ctx *gin.Context) {
 	incomingPath := ctx.Param("ProxiedPath")
 	for idx := range c.Config.Redirects {
 		if incomingPath == c.Config.Redirects[idx].From {

+ 32 - 0
pkg/pagemod.go

@@ -27,7 +27,9 @@
 package httpserver
 
 import (
+	"bytes"
 	"encoding/json"
+	"io"
 	"log"
 	"os"
 )
@@ -61,3 +63,33 @@ func LoadPageMods(loc string) *AllPageMods {
 	return &pgMod
 
 }
+
+/*
+Rewrite all occurences of these values into the response body
+*/
+func (c *Controller) pageMod(data []byte) []byte {
+	for idx := range c.PageMods.Content {
+		if c.PageMods.Content[idx].Target == "content" {
+			data = bytes.ReplaceAll(data, []byte(c.PageMods.Content[idx].Search), []byte(c.PageMods.Content[idx].Sub))
+		}
+	}
+	return data
+}
+
+/*
+perform any request body rewrites as per described in the pagemod config
+
+	:param data: a byte array to modify
+*/
+func (c *Controller) requestBodyRewrites(data io.Reader) io.Reader {
+	b, err := io.ReadAll(data)
+	if err != nil {
+		log.Fatal("couldnt read POST body data: ", err)
+	}
+	for idx := range c.PageMods.Content {
+		if c.PageMods.Content[idx].Target == "body" {
+			b = bytes.ReplaceAll(b, []byte(c.PageMods.Content[idx].Search), []byte(c.PageMods.Content[idx].Sub))
+		}
+	}
+	return bytes.NewReader(b)
+}

+ 1 - 1
pkg/routing.go

@@ -50,6 +50,6 @@ func RegisterRoutes(e *gin.Engine, cfg *HttpServerConfig, rmaps *RouteMap) {
 		os.Exit(1)
 	}(c)
 	web := e.Group("")
-	web.Any("/*ProxiedPath", c.Get)
+	web.Any("/*ProxiedPath", c.HandleAny)
 
 }