Skip to content

Commit

Permalink
Include port customization and remove cloud storage
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigo-brito committed Mar 2, 2019
1 parent 7058f66 commit 99cc003
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 78 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@
*/**/node_modules

# Project files
gcs-credentials.json
gocity
coverage.txt
6 changes: 0 additions & 6 deletions Dockerfile

This file was deleted.

10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ dev-dependencies:
go install github.com/canthefason/go-watcher/cmd/watcher

watcher: dev-dependencies
GOOGLE_APPLICATION_CREDENTIALS=`pwd`/gcs-credentials.json watcher # github.com/canthefason/go-watcher
watcher # github.com/canthefason/go-watcher

mock:
go get github.com/vektra/mockery/...
mockery -output testdata/mocks -dir ./lib -all
mockery -output testdata/mocks -dir ./analyzer -all

run:
docker run -ti -v`pwd`:/go/src/github.com/rodrigo-brito/gocity -e "GOOGLE_APPLICATION_CREDENTIALS=/go/src/github.com/rodrigo-brito/gocity/gcs-credentials.json" -p80:4000 -d -w /go/src/github.com/rodrigo-brito/gocity golang go run main.go

test:
echo "" > coverage.txt
for d in $(shell go list ./... | grep -v vendor); do \
go test -race -v -coverprofile=profile.out -covermode=atomic $$d || exit 1; \
[ -f profile.out ] && cat profile.out >> coverage.txt && rm profile.out; \
done

build-docker:
docker build -t rodrigobrito/gocity -f ./server/docker/Dockerfile .

deploy:
we deploy -p rodrigo
cd server && we deploy -p rodrigo
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<img width="500" src="https://raw.githubusercontent.com/rodrigo-brito/gocity/master/logo.png" alt="GoCity" />
<img width="500" src="https://raw.githubusercontent.com/rodrigo-brito/gocity/master/assets/logo.png" alt="GoCity" />

[![Build Status](https://travis-ci.org/rodrigo-brito/gocity.svg?branch=master)](https://travis-ci.org/rodrigo-brito/gocity)
[![codecov](https://codecov.io/gh/rodrigo-brito/gocity/branch/master/graph/badge.svg)](https://codecov.io/gh/rodrigo-brito/gocity)
Expand Down
File renamed without changes
23 changes: 0 additions & 23 deletions docker/docker-compose.yml

This file was deleted.

16 changes: 13 additions & 3 deletions handle/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import (
)

type AnalyzerHandle struct {
Storage lib.Storage
Cache lib.Cache
Storage lib.Storage
Cache lib.Cache
projectURL *string
}

func (h *AnalyzerHandle) Handler(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -81,6 +82,10 @@ func (h *AnalyzerHandle) Handler(w http.ResponseWriter, r *http.Request) {
w.Write(result)
}

func (h *AnalyzerHandle) SetProjectURL(URL string) {
h.projectURL = &URL
}

func (h *AnalyzerHandle) Serve(port int) error {
router := chi.NewRouter()
cors := middlewares.GetCors("*")
Expand All @@ -95,6 +100,11 @@ func (h *AnalyzerHandle) Serve(port int) error {
w.WriteHeader(http.StatusOK)
})

log.Infof("Server started at %s", baseURL)
if h.projectURL != nil {
log.Infof("Visualization available at: %s/#/%s", baseURL, *h.projectURL)
} else {
log.Infof("Server started at %s", baseURL)
}

return http.ListenAndServe(fmt.Sprintf(":%d", port), router)
}
20 changes: 19 additions & 1 deletion lib/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"os"

"cloud.google.com/go/storage"
)

// Sets the name for the new bucket.
const bucketName = "gocity-cache"
const (
bucketName = "gocity-cache"
EnvKeyGCS = "GOOGLE_APPLICATION_CREDENTIALS"
)

type Storage interface {
Get(projectName string) (bool, []byte, error)
Expand Down Expand Up @@ -96,3 +101,16 @@ func (NoStorage) Save(projectName string, content []byte) error {
func (NoStorage) Delete(projectName string) error {
return nil
}

func NewStorage() Storage {
if credentials := os.Getenv(EnvKeyGCS); len(credentials) > 0 {
var err error
storage, err := NewGCS(context.Background())
if err != nil {
log.Fatal(err)
}
return storage
}

return new(NoStorage)
}
43 changes: 14 additions & 29 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,24 @@
package main

import (
"context"
"fmt"
"os"
"time"

"github.com/pkg/browser"

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

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

const (
EnvKeyGCS = "GOOGLE_APPLICATION_CREDENTIALS"
defaultPort = 4000
)

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

if credentials := os.Getenv(EnvKeyGCS); len(credentials) > 0 {
var err error
storage, err = lib.NewGCS(context.Background())
if err != nil {
log.Fatal(err)
}
}

analyzer := handle.AnalyzerHandle{
Cache: cache,
Storage: storage,
Cache: lib.NewCache(),
Storage: lib.NewStorage(),
}

app := cli.NewApp()
Expand All @@ -47,8 +30,16 @@ func main() {
{
Name: "server",
Description: "Start a local server to analyze projects",
Flags: []cli.Flag{
cli.IntFlag{
Name: "port",
Value: defaultPort,
Usage: "Local server port",
},
},
Action: func(c *cli.Context) error {
return analyzer.Serve(defaultPort)
port := c.Int("port")
return analyzer.Serve(port)
},
},
{
Expand All @@ -60,7 +51,8 @@ func main() {
if !ok {
return fmt.Errorf("invalid project URL: %s", baseURL)
}
go openVisualization(url, time.Second)

analyzer.SetProjectURL(url)
return analyzer.Serve(defaultPort)
},
},
Expand All @@ -70,10 +62,3 @@ func main() {
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)
}
2 changes: 2 additions & 0 deletions server/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM rodrigobrito/gocity
EXPOSE 4000
File renamed without changes.
6 changes: 6 additions & 0 deletions server/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM golang:1.12
RUN mkdir -p /gocity
WORKDIR /gocity
ADD . .
RUN go build -mod vendor
CMD ["/gocity/gocity", "server"]
14 changes: 14 additions & 0 deletions server/docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: '3'
services:
caddy:
image: "abiosoft/caddy"
ports:
- "80:80"
- "443:443"
volumes:
- "./server/docker/Caddyfile:/etc/Caddyfile"
- "/root/.caddy"
depends_on:
- api
links:
- api
6 changes: 6 additions & 0 deletions server/wedeploy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"id": "gocity",
"memory": 1024,
"cpu": 1,
"port": 4000
}
9 changes: 0 additions & 9 deletions wedeploy.json

This file was deleted.

0 comments on commit 99cc003

Please sign in to comment.