-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2049b50
commit a9cbb1f
Showing
15 changed files
with
963 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
FROM golang:1.20-alpine3.17 as builder | ||
|
||
RUN apk update && \ | ||
apk add --no-cache file build-base | ||
|
||
WORKDIR /go/src/app | ||
|
||
COPY . . | ||
RUN mkdir /app | ||
|
||
RUN go mod download | ||
RUN CGO_ENABLED=1 GOPROXY=direct go build -o /app/birdhouse -mod=mod ./cmd/main.go | ||
|
||
# deploy-stage | ||
FROM alpine:latest | ||
|
||
RUN apk update && \ | ||
apk add --no-cache curl bash | ||
|
||
RUN mkdir /key | ||
|
||
WORKDIR /app | ||
|
||
COPY --from=builder /app ./ | ||
|
||
|
||
RUN apk update && \ | ||
apk add --no-cache curl bash | ||
|
||
HEALTHCHECK --interval=30s --start-period=1m --timeout=30s --retries=3 \ | ||
CMD curl --silent --fail --fail-early http://127.0.0.1:$PORT/docs || exit 1 | ||
EXPOSE $PORT | ||
ENTRYPOINT ["/app/birdhouse"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package internal | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"strconv" | ||
) | ||
|
||
// MustString func returns environment variable value as a string value, | ||
// If variable doesn't exist or is not set, exits from the runtime | ||
func MustString(key string) string { | ||
value, exists := os.LookupEnv(key) | ||
if !exists { | ||
log.Fatalf("required ENV %q is not set", key) | ||
} | ||
if value == "" { | ||
log.Fatalf("required ENV %q is empty", key) | ||
} | ||
return value | ||
} | ||
|
||
// GetString func returns environment variable value as a string value, | ||
// If variable doesn't exist or is not set, returns fallback value | ||
func GetString(key string, fallback string) string { | ||
value, exists := os.LookupEnv(key) | ||
if !exists { | ||
return fallback | ||
} | ||
return value | ||
} | ||
|
||
// MustInt func returns environment variable value as an integer value, | ||
// If variable doesn't exist or is not set, exits from the runtime | ||
func MustInt(key string) int { | ||
value, exists := os.LookupEnv(key) | ||
if !exists { | ||
log.Fatalf("required ENV %q is not set", key) | ||
} | ||
if value == "" { | ||
log.Fatalf("required ENV %q is empty", key) | ||
} | ||
res, err := strconv.ParseInt(value, 10, 32) | ||
if err != nil { | ||
log.Fatalf("required ENV %q must be a number but it's %q", key, value) | ||
} | ||
return int(res) | ||
} | ||
|
||
// GetInt func returns environment variable value as a integer value, | ||
// If variable doesn't exist or is not set, returns fallback value | ||
func GetInt(key string, fallback int) int { | ||
value, exists := os.LookupEnv(key) | ||
if !exists { | ||
return fallback | ||
} | ||
res, err := strconv.ParseInt(value, 10, 32) | ||
if err != nil { | ||
return fallback | ||
} | ||
return int(res) | ||
} | ||
|
||
// GetFloat func returns environment variable value as a integer value, | ||
// If variable doesn't exist or is not set, returns fallback value | ||
func GetFloat(key string, fallback int) int { | ||
value, exists := os.LookupEnv(key) | ||
if !exists { | ||
return fallback | ||
} | ||
res, err := strconv.ParseFloat(value, 64) | ||
if err != nil { | ||
return fallback | ||
} | ||
return int(res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package internal | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"log" | ||
) | ||
|
||
// DBConnect inits database connection | ||
func DBConnect() *sql.DB { | ||
// db connection env variables | ||
var ( | ||
dbHost = MustString("DATABASE_HOST") | ||
dbPort = MustInt("DATABASE_PORT") | ||
dbName = MustString("DATABASE_NAME") | ||
dbUsername = MustString("DATABASE_USER") | ||
dbPassword = MustString("DATABASE_PASS") | ||
dbTimeout = GetInt("DATABASE_CONNECT_TIMEOUT", 15) | ||
dbSSLMode = GetString("DATABASE_SSL_MODE", "disable") | ||
) | ||
|
||
dbConnStr := fmt.Sprintf( | ||
"host=%s port=%d user=%s password=%s dbname=%s sslmode=%s connect_timeout=%d", | ||
dbHost, dbPort, dbUsername, dbPassword, dbName, dbSSLMode, dbTimeout, | ||
) | ||
db, err := sql.Open("postgres", dbConnStr) | ||
if err != nil { | ||
log.Fatalf("could not connect to database: %v", err) | ||
} | ||
|
||
db.SetMaxOpenConns(50) | ||
db.SetMaxIdleConns(0) | ||
|
||
if err := db.Ping(); err != nil { | ||
log.Fatalf("database is not available right now: %v", err) | ||
} | ||
|
||
return db | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.