Skip to content

Commit

Permalink
Release1.0 (#1)
Browse files Browse the repository at this point in the history
* Add HTTPRedirector and OpenGraph packages with tests

* Refactor code

* Add dependencies and update styles and types

* Update code formatting and fix minor bugs

* Update Dockerfile and UI code

* Add UI assets and fix file path in UIHandler

* Add UI assets and fix file path in UIHandler
  • Loading branch information
alileza authored Mar 19, 2024
1 parent 633ea41 commit 2bff1c4
Show file tree
Hide file tree
Showing 52 changed files with 1,915 additions and 1,477 deletions.
12 changes: 0 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,11 @@ COPY . /app
WORKDIR /app
RUN go build -o /app/bridge

# frontend build
FROM node:21-alpine as reactbuild

WORKDIR /app
RUN npm install -g pnpm
COPY ./portal/package*.json ./
RUN pnpm install
COPY ./portal .
RUN pnpm build


# final image
FROM alpine:3.17.3

WORKDIR /app

COPY --from=gobuild /app/bridge /bin/bridge
COPY --from=reactbuild /app/dist /app/portal/dist

ENTRYPOINT [ "bridge" ]
14 changes: 3 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,16 @@ go 1.22.0

require (
github.com/aws/aws-sdk-go v1.50.32
github.com/prometheus/client_golang v1.19.0
github.com/boombuler/barcode v1.0.1
github.com/urfave/cli/v2 v2.27.1
golang.org/x/net v0.21.0
golang.org/x/time v0.5.0
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/sys v0.17.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
31 changes: 2 additions & 29 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,37 +1,18 @@
github.com/aws/aws-sdk-go v1.50.32 h1:POt81DvegnpQKM4DMDLlHz1CO6OBnEoQ1gRhYFd7QRY=
github.com/aws/aws-sdk-go v1.50.32/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/boombuler/barcode v1.0.1 h1:NDBbPmhS+EqABEs5Kg3n/5ZNjy73Pz7SIV+KCeqyXcs=
github.com/boombuler/barcode v1.0.1/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v1.19.0 h1:ygXvpU1AoN1MhdzckN+PyD9QJOSD4x7kmXYlnfbA6JU=
github.com/prometheus/client_golang v1.19.0/go.mod h1:ZRM9uEAypZakd+q/x7+gmsvXdURP+DABIEIjnmDdp+k=
github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw=
github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI=
github.com/prometheus/common v0.48.0 h1:QO8U2CdOzSn1BBsmXJXduaaW+dY/5QLjfB8svtSzKKE=
github.com/prometheus/common v0.48.0/go.mod h1:0/KsvlIEfPQCQ5I2iNSAWKPZziNCvRs5EC6ILDTlAPc=
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All @@ -41,17 +22,9 @@ github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRT
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
131 changes: 131 additions & 0 deletions httpredirector/httpredirector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package httpredirector

import (
"fmt"
"net/http"
"net/url"
"strings"

"bridge/opengraph"
)

const DefaultRedirectURL = "https://alileza.me/"

// HTTPRedirector is a struct that holds the routes and their destinations
type HTTPRedirector struct {
BaseURL string
EnableOpengraph bool
Storage Storage
}

type Route struct {
Preview string `json:"preview"`
Key string `json:"key"`
URL string `json:"url"`
}

type Storage interface {
Store(key any, value any)
Load(key any) (value any, ok bool)
Delete(key any)
Range(f func(key any, value any) bool)
}

// LoadRoutes loads a map of routes into the redirector
func (rdr *HTTPRedirector) LoadRoutes(routes map[string]string) {
for k, v := range routes {
rdr.Storage.Store(k, v)
}
}

func (rdr *HTTPRedirector) ListRoutes() []Route {
var routes []Route

rdr.Storage.Range(func(k, v interface{}) bool {
i, _ := opengraph.GenerateBarcode(rdr.BaseURL + k.(string))
routes = append(routes, Route{
Preview: "data:image/png;base64," + opengraph.EncodeImageToBase64(i),
Key: k.(string),
URL: v.(string),
})
return true
})
return routes
}

// AddRoute adds a route to the redirector it can be path or full with hostname without scheme
func (rdr *HTTPRedirector) SetRoute(key string, destURL string) error {
_, err := url.ParseRequestURI(destURL)
if err != nil {
return fmt.Errorf("invalid destination URL: %w", err)
}

if !strings.Contains(key, "/") && !strings.Contains(key, ".") {
key = "/" + key
}

rdr.Storage.Store(key, destURL)
return nil
}

// RemoveRoute removes a route from the redirector
func (rdr *HTTPRedirector) RemoveRoute(key string) error {
if _, ok := rdr.Storage.Load(key); !ok {
return fmt.Errorf("route not found: %s", key)
}

rdr.Storage.Delete(key)
return nil
}

// GetRedirectURL returns the destination for a given route
func (rdr *HTTPRedirector) GetRedirectURL(key *url.URL) string {
keyWithHost := key.Host + key.Path
dest, ok := rdr.Storage.Load(keyWithHost)
if ok {
return dest.(string)
}

dest, ok = rdr.Storage.Load(key.Path)
if ok {
return dest.(string)
}

return DefaultRedirectURL
}

func (rdr *HTTPRedirector) Handler(w http.ResponseWriter, r *http.Request) {
destinationURL := rdr.GetRedirectURL(r.URL)

if !rdr.EnableOpengraph {
http.Redirect(w, r, destinationURL, http.StatusFound)
return
}

headElements, err := opengraph.FetchMetaTags(r.Context(), destinationURL)
if err != nil {
http.Redirect(w, r, destinationURL, http.StatusFound)
return
}

w.Header().Set("Content-Type", "text/html")
w.Write([]byte(fmt.Sprintf(responseBody, headElements, destinationURL, destinationURL, destinationURL)))
}

const responseBody = `
<!DOCTYPE html>
<html>
<head>
%s
<title>Bridge - github.com/alileza/bridge</title>
</head>
<body>
<noscript>
<a href="%s">Click here to continue to: %s</a>
</noscript>
<script>
window.location.replace("%s");
</script>
</body>
</html>
`
77 changes: 77 additions & 0 deletions httpredirector/httpredirector_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package httpredirector

import (
"net/url"
"sync"
"testing"
)

// Test loading multiple routes at once
func TestLoadRoutes(t *testing.T) {
rdr := &HTTPRedirector{routes: sync.Map{}}
routes := map[string]string{
"/key1": "http://destination1.com",
"/key2": "http://destination2.com",
}
rdr.LoadRoutes(routes)

for k, v := range routes {
dest, ok := rdr.routes.Load(k)
if !ok {
t.Fatalf("expected to find route for key %s, but did not", k)
}
if dest != v {
t.Fatalf("expected destination %s, got %s", v, dest)
}
}
}

// Test the GetRedirectURL function
func TestGetRedirectURL(t *testing.T) {
tests := []struct {
name string
setupActions func(rdr *HTTPRedirector)
key string
expectedDest string
expectedFound bool
}{
{
name: "find route with path only",
setupActions: func(rdr *HTTPRedirector) {
rdr.AddRoute("/path-only", "http://destination-path-only.com")
},
key: "/path-only",
expectedDest: "http://destination-path-only.com",
expectedFound: true,
},
{
name: "find route with domain and path",
setupActions: func(rdr *HTTPRedirector) {
rdr.AddRoute("example.com/path", "http://destination-domain-path.com")
},
key: "http://example.com/path",
expectedDest: "http://destination-domain-path.com",
expectedFound: true,
},
{
name: "fallback to default for non-existing route",
setupActions: func(rdr *HTTPRedirector) {},
key: "http://nonexisting.com/path",
expectedDest: "https://alileza.me/",
expectedFound: false,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
rdr := &HTTPRedirector{routes: sync.Map{}}
tc.setupActions(rdr)

parsedKey, _ := url.Parse(tc.key)
dest := rdr.GetRedirectURL(parsedKey)
if dest != tc.expectedDest {
t.Fatalf("expected destination %s, got %s for key %s", tc.expectedDest, dest, tc.key)
}
})
}
}
Loading

0 comments on commit 2bff1c4

Please sign in to comment.