Skip to content

Commit

Permalink
Include proxy pass to frontend and cookie for API address
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigo-brito committed Feb 14, 2019
1 parent 5c92ab1 commit 705567f
Show file tree
Hide file tree
Showing 86 changed files with 17,152 additions and 26 deletions.
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module github.com/rodrigo-brito/gocity
require (
cloud.google.com/go v0.30.0
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 // indirect
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc // indirect
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 // indirect
Expand All @@ -16,19 +18,23 @@ require (
github.com/kevinburke/ssh_config v0.0.0-20180830205328-81db2a75821e // indirect
github.com/mitchellh/go-homedir v1.0.0 // indirect
github.com/pelletier/go-buffruneio v0.2.0 // indirect
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
github.com/pkg/errors v0.8.0 // indirect
github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e
github.com/sergi/go-diff v1.0.0 // indirect
github.com/sirupsen/logrus v1.3.0
github.com/src-d/gcfg v1.3.0 // indirect
github.com/stretchr/testify v1.2.2
github.com/urfave/cli v1.20.0 // indirect
github.com/tsuru/tsuru v0.0.0-20190213172318-ad6c1309d678 // indirect
github.com/urfave/cli v1.20.0
github.com/xanzy/ssh-agent v0.2.0 // indirect
go.opencensus.io v0.17.0 // indirect
golang.org/x/crypto v0.0.0-20181001203147-e3636079e1a4 // indirect
golang.org/x/net v0.0.0-20181005035420-146acd28ed58 // indirect
google.golang.org/api v0.0.0-20181021000519-a2651947f503 // indirect
google.golang.org/genproto v0.0.0-20181016170114-94acd270e44e // indirect
google.golang.org/grpc v1.15.0 // indirect
gopkg.in/alecthomas/kingpin.v2 v2.2.6 // indirect
gopkg.in/src-d/go-billy.v4 v4.3.0 // indirect
gopkg.in/src-d/go-git-fixtures.v3 v3.1.1 // indirect
gopkg.in/src-d/go-git.v4 v4.7.0
Expand Down
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ cloud.google.com/go v0.30.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT
git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg=
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 h1:uSoVVbwJiQipAclBbw+8quDsfcvFjOpI5iCf4p/cqCs=
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 h1:kFOfPq6dUM1hTo4JG6LR5AXSUEsOjtdm0kw0FtQtMJA=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
Expand Down Expand Up @@ -51,12 +55,15 @@ github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrk
github.com/openzipkin/zipkin-go v0.1.1/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTmOf0Erfk+hxe8=
github.com/pelletier/go-buffruneio v0.2.0 h1:U4t4R6YkofJ5xHm3dJzuRpPZ0mr5MMCoAWooScCR7aA=
github.com/pelletier/go-buffruneio v0.2.0/go.mod h1:JkE26KsDizTr40EUHkXVtNPvgGtbSNq5BcowyYOWdKo=
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4 h1:49lOXmGaUpV9Fz3gd7TFZY106KVlPVa5jcYD1gaQf98=
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
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 v0.8.0/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e h1:n/3MEhJQjQxrOUCzh1Y3Re6aJUUWRp2M9+Oc3eVn/54=
github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
Expand All @@ -68,6 +75,8 @@ github.com/src-d/gcfg v1.3.0/go.mod h1:p/UMsR43ujA89BJY9duynAwIpvqEujIH/jFlfL7jW
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/tsuru/tsuru v0.0.0-20190213172318-ad6c1309d678 h1:WoWDQ0TEgGHVl12oX3V6Q6XqPrC9bqngiTFBuVnZw9A=
github.com/tsuru/tsuru v0.0.0-20190213172318-ad6c1309d678/go.mod h1:8EIA5MXZUPRxQR1lf8kQkcJVss0q+lv0L8f2nb23siQ=
github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/xanzy/ssh-agent v0.2.0 h1:Adglfbi5p9Z0BmK2oKU9nTG+zKfniSfnaMYB+ULd+Ro=
Expand Down Expand Up @@ -106,6 +115,8 @@ google.golang.org/genproto v0.0.0-20181016170114-94acd270e44e/go.mod h1:JiN7NxoA
google.golang.org/grpc v1.14.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw=
google.golang.org/grpc v1.15.0 h1:Az/KuahOM4NAidTEuJCv/RonAA7rYsTPkqXVjr+8OOw=
google.golang.org/grpc v1.15.0/go.mod h1:0JHn/cJsOMiMfNA9+DeHDlAU7KAAB5GDlYFpa9MZMio=
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/src-d/go-billy.v4 v4.3.0 h1:KtlZ4c1OWbIs4jCv5ZXrTqG8EQocr0g/d4DjNg70aek=
Expand Down
22 changes: 22 additions & 0 deletions handle/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package handle

import (
"encoding/json"
"fmt"
"net/http"
"time"

"github.com/go-chi/chi"
"github.com/rodrigo-brito/gocity/handle/middlewares"

log "github.com/sirupsen/logrus"

"github.com/rodrigo-brito/gocity/utils"
Expand Down Expand Up @@ -76,3 +80,21 @@ func (h *AnalyzerHandle) Handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write(result)
}

func (h *AnalyzerHandle) Serve(port int) error {
router := chi.NewRouter()
cors := middlewares.GetCors("*")
baseURL := fmt.Sprintf("http://localhost:%d", port)

router.Use(cors.Handler)
router.Use(middlewares.APIHeader(fmt.Sprintf("%s/api", baseURL)))

router.Get("/*", FrontEndProxy)
router.Get("/api", h.Handler)
router.Get("/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})

log.Infof("Server started at %s", baseURL)
return http.ListenAndServe(fmt.Sprintf(":%d", port), router)
}
7 changes: 0 additions & 7 deletions handle/healthcheck.go

This file was deleted.

31 changes: 31 additions & 0 deletions handle/middlewares/headers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package middlewares

import (
"net/http"
"time"
)

const cookieKey = "gocity_api"

type Middleware func(http.Handler) http.Handler

func setAPICookie(w http.ResponseWriter, url string) {
expire := time.Now().Add(24 * time.Hour)
cookie := http.Cookie{
Name: cookieKey,
Value: url,
Path: "/",
Expires: expire,
MaxAge: 90000,
}
http.SetCookie(w, &cookie)
}

func APIHeader(APIUrl string) Middleware {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
setAPICookie(w, APIUrl)
next.ServeHTTP(w, r)
})
}
}
31 changes: 31 additions & 0 deletions handle/proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package handle

