Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Implement GET APIs for posts #574

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/deploy-backend-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ jobs:

- name: Build backend and copy zip to S3
run: |
secret_value="${{ secrets.RECAPTCHA_CONFIG_JSON_BASE64 }}"
awk -v val="${secret_value}" '{gsub(/{{RECAPTCHA_CONFIG_JSON_BASE64}}/, val)} 1' utils/helper.go > recatpcha_config_key.go && mv recatpcha_config_key.go utils/helper.go
apt-get update && apt-get install -y zip
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o main main.go
zip canopas_serverless_dev_${{ github.sha }}.zip main
Expand Down Expand Up @@ -55,15 +57,15 @@ jobs:
DbHost=${{ secrets.DB_HOST }},
DbPort=${{ secrets.DB_PORT }},
DbName=${{ secrets.DB_NAME }},
RecaptchaConfigJSONBase64=${{ secrets.RECAPTCHA_CONFIG_JSON_BASE64 }},
BlogDbName=${{ secrets.BLOG_DB_NAME }},
RecaptchaSiteKey=${{ secrets.RECAPTCHA_SITE_KEY }},
RecaptchaProjectId=${{ secrets.RECAPTCHA_PROJECT_ID }},
JobsSpreadsheetId=${{ secrets.JOBS_SPREADSHEET_ID }},
GithubAccessToken=${{ secrets.PERSONAL_ACCESS_TOKEN_GITHUB }},
ResourcesURL=${{secrets.RESOURCES_URL}},
LambdaBucket=canopas-lambda-handlers,
LambdaUrl=canopas_serverless_dev_${{ github.sha }}.zip,
LambdaTimeout=10,
LambdaTimeout=10,
LambdaRoleName=canopas-dev-lambda-role,
LambdaName=canopas-dev-lambda-function,
ApiGatewayName=canopas-dev-lambda-api,
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/deploy-backend-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ jobs:

- name: Build backend and copy zip to S3
run: |
secret_value="${{ secrets.RECAPTCHA_CONFIG_JSON_BASE64 }}"
awk -v val="${secret_value}" '{gsub(/{{RECAPTCHA_CONFIG_JSON_BASE64}}/, val)} 1' utils/helper.go > recatpcha_config_key.go && mv recatpcha_config_key.go utils/helper.go
apt-get update && apt-get install -y zip
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o main main.go
zip canopas_serverless_prod_${{ github.sha }}.zip main
Expand Down Expand Up @@ -55,7 +57,7 @@ jobs:
DbHost=${{ secrets.DB_HOST }},
DbPort=${{ secrets.DB_PORT }},
DbName=${{ secrets.DB_NAME }},
RecaptchaConfigJSONBase64=${{ secrets.RECAPTCHA_CONFIG_JSON_BASE64 }},
BlogDbName=${{ secrets.BLOG_DB_NAME }},
RecaptchaSiteKey=${{ secrets.RECAPTCHA_SITE_KEY }},
RecaptchaProjectId=${{ secrets.RECAPTCHA_PROJECT_ID }},
JobsSpreadsheetId=${{ secrets.JOBS_SPREADSHEET_ID }},
Expand Down
64 changes: 64 additions & 0 deletions db/blog_sql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package db

import (
"fmt"
"os"
"strings"
"time"

"github.com/jmoiron/sqlx"
"github.com/jmoiron/sqlx/reflectx"
_ "github.com/lib/pq"
log "github.com/sirupsen/logrus"
)

