Skip to content

Commit

Permalink
Merge pull request #4 from spectrocloud/interface
Browse files Browse the repository at this point in the history
fix: updated bind interface to use 0.0.0.0 instead of localhost
  • Loading branch information
karl-cardenas-coding authored Jan 9, 2023
2 parents 3ce351e + 3c5897a commit eed61c8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ FROM alpine:latest
LABEL org.opencontainers.image.source="https://github.com/spectrocloud/hello-universe-api"
LABEL org.opencontainers.image.description "A Spectro Cloud demo application intended for learning and showcasing products. This is the API server for Hello Universe."
ENV PORT 3000
ENV HOST localhost
ENV DB_HOST localhost
ENV HOST '0.0.0.0'
ENV DB_HOST '0.0.0.0'
ENV DB_PORT 5432
ENV DB_USER postgres
ENV DB_PASSWORD password
ENV DB_NAME counter
COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /etc/group /etc/group
COPY --from=builder --chown=appuser:appuser /go/bin/app /usr/bin/app
RUN apk -U upgrade --no-cache && apk add --no-cache tzdata bash curl openssl && \
RUN apk -U upgrade --no-cache && apk add --no-cache tzdata bash curl openssl jq bind-tools && \
chmod a+x /usr/bin/app
USER appuser
EXPOSE 3000
Expand Down
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ A Spectro Cloud demo application. This is the API server for the [Hello Universe
# Overview
The [Hello Universe](https://github.com/spectrocloud/hello-universe) app includes an API server that expands the capabilities of the application. The API server requires a Postgres database to store and retrieve data. Use the [Hello Universe DB](https://github.com/spectrocloud/hello-universe-db) container for a simple integration with a Postgres database.

# Endpoints

A Postman collection is available to help you explore the API. Review the [Postman collection](./tests/postman_collection.json) to get started.

# Usage

The quickest method to start the API server locally is by using the Docker image.

```shell
docker pull ghcr.io/spectrocloud/hello-universe-api:1.0.4
docker run -p 3000:3000 ghcr.io/spectrocloud/hello-universe-api:1.0.4
docker pull ghcr.io/spectrocloud/hello-universe-api:1.0.5
docker run -p 3000:3000 ghcr.io/spectrocloud/hello-universe-api:1.0.5
```

To start the API server you must have connectivity to a postgres instance. Use [environment variables](#environment-variables) to customize the API server start parameters.
Expand All @@ -29,10 +33,10 @@ The API server accepts the following environment variables.
| Variable | Description | Default |
|-------------|----------------------------------------------------|-----------|
| `PORT` | The port number the application will listen on. | `3000` |
| `HOST` | The host value name the API server will listen on. | `localhost` |
| `HOST` | The host value name the API server will listen on. | `0.0.0.0` |
| `DB_NAME` | The database name. | `counter` |
| `DB_USER` | The database user name to use for queries. | `postgres` |
| `DB_HOST` | The hostname or url to the database. | `localhost` |
| `DB_HOST` | The hostname or url to the database. | `0.0.0.0` |
| `DB_PASSWORD` | The database password. | `password` |
| `DB_ENCRYPTION`| The Postgres [ssl mode](https://www.postgresql.org/docs/current/libpq-ssl.html) behavior to enable. Allowed values are: `require`, `verify-full`, `verify-ca`, or `disable` |`disable`|
| `DB_INIT` | Set to `true` if you want the API server to create the required database schema and tables in the target database.| `false` |
Expand Down
8 changes: 5 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ func init() {
globalTraceLevel = internal.Getenv("TRACE", "INFO")
initDB := strings.ToLower(internal.Getenv("DB_INIT", "false"))
port := internal.Getenv("PORT", "3000")
host := internal.Getenv("HOST", "localhost")
host := internal.Getenv("HOST", "0.0.0.0")
globalHost = host
globalPort = port
globalHostURL = host + ":" + port

internal.InitLogger(globalTraceLevel)
dbName = internal.Getenv("DB_NAME", "counter")
dbUser = internal.Getenv("DB_USER", "postgres")
dbHost = internal.Getenv("DB_HOST", "localhost")
dbHost = internal.Getenv("DB_HOST", "0.0.0.0")
dbEncryption := internal.Getenv("DB_ENCRYPTION", "disable")
dbPassword = internal.Getenv("DB_PASSWORD", "password")
dbPort = internal.StringToInt64(internal.Getenv("DB_PORT", "5432"))
Expand Down Expand Up @@ -91,7 +92,8 @@ func main() {
http.HandleFunc(internal.ApiPrefix+"counter", counterRoute.CounterHTTPHandler)
http.HandleFunc(internal.ApiPrefix+"health", healthRoute.HealthHTTPHandler)

log.Info().Msgf("Server is configured for port %s", globalPort)
log.Info().Msgf("Server is configured for port %s and listing on %s", globalPort, globalHostURL)
log.Info().Msgf("Database is configured for %s:%d", dbHost, dbPort)
log.Info().Msgf("Trace level set to: %s", globalTraceLevel)
log.Info().Msg("Starting client Application")
err := http.ListenAndServe(globalHostURL, nil)
Expand Down

0 comments on commit eed61c8

Please sign in to comment.