From 8bcc7d1930e21dbfe9cd448643c218501e33daf6 Mon Sep 17 00:00:00 2001 From: Roman Serikov Date: Tue, 1 Dec 2020 21:28:54 +0300 Subject: [PATCH] Added: contracts custom titles and descriptions to nginx conf --- Makefile | 2 +- scripts/nginx/main.go | 12 +++- scripts/nginx/nginx.go | 148 +++++++++++++++++++++++---------------- scripts/nginx/sitemap.go | 49 +++---------- 4 files changed, 107 insertions(+), 104 deletions(-) diff --git a/Makefile b/Makefile index b0127494d..da9cc4402 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/scripts/nginx/main.go b/scripts/nginx/main.go index 41080a387..cc96b7f72 100644 --- a/scripts/nginx/main.go +++ b/scripts/nginx/main.go @@ -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" ) @@ -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) @@ -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) } } diff --git a/scripts/nginx/nginx.go b/scripts/nginx/nginx.go index 7da5e9b8c..d8f97a9e2 100644 --- a/scripts/nginx/nginx.go +++ b/scripts/nginx/nginx.go @@ -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 { @@ -39,41 +40,85 @@ const defaultConfTemplate = `server { } }` -const dappLocationTemplate = ` - location /dapps/{{.slug}} { +const locationTemplate = ` + location {{.location}} { rewrite ^ /index.html break; - sub_filter '{{.pageTitle}}' '{{.title}}'; + sub_filter '{{.pageTitle}}' '{{.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 { @@ -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 { @@ -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 } diff --git a/scripts/nginx/sitemap.go b/scripts/nginx/sitemap.go index 3c529a0b9..9998507b9 100644 --- a/scripts/nginx/sitemap.go +++ b/scripts/nginx/sitemap.go @@ -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) @@ -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 }