Skip to content

Commit

Permalink
Adding the CDN for avatars
Browse files Browse the repository at this point in the history
  • Loading branch information
COMTOP1 committed Feb 16, 2025
1 parent 682f0df commit 601359b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
16 changes: 15 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/joho/godotenv"

"github.com/ystv/web-auth/utils"
"github.com/ystv/web-auth/views"
)

Expand Down Expand Up @@ -77,6 +78,19 @@ func main() {

domainName := os.Getenv("WAUTH_DOMAIN_NAME")

// CDN
cdnConfig := utils.CDNConfig{
Endpoint: os.Getenv("WAUTH_CDN_ENDPOINT"),
Region: os.Getenv("WAUTH_CDN_REGION"),
AccessKeyID: os.Getenv("WAUTH_CDN_ACCESSKEYID"),
SecretAccessKey: os.Getenv("WAUTH_CDN_SECRETACCESSKEY"),
}
cdn, err := utils.NewCDN(cdnConfig)
if err != nil {
log.Fatalf("Unable to connect to CDN: %v", err)
}
log.Printf("Connected to CDN: %s", cdnConfig.Endpoint)

// Generate config
conf := &views.Config{
Version: Version,
Expand All @@ -103,7 +117,7 @@ func main() {
},
}

v := views.New(conf, dbHost)
v := views.New(conf, dbHost, cdn)

router := NewRouter(&RouterConf{
Config: conf,
Expand Down
32 changes: 32 additions & 0 deletions utils/cdn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package utils

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)

// CDNConfig represents a configuration to connect to a CDN / S3 instance
type CDNConfig struct {
Endpoint string
Region string
AccessKeyID string
SecretAccessKey string
}

// NewCDN Initialise connection to CDN
func NewCDN(config CDNConfig) (*s3.S3, error) {
s3Config := &aws.Config{
Credentials: credentials.NewStaticCredentials(config.AccessKeyID, config.SecretAccessKey, ""),
Endpoint: aws.String(config.Endpoint),
Region: aws.String(config.Region),
S3ForcePathStyle: aws.Bool(true),
}
newSession, err := session.NewSession(s3Config)
if err != nil {
return nil, err
}
cdn := s3.New(newSession)
return cdn, nil
}
6 changes: 5 additions & 1 deletion views/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"time"

"github.com/aws/aws-sdk-go/service/s3"
"github.com/go-playground/validator/v10"
"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
Expand Down Expand Up @@ -60,6 +61,7 @@ type (
Views struct {
api api.Repo
cache *cache.Cache
cdn *s3.S3
conf *Config
cookie *sessions.CookieStore
crowd crowd.Repo
Expand All @@ -81,7 +83,7 @@ type (
)

// New initialises connections, templates, and cookies
func New(conf *Config, host string) *Views {
func New(conf *Config, host string, cdn *s3.S3) *Views {
v := &Views{}
// Connecting to stores
dbStore := db.NewStore(conf.DatabaseURL, host)
Expand All @@ -92,6 +94,8 @@ func New(conf *Config, host string) *Views {
v.api = api.NewAPIRepo(dbStore)
v.crowd = crowd.NewCrowdRepo(dbStore)

v.cdn = cdn

v.template = templates.NewTemplate(v.permission, v.role, v.user)

// Initialising cache
Expand Down

0 comments on commit 601359b

Please sign in to comment.