diff --git a/.gitignore b/.gitignore index d8893996e28df927f473d67c23bce572aee09200..9513bf1c668686c72953f5b1f6ac8f0bd687af0a 100644 --- a/.gitignore +++ b/.gitignore @@ -88,6 +88,6 @@ ehthumbs.db Thumbs.db # Personal ignore patterns -**/firebasecredentials.json +**/firebaseCredentials.json **/.secrets .env \ No newline at end of file diff --git a/Go/.dockerignore b/Go/.dockerignore index 9b2b66851ab8e85967613e7e989357eb76a9afdd..002b2a5c46c9cc387b754b2a1efb318cd39a4784 100644 --- a/Go/.dockerignore +++ b/Go/.dockerignore @@ -88,7 +88,7 @@ ehthumbs.db Thumbs.db # Personal ignore patterns -**/firebasecredentials.json +**/firebaseCredentials.json **/.secrets .env diff --git a/Go/.env.example b/Go/.env.example index 056673cb7b779a73ad5f5b323bec63dfdf18836b..37a44f981ef59fbe383a4b4be7cb9cf639b64b61 100644 --- a/Go/.env.example +++ b/Go/.env.example @@ -1,2 +1,3 @@ PORT=8080 -TOKEN=your_api_token #For Testing +TOKEN=your_api_token +FIREBASE_CREDENTIALS_FILE=path_to_Firebase_credentials_file \ No newline at end of file diff --git a/Go/Dockerfile b/Go/Dockerfile index 93d3706b30186f579ecb3a4a8f7446eeab5fbd81..c7b24170061ef8041a6cf83f1dc1dbd214c8711d 100644 --- a/Go/Dockerfile +++ b/Go/Dockerfile @@ -1,14 +1,14 @@ # syntax=docker/dockerfile:1.2 FROM golang:1.22 AS builder +LABEL authors="Torgrim Thorsen" + # Set destination for COPY WORKDIR /app # Download Go modules COPY go.mod go.sum ./ -RUN go mod download - # Copy the source code. COPY ../ ./ diff --git a/Go/Dockerfile-test b/Go/Dockerfile-test index 9160f128505082efdddaa3626f5da93825d02ae2..2d0ca1280822e38ef9b3223905abb7d7dcc49d2c 100644 --- a/Go/Dockerfile-test +++ b/Go/Dockerfile-test @@ -1,14 +1,14 @@ # syntax=docker/dockerfile:1.2 FROM golang:1.22 AS builder +LABEL authors="Torgrim Thorsen" + # Set destination for COPY WORKDIR /app # Download Go modules COPY go.mod go.sum ./ -RUN go mod download - # Copy the source code. COPY ../ ./ @@ -26,12 +26,5 @@ WORKDIR /root/ # Copy the pre-built binary file from the previous stage. COPY --from=builder /app/main . -# Optional: -# To bind to a TCP port, runtime parameters must be supplied to the docker command. -# But we can document in the Dockerfile what ports -# the application is going to listen on by default. -# https://docs.docker.com/reference/dockerfile/#expose -EXPOSE 8080 - # Run CMD ["./main"] \ No newline at end of file diff --git a/Go/auth/auth.go b/Go/auth/auth.go index 654f74d3deae6132d48cd1c66dbfcc8b8c61062c..f487b6c22050ea8d035e774f29832b2734dedeec 100644 --- a/Go/auth/auth.go +++ b/Go/auth/auth.go @@ -4,9 +4,9 @@ import ( "context" firebase "firebase.google.com/go" "firebase.google.com/go/auth" - "globeboard/internal/utils/constants" "google.golang.org/api/option" "log" + "os" ) var ( @@ -16,10 +16,10 @@ var ( func GetFireBaseAuthClient() (*auth.Client, error) { // Using the credential file - sa := option.WithCredentialsFile(constants.FirebaseCredentialPath) + sa := option.WithCredentialsFile(os.Getenv("FIREBASE_CREDENTIALS_FILE")) app, err := firebase.NewApp(ctx, nil, sa) if err != nil { - log.Println("Credentials not found: " + constants.FirebaseCredentialPath) + log.Println("Credentials not found: " + os.Getenv("FIREBASE_CREDENTIALS_FILE")) log.Println("Error on getting the application") return nil, err } @@ -28,7 +28,7 @@ func GetFireBaseAuthClient() (*auth.Client, error) { client, err := app.Auth(ctx) if err != nil { // Logging the error - log.Println("Credentials file: '" + constants.FirebaseCredentialPath + "' lead to an error.") + log.Println("Credentials file: '" + os.Getenv("FIREBASE_CREDENTIALS_FILE") + "' lead to an error.") return nil, err } diff --git a/Go/cmd/globeboard/app.go b/Go/cmd/globeboard/app.go index b929882eb03f63035a616b93dde988d082f5b6de..2bedc4213bf454cc33fa265ae9412bbf72ce630b 100644 --- a/Go/cmd/globeboard/app.go +++ b/Go/cmd/globeboard/app.go @@ -4,7 +4,6 @@ import ( "globeboard/internal/handlers" "globeboard/internal/handlers/endpoint/dashboard" "globeboard/internal/handlers/endpoint/util" - "globeboard/internal/utils/constants" "globeboard/internal/utils/constants/Endpoints" "globeboard/internal/utils/constants/Paths" "log" @@ -23,7 +22,7 @@ func fileExists(filename string) bool { } func main() { - if !fileExists(constants.FirebaseCredentialPath) { + if !fileExists(os.Getenv("FIREBASE_CREDENTIALS_FILE")) { log.Fatal("Firebase Credentials file is not mounted") return } diff --git a/Go/db/db.go b/Go/db/db.go index 917c3753573e1e5331472b5f2007a38f855854fb..4fb7f68c8d46a62101d914f24043f2104f400519 100644 --- a/Go/db/db.go +++ b/Go/db/db.go @@ -7,7 +7,6 @@ import ( firebase "firebase.google.com/go" "fmt" authenticate "globeboard/auth" - "globeboard/internal/utils/constants" "globeboard/internal/utils/constants/Firestore" "globeboard/internal/utils/structs" "google.golang.org/api/iterator" @@ -16,6 +15,7 @@ import ( "google.golang.org/grpc/status" "log" "net/http" + "os" ) const ( @@ -32,10 +32,10 @@ var ( func getFirestoreClient() (*firestore.Client, error) { // Using the credential file - sa := option.WithCredentialsFile(constants.FirebaseCredentialPath) + sa := option.WithCredentialsFile(os.Getenv("FIREBASE_CREDENTIALS_FILE")) app, err := firebase.NewApp(ctx, nil, sa) if err != nil { - log.Println("Credentials not found: " + constants.FirebaseCredentialPath) + log.Println("Credentials not found: " + os.Getenv("FIREBASE_CREDENTIALS_FILE")) log.Println("Error on getting the application") return nil, err } @@ -44,7 +44,7 @@ func getFirestoreClient() (*firestore.Client, error) { client, err := app.Firestore(ctx) if err != nil { // Logging the error - log.Println("Credentials file: '" + constants.FirebaseCredentialPath + "' lead to an error.") + log.Println("Credentials file: '" + os.Getenv("FIREBASE_CREDENTIALS_FILE") + "' lead to an error.") return nil, err } diff --git a/Go/docker-compose.yml b/Go/docker-compose.yml index ae4e68f5ba23cdc8e624f08908f29eb079e57739..9b5d34181a0a98752701f81724f3fb32f3c43bf6 100644 --- a/Go/docker-compose.yml +++ b/Go/docker-compose.yml @@ -2,28 +2,38 @@ version: "3.8" services: globeboard-test: - image: go-globeboard-test:latest + image: go-globeboard-test build: context: . dockerfile: Dockerfile-test + restart: no environment: - - TOKEN=${TOKEN} + TOKEN: ${TOKEN} + FIREBASE_CREDENTIALS_FILE: /run/secrets/firebase_credentials + secrets: + - firebase_credentials volumes: - - ./.secrets:/root/.secrets:ro - ./web:/root/web:ro + globeboard: - image: go-globeboard:latest + image: go-globeboard build: context: . dockerfile: Dockerfile ports: - '${PORT}:${PORT}' + restart: unless-stopped environment: - - PORT=${PORT} - - TOKEN=${TOKEN} + PORT: ${PORT} + FIREBASE_CREDENTIALS_FILE: /run/secrets/firebase_credentials + secrets: + - firebase_credentials volumes: - - ./.secrets:/root/.secrets:ro - ./web:/root/web:ro depends_on: globeboard-test: - condition: service_completed_successfully \ No newline at end of file + condition: service_completed_successfully + +secrets: + firebase_credentials: + file: ${FIREBASE_CREDENTIALS_FILE} \ No newline at end of file diff --git a/Go/internal/utils/constants/constants.go b/Go/internal/utils/constants/constants.go index 91f5a8164f04a61163b27a0d3ee8533d326d1b3b..a79df750b3199b08224e09dfd9a233163dd480fe 100644 --- a/Go/internal/utils/constants/constants.go +++ b/Go/internal/utils/constants/constants.go @@ -3,9 +3,8 @@ package constants // APIVersion represents the version of the API. const ( - APIVersion = "v1" - FirebaseCredentialPath = "./.secrets/firebaseCredentials.json" - ApiKeyLength = 20 - DocIdLength = 24 - IdLength = 16 + APIVersion = "v1" + ApiKeyLength = 20 + DocIdLength = 24 + IdLength = 16 ) diff --git a/gitlab-ci.yml b/gitlab-ci.yml new file mode 100644 index 0000000000000000000000000000000000000000..9c6340c9e31296e6569b6cf8034a4a35327a471f --- /dev/null +++ b/gitlab-ci.yml @@ -0,0 +1,17 @@ +stages: + - test + +test_globeboard: + stage: test + image: docker:19.03.12 + services: + - docker:19.03.12-dind + variables: + DOCKER_HOST: tcp://docker:2375 + DOCKER_DRIVER: overlay2 + DOCKER_TLS_CERTDIR: "" + script: + - apk add --no-cache docker-compose + - docker-compose -f Go/docker-compose.yml up --build globeboard-test + only: + - main