func BlogDBInstance() *sqlx.DB {

log.Info("Initializing New Blog Postgres Instance")

username := os.Getenv("DB_USERNAME")
if username == "" {
username = "docker_user"
}

password := os.Getenv("DB_PASSWORD")
if password == "" {
password = "docker_user"
}

host := os.Getenv("DB_HOST")
if host == "" {
host = "localhost"
}

port := os.Getenv("DB_PORT")
if port == "" {
port = "5433"
}

name := os.Getenv("BLOG_DB_NAME")
if name == "" {
name = "canopas_website"
}

sslmode := "require"

if os.Getenv("DB_ENV") == "test" {
sslmode = "disable"
}

var db *sqlx.DB

db, err := sqlx.Open("postgres", "postgres://"+username+":"+password+"@"+host+":"+port+"/"+name+"?sslmode="+sslmode)
if err != nil {
fmt.Println("Error while connecting to the blog database: ", err)
}

log.Info("Blog database instance initialized successfully")
db.Mapper = reflectx.NewMapperFunc("json", strings.ToLower)
db.SetConnMaxLifetime(time.Minute * 1)
db.SetConnMaxIdleTime(15 * time.Second)
db.SetMaxOpenConns(2)

return db
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module canopas-website

go 1.21.3

replace post => ./post

replace contact => ./contact

replace db => ./db
Expand Down Expand Up @@ -69,11 +71,13 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.9 // indirect
github.com/samber/lo v1.39.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/arch v0.4.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.11.0 // indirect
golang.org/x/sync v0.3.0 // indirect
Expand All @@ -90,4 +94,5 @@ require (
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df // indirect
gopkg.in/guregu/null.v3 v3.5.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
post v0.0.0-00010101000000-000000000000 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6L
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/samber/lo v1.39.0 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA=
github.com/samber/lo v1.39.0/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down Expand Up @@ -206,6 +208,8 @@ golang.org/x/crypto v0.0.0-20220314234659-1baeb1ce4c0b/go.mod h1:IxCIyHEi3zRg3s0
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 h1:3MTrJm4PyNL9NBqvYDSj3DHl46qQakyfqfWo4jgfaEM=
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
Expand Down
8 changes: 4 additions & 4 deletions infrastructure/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ Parameters:
DbName:
Type: String
Description: Postgres database name.
RecaptchaConfigJSONBase64:
BlogDbName:
Type: String
Description: Recaptcha enterprise credentials in base64 format
Description: Postgres database name for blogs.
RecaptchaSiteKey:
Type: String
Description: Use to verify recpatcha token.
Expand Down Expand Up @@ -166,8 +166,8 @@ Resources:
Fn::Sub: ${DbPort}
DB_NAME:
Fn::Sub: ${DbName}
RECAPTCHA_CONFIG_JSON_BASE64:
Fn::Sub: ${RecaptchaConfigJSONBase64}
BLOG_DB_NAME:
Fn::Sub: ${BlogDbName}
RECAPTCHA_SITE_KEY:
Fn::Sub: ${RecaptchaSiteKey}
RECAPTCHA_PROJECT_ID:
Expand Down
12 changes: 12 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net/http"
"notification"
"os"
"post"
"sitemap"
"utils"

Expand Down Expand Up @@ -93,6 +94,17 @@ func setupRouter() *gin.Engine {

router.GET("/api/perksimages", lifePerksImagesRepo.PerksImages)

blogSqlDb := db.BlogDBInstance()

postRepo := post.New(blogSqlDb)

router.GET("/api/posts", postRepo.Get)
router.GET("/api/posts/:slug", postRepo.Show)

router.GET("/api/posts/tags/:slug", postRepo.GetPostsByTag)

router.GET("/api/posts/author/:username", postRepo.GetPostsByAuthor)

return router
}

Expand Down
4 changes: 2 additions & 2 deletions nuxt-frontend/pages/[slug].vue
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ if (status.value !== config.SUCCESS) {
published_time = new Date(post.value?.published_on).toLocaleTimeString();
}

const CTAData = post.value?.cta?.data;
CTACompName.value = CTAData?.attributes.component_name;
const CTAData = post.value?.cta;
CTACompName.value = CTAData?.component_name;

useHead({
script: [
Expand Down
53 changes: 17 additions & 36 deletions nuxt-frontend/stores/author/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,52 +18,33 @@ export const useAuthorListStore = defineStore("authors", {
this.isLoading = true;
this.error = null;

let published = showDrafts
? "&publicationState=preview"
: "&publicationState=live";
let published = showDrafts ? "is_published=false" : "is_published=true";

const limitQuery = limit
? "&pagination[start]=" + start + "&pagination[limit]=" + limit
: "";
const limitQuery = limit ? "&skip=" + start + "&limit=" + limit : "";

let url =
config.STRAPI_URL +
"/v1/paginate?populate=deep" +
published +
"&filters[author][username]=" +
config.API_BASE +
"/api/posts/author/" +
slug +
"&fields[0]=title&fields[1]=slug&fields[2]=published_on&fields[3]=summary&fields[4]=reading_time&fields[5]=tags" +
"?" +
published +
limitQuery;

axios
.request({
timeout: 2000,
method: "GET",
url: config.STRAPI_URL + "/favicon.ico",
})
.then(() => {
axios
.get(url)
.then((response) => {
let posts = [];
response.data.data.forEach((post) => {
posts.push(setPostFields(post));
});
this.items = posts;
this.isLoading = false;
this.status =
posts.length > 0 ? response.status : config.NOT_FOUND;
resolve();
})
.catch((error) => {
this.error = error;
this.isLoading = false;
this.status = config.NOT_FOUND;
resolve();
});
.get(url)
.then((response) => {
let posts = [];
response.data.forEach((post) => {
posts.push(setPostFields(post));
});
this.items = posts;
this.isLoading = false;
this.status = posts.length > 0 ? response.status : config.NOT_FOUND;
resolve();
})
.catch((error) => {
this.error = error;
this.isLoading = false;
this.status = config.NOT_FOUND;
reject(error);
});
Expand Down
Loading
Loading