Skip to content
This repository was archived by the owner on Nov 15, 2022. It is now read-only.

Commit 5b755e8

Browse files
authored
Issue #192 - Salvar Sitemap no CDN do Frontend (#318)
* poder usar "s3" localmente atraves do minio * salvar sitemap no s3 do frontend * updates sitemap on deploy * hosts on readme.md
1 parent 83fca31 commit 5b755e8

File tree

7 files changed

+95
-29
lines changed

7 files changed

+95
-29
lines changed

Makefile

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ migrations-on-docker: ## run migrations inside docker
5555
docker-compose up -d
5656
docker-compose exec golang sql-migrate up -config=devops/dbconfig.yml -env=docker
5757

58-
serve: ## start server
59-
docker-compose up -d postgres
58+
start-dependences: ## docker up all container dependencies
59+
docker-compose up -d postgres minio images-server
60+
61+
serve: start-dependences ## start server
6062
cd server && go run main.go serve
6163

6264
install-on-docker: ## install dependences from docker
@@ -67,8 +69,7 @@ serve-on-docker: ## start the server inside docker
6769
docker-compose up -d
6870
docker-compose exec golang sh -c "cd server && go run main.go serve"
6971

70-
serve-watch: ## start server with hot reload (bin=vitrine-social)
71-
docker-compose up -d postgres
72+
serve-watch: start-dependences ## start server with hot reload (bin=vitrine-social)
7273
go get -u github.com/codegangsta/gin
7374
cd server; API_PORT=8001 gin --port 8000 --appPort 8001 --bin $(bin) run serve
7475

@@ -87,9 +88,12 @@ docs-serve: ## start a server with the docs
8788
docs-build: ## build the docs
8889
cd docs && make build
8990

90-
docs-open:
91+
docs-open: ## opens the docs on your browser
9192
$$BROWSER docs/index.html
9293