import (
"net/http"
"net/http/httputil"
"net/url"

"github.com/prometheus/common/log"
)

const frontendURL = "https://go-city.github.io"

func FrontEndProxy(w http.ResponseWriter, baseRequest *http.Request) {
baseURL, err := url.Parse(frontendURL)
if err != nil {
log.Error(err)
w.WriteHeader(http.StatusServiceUnavailable)
}

proxy := httputil.NewSingleHostReverseProxy(&url.URL{
Scheme: baseURL.Scheme,
Host: baseURL.Host,
})

req := baseRequest
req.URL.Scheme = baseURL.Scheme
req.URL.Host = baseURL.Host
req.Host = baseURL.Host

proxy.ServeHTTP(w, req)
}
44 changes: 26 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,28 @@ package main
import (
"context"
"fmt"
"net/http"
"os"
"time"

"github.com/pkg/browser"

"github.com/rodrigo-brito/gocity/utils"

"github.com/go-chi/chi"
"github.com/rodrigo-brito/gocity/handle"
"github.com/rodrigo-brito/gocity/handle/middlewares"
"github.com/rodrigo-brito/gocity/lib"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)

const EnvKeyGCS = "GOOGLE_APPLICATION_CREDENTIALS"
const (
EnvKeyGCS = "GOOGLE_APPLICATION_CREDENTIALS"
defaultPort = 4000
)

func main() {
storage := lib.Storage(new(lib.NoStorage))
router := chi.NewRouter()
cache := lib.NewCache()

// Use Google Cloud Storage for cache, if credentials available
if credentials := os.Getenv(EnvKeyGCS); len(credentials) > 0 {
var err error
storage, err = lib.NewGCS(context.Background())
Expand All @@ -30,8 +33,6 @@ func main() {
}
}

corsMiddleware := middlewares.GetCors("*")
router.Use(corsMiddleware.Handler)
analyzer := handle.AnalyzerHandle{
Cache: cache,
Storage: storage,
Expand All @@ -40,32 +41,39 @@ func main() {
app := cli.NewApp()
app.Version = "1.0.0"
app.Description = "Code City metaphor for visualizing Go source code in 3D"
app.Author = "Rodrigo Brito"
app.Author = "Rodrigo Brito (https://github.com/rodrigo-brito)"

app.Commands = []cli.Command{
{
Name: "server",
Description: "Start a local server to analyze projects",
Action: func(c *cli.Context) error {
router.Get("/api", analyzer.Handler)
router.Get("/health", handle.HealthCheck)

log.Println("Server started at http://localhost:4000")

return http.ListenAndServe(":4000", router)
return analyzer.Serve(defaultPort)
},
},
{
Name: "open",
Description: "Open a given project in local server",
Action: func(c *cli.Context) error {
fmt.Println(c.Args().First())
return nil
baseURL := c.Args().First()
url, ok := utils.GetGithubBaseURL(c.Args().First())
if !ok {
return fmt.Errorf("invalid project URL: %s", baseURL)
}
go openVisualization(url, time.Second)
return analyzer.Serve(defaultPort)
},
},
}

if err := app.Run(os.Args); err != nil {
log.Fatal(err)
log.Error(err)
}
}

func openVisualization(url string, delay time.Duration) {
time.Sleep(delay)
cityURL := fmt.Sprintf("http://localhost:%d/#/%s", defaultPort, url)
log.Info("opening ", cityURL)
browser.OpenURL(cityURL)
}
27 changes: 27 additions & 0 deletions vendor/github.com/alecthomas/template/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions vendor/github.com/alecthomas/template/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 705567f

Please sign in to comment.