Skip to content

Commit

Permalink
Refactor code to handle case when API key is not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusziade committed Aug 31, 2023
1 parent c003a33 commit 13a93e0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
33 changes: 14 additions & 19 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
# Use the official Go image
FROM golang:1.19 AS build
# Use the official Golang image to create a build artifact.
FROM golang:1.19 as builder

# Set the working directory
WORKDIR /app

# Copy go mod and sum files
COPY go.mod go.sum ./
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum

# Download dependencies
RUN go mod download

# Copy source code
# Copy the source code
COPY . .

# Build the application
RUN go build -o main .
# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o /app/main .

# Use a lightweight image for deployment
FROM alpine:latest AS runtime
# Use a minimal image for deployment
FROM alpine:latest

# Install necessary libraries
RUN apk --no-cache add ca-certificates
# Copy the binary
COPY --from=builder /app/main /app/main

# Copy binary
COPY --from=build /app/main /app/

# Set the working directory
WORKDIR /app

# Run the application
CMD [". --download-only"]
# This is the entry point, specify your flags here
ENTRYPOINT [ "/app/main", "--download-only" ]
7 changes: 6 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ type Config struct {
}

func getOrCreateAPIKey() string {
apiKey := os.Getenv("NASA_API_KEY")
if apiKey != "" {
return apiKey
}

filePathName := "Keys.json"
config, err := readConfig(filePathName)
if err != nil {
Expand Down Expand Up @@ -42,7 +47,7 @@ func getOrCreateAPIKey() string {
log.Fatalf("Error reading Keys.json: %v", err)
}

apiKey := config.APIKey
apiKey = config.APIKey
return apiKey
}

Expand Down
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ type NasaAPOD struct {
const apiURL = "https://api.nasa.gov/planetary/apod"

func main() {
apiKey := getOrCreateAPIKey()
apiKey := os.Getenv("NASA_API_KEY")
if apiKey == "" {
apiKey = getOrCreateAPIKey()
}

start, end, downloadOnly := parseArgumentsForDateRange()

Expand Down

0 comments on commit 13a93e0

Please sign in to comment.