94+
open-minio: ## opens the docs on your browser
95+
$$BROWSER localhost:9000
96+
9397
# Absolutely awesome: http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
9498
help: ## show this help
9599
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ Vitrine Social [![Codacy Badge](https://api.codacy.com/project/badge/Grade/5d73b
66

77
O controle das tarefas e do progresso das mesmas estão sendo feitas no Waffle. Clique aqui para acompanhar: https://waffle.io/Coderockr/vitrine-social
88

9-
109
## Instalação Backend (Go)
1110

1211
Estamos utilizando [Go Modules](https://github.com/golang/go/wiki/Modules) nesse projeto, por isso a pasta do projeto precisa ficar fora do seu `GOPATH`, ou terá que adicionar a ENV `GO111MODULE` como `on` em seu ambiente para que o projeto funcione dentro do `GOPATH`.
@@ -25,6 +24,17 @@ make migrations # isso pode falhar por causa do warmup do postgres
2524
make serve # agora esta rodando :)
2625
```
2726

27+
### Domínios e Subdomínios locais
28+
29+
Incluir os seguintes domínios no seu `/etc/hosts` deve agilizar o setup do seu projeto:
30+
31+
```sh
32+
127.0.0.1 api.vitrinesocial.test # usar porta 8000 (golang)
33+
127.0.0.1 images.vitrinesocial.test # usar porta 7000 (images-server)
34+
127.0.0.1 minio.vitrinesocial.test # usar porta 9000 (minio)
35+
127.0.0.1 vitrinesocial.test # usar porta 3000 (frontend)
36+
```
37+
2838
## Instalação Frontend (React)
2939

3040
```sh

devops/deploy.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ ssh -i ./devops/vitrinesocial.pem -t $DEPLOY_USER@$DEPLOY_HOST 'sudo systemctl s
1515

1616
# Run Migrations
1717
sql-migrate up -config=devops/dbconfig.yml -env=production
18+
19+
# Update sitemap
20+
ssh -i ./devops/vitrinesocial.pem -t $DEPLOY_USER@$DEPLOY_HOST './vitrine-social/vitrine-social sitemap-generate'

docker-compose.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,23 @@ services:
2828
working_dir: /vitrine-social
2929
links:
3030
- postgres
31+
minio:
32+
image: minio/minio:RELEASE.2018-10-18T00-28-58Z
33+
environment:
34+
MINIO_ACCESS_KEY: configAccessKeyId
35+
MINIO_SECRET_KEY: configSecretKey
36+
volumes:
37+
- "./.data/minio:/data"
38+
- "./.data/minio-config:/root/.minio"
39+
ports:
40+
- "9000:9000"
41+
entrypoint: sh
42+
command: -c 'mkdir -p /data/images.vitrinesocial.test /data/vitrinesocial.test && /usr/bin/minio server /data'
43+
images-server:
44+
image: halverneus/static-file-server:latest
45+
volumes:
46+
- "./.data/minio/images.vitrinesocial.test:/web"
47+
ports:
48+
- 7000:8080
49+
depends_on:
50+
- minio

server/cmd/sitemap.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@
1515
package cmd
1616

1717
import (
18+
"bytes"
1819
"fmt"
1920
"log"
2021
"os"
2122

2223
"github.com/Coderockr/vitrine-social/server/db"
2324
"github.com/Coderockr/vitrine-social/server/db/repo"
2425
"github.com/Coderockr/vitrine-social/server/model"
26+
"github.com/Coderockr/vitrine-social/server/storage"
27+
"github.com/graymeta/stow"
2528
"github.com/spf13/cobra"
2629
"github.com/thecodenation/go-sitemap-generator/stm"
2730
)
@@ -42,6 +45,12 @@ func sitemapFunc(cmd *cobra.Command, args []string) {
4245
var o []*model.Organization
4346
sm := generateSitemap()
4447

48+
storageContainer, err := storage.ConnectFrontend()
49+
if err != nil {
50+
log.Fatal(err.Error())
51+
os.Exit(1)
52+
}
53+
4554
conn, err := db.GetFromEnv()
4655
if err != nil {
4756
log.Fatal(err.Error())
@@ -66,7 +75,7 @@ func sitemapFunc(cmd *cobra.Command, args []string) {
6675
sm.Add(stm.URL{"loc": fmt.Sprintf("%s/v1/need/%d/share", os.Getenv("API_URL"), k.ID), "changefreq": "hourly", "priority": 0.8})
6776
}
6877
}
69-
err = saveSitemap(sm)
78+
err = saveSitemap(sm, storageContainer)
7079
if err != nil {
7180
log.Fatal(err.Error())
7281
os.Exit(1)
@@ -83,17 +92,16 @@ func generateSitemap() *stm.Sitemap {
8392
return sm
8493
}
8594

86-
func saveSitemap(sm *stm.Sitemap) error {
87-
f, err := os.Create("../frontend/public/sitemap.xml")
88-
if err != nil {
89-
return err
90-
}
91-
defer f.Close()
92-
_, err = f.Write(sm.XMLContent())
93-
if err != nil {
94-
return err
95-
}
96-
err = f.Sync()
95+
func saveSitemap(sm *stm.Sitemap, c stow.Container) error {
96+
content := sm.XMLContent()
97+
98+
_, err := c.Put(
99+
"sitemap.xml",
100+
bytes.NewBuffer(content),
101+
int64(len(content)),
102+
nil,
103+
)
104+
97105
if err != nil {
98106
return err
99107
}

server/config/dev.env.dist

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,27 @@ DATABASE_USER=postgres
33
DATABASE_PASSWORD=postgres
44
DATABASE_HOST=127.0.0.1
55
DATABASE_PORT=5432
6-
API_URL=http://vitrinesocial.test
6+
API_URL=http://api.vitrinesocial.test
77
API_PORT=8000
88
FRONTEND_URL=https://vitrinesocial.org
99
VITRINESOCIAL_PRIVATE_KEY=ThisIsASecretSalt
1010
VITRINESOCIAL_PUBLIC_KEY=
1111
VITRINESOCIAL_SIGNING_METHOD=HS256
12+
1213
STORAGE=s3
13-
IMAGE_STORAGE_BASE_URL=http://images.vitrinesocial.org/
14-
STORAGE_S3_BUCKET_NAME=images.vitrinesocial.org
14+
IMAGE_STORAGE_BASE_URL=http://images.vitrinesocial.test:7000/
15+
STORAGE_S3_BUCKET_NAME=images.vitrinesocial.test
16+
STORAGE_S3_ENDPOINT=http://127.0.0.1:9000
1517
STORAGE_S3_CONFIG_ACCESS_KEY_ID=configAccessKeyId
1618
STORAGE_S3_CONFIG_SECRET_KEY=configSecretKey
1719
STORAGE_S3_CONFIG_REGION=configRegion
1820
STORAGE_LOCAL_PATH=
1921
STORAGE_GOOGLE_PATH=
2022

23+
STORAGE_LOCAL_PATH_FRONTEND=../frontend/public
24+
STORAGE_GOOGLE_PATH_FRONTEND=
25+
STORAGE_S3_BUCKET_NAME_FRONTEND=vitrinesocial.test
26+
2127
BUGSNAG_KEY=
2228

2329
MAIL_METHOD=

server/storage/storage.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,36 @@ import (
44
"os"
55

66
"github.com/graymeta/stow"
7-
// support Google storage
8-
google "github.com/graymeta/stow/google"
9-
// support local storage
10-
local "github.com/graymeta/stow/local"
11-
// support s3 storage
12-
s3 "github.com/lucassabreu/stow/s3"
7+
google "github.com/graymeta/stow/google" // support Google storage
8+
local "github.com/graymeta/stow/local" // support local storage
9+
s3 "github.com/lucassabreu/stow/s3" // support s3 storage
1310
)
1411

1512
// Connect to storage and return the container
1613
func Connect() (stow.Container, error) {
14+
return connectTo("")
15+
}
16+
17+
// ConnectFrontend will connect to the frontends file container
18+
func ConnectFrontend() (stow.Container, error) {
19+
return connectTo("FRONTEND")
20+
}
21+
22+
func connectTo(sufix string) (stow.Container, error) {
1723
var kind string
1824
var config stow.ConfigMap
1925
var containerName string
2026

27+
if len(sufix) > 0 {
28+
sufix = "_" + sufix
29+
}
30+
2131
if os.Getenv("STORAGE") == "local" {
2232
kind = "local"
2333
config = stow.ConfigMap{
2434
local.ConfigKeyPath: os.Getenv("STORAGE_LOCAL_PATH"),
2535
}
26-
containerName = os.Getenv("STORAGE_LOCAL_PATH")
36+
containerName = os.Getenv("STORAGE_LOCAL_PATH" + sufix)
2737
}
2838

2939
if os.Getenv("STORAGE") == "s3" {
@@ -33,7 +43,12 @@ func Connect() (stow.Container, error) {
3343
s3.ConfigSecretKey: os.Getenv("STORAGE_S3_CONFIG_SECRET_KEY"),
3444
s3.ConfigRegion: os.Getenv("STORAGE_S3_CONFIG_REGION"),
3545
}
36-
containerName = os.Getenv("STORAGE_S3_BUCKET_NAME")
46+
47+
if endpoint := os.Getenv("STORAGE_S3_ENDPOINT"); len(endpoint) > 0 {
48+
config[s3.ConfigEndpoint] = endpoint
49+
}
50+
51+
containerName = os.Getenv("STORAGE_S3_BUCKET_NAME" + sufix)
3752
}
3853

3954
if os.Getenv("STORAGE") == "google" {

0 commit comments

Comments
 (0)