Skip to content

Commit

Permalink
Added: contracts custom titles and descriptions to nginx conf
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanSerikov authored and m-kus committed Dec 3, 2020
1 parent 6dd2eb9 commit 8bcc7d1
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 104 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ ifeq ($(BCD_ENV), development)
cd scripts/nginx && go run .
else
docker exec -it $$BCD_ENV-api seo
endif
docker restart $$BCD_ENV-gui
endif

migration:
ifeq ($(BCD_ENV), development)
Expand Down
12 changes: 10 additions & 2 deletions scripts/nginx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"github.com/baking-bad/bcdhub/internal/config"
"github.com/baking-bad/bcdhub/internal/contractparser/consts"
"github.com/baking-bad/bcdhub/internal/logger"
)

Expand All @@ -25,6 +26,11 @@ func main() {
logger.Fatal(err)
}

aliases, err := ctx.ES.GetAliases(consts.Mainnet)
if err != nil {
logger.Fatal(err)
}

outputDir := fmt.Sprintf("%s/nginx", cfg.SharePath)
_ = os.Mkdir(outputDir, os.ModePerm)

Expand All @@ -33,11 +39,13 @@ func main() {
logger.Fatal(fmt.Errorf("BCD_ENV env var is empty"))
}

if err := makeNginxConfig(ctx, dapps, outputDir, env); err != nil {
nginxConfigFilename := fmt.Sprintf("%s/default.%s.conf", outputDir, env)
if err := makeNginxConfig(dapps, aliases, nginxConfigFilename, ctx.Config.BaseURL); err != nil {
logger.Fatal(err)
}

if err := makeSitemap(ctx, dapps, outputDir, env); err != nil {
sitemapFilename := fmt.Sprintf("%s/sitemap.%s.xml", outputDir, env)
if err := makeSitemap(dapps, aliases, sitemapFilename, ctx.Config); err != nil {
logger.Fatal(err)
}
}
148 changes: 88 additions & 60 deletions scripts/nginx/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ import (
"strings"
"text/template"

"github.com/baking-bad/bcdhub/internal/config"
"github.com/baking-bad/bcdhub/internal/logger"
"github.com/baking-bad/bcdhub/internal/models"
"github.com/baking-bad/bcdhub/internal/models/tzip"
)

const (
ogTitle = "Better Call Dev"
ogDescription = "Tezos smart contract explorer, developer dashboard, and API provider. Easy to spin up / integrate with your sandbox."
ogImage = "/img/logo_og.png"
pageTitle = "Better Call Dev — Tezos smart contract explorer by Baking Bad"
dappsTitle = "Tezos DApps"
dappsDescription = "Track the Tezos ecosystem growth: aggregated DApps usage stats, DEX token turnover, affiliated smart contracts, screenshots, social links, and more."
ogTitle = "Better Call Dev"
ogDescription = "Tezos smart contract explorer, developer dashboard, and API provider. Easy to spin up / integrate with your sandbox."
ogImage = "/img/logo_og.png"
pageTitle = "Better Call Dev — Tezos smart contract explorer by Baking Bad"
dappsTitle = "Tezos DApps"
dappsDescription = "Track the Tezos ecosystem growth: aggregated DApps usage stats, DEX token turnover, affiliated smart contracts, screenshots, social links, and more."
contractDescription = "Check out recent operations, inspect contract code and storage, invoke contract methods."
)

const defaultConfTemplate = `server {
Expand All @@ -39,41 +40,85 @@ const defaultConfTemplate = `server {
}
}`

const dappLocationTemplate = `
location /dapps/{{.slug}} {
const locationTemplate = `
location {{.location}} {
rewrite ^ /index.html break;
sub_filter '<meta property=og:url content=/' '<meta property=og:url content={{.baseUrl}}/dapps/{{.slug}}';
sub_filter '<meta property=og:title content="{{.ogTitle}}"' '<meta property=og:title content="{{.title}}"';
sub_filter '<meta property=og:description content="{{.ogDescription}}"' '<meta property=og:description content="{{.description}}"';
sub_filter '<meta property=og:image content={{.ogImage}}' '<meta property=og:image content={{.logoURL}}';
sub_filter '<meta property=og:image:secure_url content={{.ogImage}}' '<meta property=og:image:secure_url content={{.logoURL}}';
sub_filter '<meta name=twitter:image content={{.ogImage}}' '<meta name=twitter:image content={{.logoURL}}';
sub_filter '<meta name=twitter:title content="{{.ogTitle}}"' '<meta name=twitter:title content="{{.title}}"';
sub_filter '<meta property=og:url content=/' '<meta property=og:url content={{.url}}';
sub_filter '<meta property=og:title content="{{.ogTitle}}"' '<meta property=og:title content="{{.title}}"';
sub_filter '<meta property=og:description content="{{.ogDescription}}"' '<meta property=og:description content="{{.description}}"';
sub_filter '<meta property=og:image content={{.ogImage}}' '<meta property=og:image content={{.logoURL}}';
sub_filter '<meta property=og:image:secure_url content={{.ogImage}}' '<meta property=og:image:secure_url content={{.logoURL}}';
sub_filter '<meta name=twitter:image content={{.ogImage}}' '<meta name=twitter:image content={{.logoURL}}';
sub_filter '<meta name=twitter:title content="{{.ogTitle}}"' '<meta name=twitter:title content="{{.title}}"';
sub_filter '<meta name=twitter:description content="{{.ogDescription}}"' '<meta name=twitter:description content="{{.description}}"';
sub_filter '<title>{{.pageTitle}}</title>' '<title>{{.title}}</title>';
sub_filter '<title>{{.pageTitle}}</title>' '<title>{{.title}}</title>';
sub_filter_once on;
}`

func makeDappLocation(dapp tzip.DApp, baseURL string) (string, error) {
logoURL := ""
func makeNginxConfig(dapps []tzip.DApp, aliases []models.TZIP, filepath, baseURL string) error {
var locations strings.Builder
tmpl := template.Must(template.New("").Parse(locationTemplate))

for _, dapp := range dapps {
loc, err := makeDappLocation(tmpl, dapp, baseURL)
if err != nil {
return err
}
locations.WriteString(loc)
locations.WriteString("\n")
}

loc, err := makeDappRootLocation(tmpl, "list", baseURL)
if err != nil {
return err
}
locations.WriteString(loc)
locations.WriteString("\n")

for _, alias := range aliases {
loc, err := makeContractsLocation(tmpl, alias.Address, alias.Name, baseURL)
if err != nil {
return err
}

locations.WriteString(loc)
locations.WriteString("\n")
}

defaultConf := fmt.Sprintf(defaultConfTemplate, locations.String())
file, err := os.Create(filepath)
if err != nil {
return err
}
defer file.Close()

if _, err = file.WriteString(defaultConf); err != nil {
logger.Fatal(err)
}

logger.Info("Nginx default config created in %s", filepath)

return nil
}

func makeDappLocation(tmpl *template.Template, dapp tzip.DApp, baseURL string) (string, error) {
var logoURL string
for _, picture := range dapp.Pictures {
if picture.Type == "logo" {
logoURL = picture.Link
}
}

buf := &bytes.Buffer{}
tmpl := template.Must(template.New("").Parse(dappLocationTemplate))

buf := new(bytes.Buffer)
err := tmpl.Execute(buf, map[string]interface{}{
"slug": dapp.Slug,
"location": fmt.Sprintf("/dapps/%s", dapp.Slug),
"url": fmt.Sprintf("%s/dapps/%s", baseURL, dapp.Slug),
"title": fmt.Sprintf("%s — %s", dapp.Name, dapp.ShortDescription),
"description": dapp.FullDescription,
"ogTitle": ogTitle,
"ogDescription": ogDescription,
"ogImage": ogImage,
"pageTitle": pageTitle,
"baseUrl": baseURL,
"logoURL": logoURL,
})
if err != nil {
Expand All @@ -83,19 +128,17 @@ func makeDappLocation(dapp tzip.DApp, baseURL string) (string, error) {
return buf.String(), nil
}

func makeDappRootLocation(path, baseURL string) (string, error) {
buf := &bytes.Buffer{}
tmpl := template.Must(template.New("").Parse(dappLocationTemplate))

func makeDappRootLocation(tmpl *template.Template, path, baseURL string) (string, error) {
buf := new(bytes.Buffer)
err := tmpl.Execute(buf, map[string]interface{}{
"slug": path,
"location": fmt.Sprintf("/dapps/%s", path),
"url": fmt.Sprintf("%s/dapps/%s", baseURL, path),
"title": dappsTitle,
"description": dappsDescription,
"ogTitle": ogTitle,
"ogDescription": ogDescription,
"ogImage": ogImage,
"pageTitle": pageTitle,
"baseUrl": baseURL,
"logoURL": ogImage,
})
if err != nil {
Expand All @@ -105,37 +148,22 @@ func makeDappRootLocation(path, baseURL string) (string, error) {
return buf.String(), nil
}

func makeNginxConfig(ctx *config.Context, dapps []tzip.DApp, outputDir, env string) error {
filePath := fmt.Sprintf("%s/default.%s.conf", outputDir, env)
file, err := os.Create(filePath)
if err != nil {
return err
}
defer file.Close()

var dappLocations strings.Builder
for _, dapp := range dapps {
loc, err := makeDappLocation(dapp, ctx.Config.BaseURL)
if err != nil {
return err
}
dappLocations.WriteString(loc)
dappLocations.WriteString("\n")
}

loc, err := makeDappRootLocation("list", ctx.Config.BaseURL)
func makeContractsLocation(tmpl *template.Template, address, alias, baseURL string) (string, error) {
buf := new(bytes.Buffer)
err := tmpl.Execute(buf, map[string]interface{}{
"location": fmt.Sprintf("/mainnet/%s", address),
"url": fmt.Sprintf("%s/mainnet/%s", baseURL, address),
"title": fmt.Sprintf("%s — %s", alias, ogTitle),
"description": contractDescription,
"ogTitle": ogTitle,
"ogDescription": ogDescription,
"ogImage": ogImage,
"pageTitle": pageTitle,
"logoURL": ogImage,
})
if err != nil {
return err
}
dappLocations.WriteString(loc)
dappLocations.WriteString("\n")

defaultConf := fmt.Sprintf(defaultConfTemplate, dappLocations.String())
if _, err = file.WriteString(defaultConf); err != nil {
logger.Fatal(err)
return "", err
}

logger.Info("nginx default config created in %s", filePath)

return nil
return buf.String(), nil
}
49 changes: 8 additions & 41 deletions scripts/nginx/sitemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,13 @@ import (
"fmt"

"github.com/baking-bad/bcdhub/internal/config"
"github.com/baking-bad/bcdhub/internal/contractparser/consts"
"github.com/baking-bad/bcdhub/internal/logger"
"github.com/baking-bad/bcdhub/internal/models"
"github.com/baking-bad/bcdhub/internal/models/tzip"
"github.com/baking-bad/bcdhub/scripts/nginx/pkg/sitemap"
)

func makeSitemap(ctx *config.Context, dapps []tzip.DApp, outputDir, env string) error {
aliases, err := ctx.ES.GetAliases(consts.Mainnet)
if err != nil {
return err
}

aliasModels := make([]models.TZIP, 0)

for _, a := range aliases {
if a.Slug == "" {
continue
}

data := models.TZIP{
Address: a.Address,
Network: a.Network,
}
if err := ctx.ES.GetByID(&data); err != nil {
continue
}

logger.Info("%s %s", a.Address, data.Name)

aliasModels = append(aliasModels, a)
}

// logger.Info("Total aliases: %d", len(aliasModels))

filename := fmt.Sprintf("%s/sitemap.%s.xml", outputDir, env)
if err := buildXML(aliasModels, ctx.Config, dapps, filename); err != nil {
return err
}

logger.Info("Sitemap created in sitemap.xml")

return nil
}

func buildXML(aliases []models.TZIP, cfg config.Config, dapps []tzip.DApp, filename string) error {
func makeSitemap(dapps []tzip.DApp, aliases []models.TZIP, filepath string, cfg config.Config) error {
s := sitemap.New()

s.AddLocation(cfg.BaseURL)
Expand All @@ -70,5 +31,11 @@ func buildXML(aliases []models.TZIP, cfg config.Config, dapps []tzip.DApp, filen
s.AddLocation(fmt.Sprintf("%s/dapps/%s", cfg.BaseURL, d.Slug))
}

return s.SaveToFile(filename)
if err := s.SaveToFile(filepath); err != nil {
return err
}

logger.Info("Sitemap created in %s", filepath)

return nil
}

0 comments on commit 8bcc7d1

Please sign in to comment.