Skip to content

Commit

Permalink
Merge pull request #35 from algo7/fix/dynamic_r2_url
Browse files Browse the repository at this point in the history
Fix/dynamic r2 url
  • Loading branch information
algo7 committed Sep 28, 2023
2 parents de0093f + 2c4ab7c commit dd908f7
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 19 deletions.
9 changes: 5 additions & 4 deletions container_provisioner/api/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"encoding/json"
"fmt"
"os"
"strconv"
"strings"

Expand All @@ -13,8 +14,8 @@ import (
"github.com/gofiber/fiber/v2"
)

// R2Url is the URL of the R2 bucket
var R2Url = "https://storage.algo7.tools/"
// The URL of the R2 bucket
var r2Url string = os.Getenv("R2_URL")

type enrichedR2Obj struct {
FileName string
Expand Down Expand Up @@ -70,7 +71,7 @@ func getMain(c *fiber.Ctx) error {
for i, r2Obj := range R2ObjMetaData {
enrichedR2Objs[i] = enrichedR2Obj{
FileName: r2Obj.Key,
Link: R2Url + r2Obj.Key,
Link: r2Url + r2Obj.Key,
UploadedBy: r2Obj.Metadata,
Date: utils.ParseTime(r2Obj.LastModified),
}
Expand Down Expand Up @@ -142,7 +143,7 @@ func postProvision(c *fiber.Ctx) error {
"ContainerId": containerID,
"UploadID": fmt.Sprintf("Your Upload ID: %s", uploadIdentifier),
"ReturnHome": false,
// "URL": R2Url + fileSuffix + "-" + "0" + "_" + hotelName + ".csv",
// "URL": r2Url + fileSuffix + "-" + "0" + "_" + hotelName + ".csv",

})
}
Expand Down
1 change: 1 addition & 0 deletions container_provisioner/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ services:
environment:
IS_CONTAINER: true
REDIS_HOST: redis:6379
R2_URL: https://storage.algo7.tools/
# Image name
image: ghcr.io/algo7/tripadvisor-review-scraper/container_provisioner:latest
volumes:
Expand Down
8 changes: 8 additions & 0 deletions container_provisioner/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package main

import (
"log"
"os"

"github.com/algo7/TripAdvisor-Review-Scraper/container_provisioner/api"
"github.com/algo7/TripAdvisor-Review-Scraper/container_provisioner/database"
)

func main() {

// Check if the R2_URL environment variable is set
if os.Getenv("R2_URL") == "" {
log.Fatal("R2_URL environment variable not set")
}

// Check if the redis server is up and running
database.RedisConnectionCheck()

Expand Down
10 changes: 5 additions & 5 deletions container_provisioner/utils/cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ var (
// Read the creds from the JSON file
data = ParseCredsFromJSON("./credentials/creds.json")
// Create a new R2 client
r2Client = CreateR2Client(data.AccessKeyId, data.AccessKeySecret, data.AccountId)
r2Client = CreateR2Client(data.AccessKeyID, data.AccessKeySecret, data.AccountID)
ctx = context.TODO()
)

// R2 object struct
// R2Obj is an object struct for R2 bucket objects
type R2Obj struct {
ChecksumAlgorithm string `json:"checksumAlgorithm"`
Etag string `json:"Etag"`
Expand Down Expand Up @@ -87,21 +87,21 @@ func R2ListObjects() []R2Obj {
}

// CreateR2Client creates a new R2 client
func CreateR2Client(accessKeyId string, accessKeySecret string, accountId string) *r2.Client {
func CreateR2Client(accessKeyID string, accessKeySecret string, accountID string) *r2.Client {

// Logic from the documentation
r2Resolver := aws.EndpointResolverWithOptionsFunc(func(service string, region string, options ...interface{}) (aws.Endpoint, error) {

// Logic from the documentation
return aws.Endpoint{
URL: fmt.Sprintf("https://%s.r2.cloudflarestorage.com", accountId),
URL: fmt.Sprintf("https://%s.r2.cloudflarestorage.com", accountID),
}, nil
})

// Load the default configuration
cfg, err := config.LoadDefaultConfig(ctx,
config.WithEndpointResolverWithOptions(r2Resolver),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(accessKeyId, accessKeySecret, "")),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(accessKeyID, accessKeySecret, "")),
)
ErrorHandler(err)

Expand Down
18 changes: 9 additions & 9 deletions container_provisioner/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"os"
"regexp"
"sort"
Expand All @@ -14,19 +15,18 @@ import (
"github.com/google/uuid"
)

type Creds struct {
AccessKeyId string `json:"accessKeyId"`
type creds struct {
AccessKeyID string `json:"accessKeyId"`
AccessKeySecret string `json:"accessKeySecret"`
AccountId string `json:"accountId"`
AccountID string `json:"accountId"`
BucketName string `json:"bucketName"`
}

// ErrorHandler is a generic error handler
func ErrorHandler(err error) {
if err != nil {
formattedError := fmt.Errorf("Error: %w", err)
fmt.Println(formattedError)
fmt.Print(err)
log.Fatalln(formattedError)
}
}

Expand Down Expand Up @@ -85,15 +85,15 @@ func ReadFromFile(fileName string) *os.File {
return file
}

// ParseCreds parses the credentials from a JSON file
func ParseCredsFromJSON(fileName string) Creds {
// ParseCredsFromJSON parses the credentials from a JSON file
func ParseCredsFromJSON(fileName string) creds {
// Read file
file := ReadFromFile(fileName)
defer file.Close()

// Parse the JSON file
decoder := json.NewDecoder(file)
var creds Creds
var creds creds
err := decoder.Decode(&creds)
ErrorHandler(err)

Expand All @@ -111,7 +111,7 @@ func GetHotelNameFromURL(url string) string {
return fileName
}

// ValidateTripAdvisorURL validates the TripAdvisor Hotel URL
// ValidateTripAdvisorHotelURL validates the TripAdvisor Hotel URL
func ValidateTripAdvisorHotelURL(url string) bool {
regex := `^https:\/\/www\.tripadvisor\.com\/Hotel_Review-g\d{6,10}-d\d{1,10}-Reviews-[\w-]{1,255}\.html$`
match, _ := regexp.MatchString(regex, url)
Expand Down
2 changes: 1 addition & 1 deletion container_provisioner/views/tasks.html
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
<tbody>
{{range .RunningTasks}}
<tr>
<td>{{.ContainerId}}</td>
<td>{{.ContainerID}}</td>
<td>{{.HotelName}}</td>
<td>{{.TaskOwner}}</td>
<td><a href="{{.Url}}">View Logs</a></td>
Expand Down

0 comments on commit dd908f7

Please sign in to comment.