Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance service configuration #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BUCKET=sample_bucket
PORT=55555
DB_PATH=sample_db.db
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM golang:1.21 AS builder

WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN go build -o main .

FROM alpine:latest

WORKDIR /app
COPY --from=builder /app/main .
EXPOSE 8080
CMD ["./main"]
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,20 @@ The RESTful API is described in an OpenAPI 3.0 document found here: ([openapi.js
Running
=======

```bash
usage: ./rest-api-microservice-demo db_path [port] [db_bucket_name]
**Native**
```sh
go run main.go
```

**Docker**
```sh
docker build -t rest-api-microservice-demo .
docker run -p ${PORT}:${PORT} rest-api-microservice-demo
```

**Docker Compose**
```sh
docker-compose up --build
```

Testing
Expand Down
9 changes: 9 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
go-service:
build: .
ports:
- "${PORT}:${PORT}"
environment:
- BUCKET=${BUCKET}
- PORT=${PORT}
- DB_PATH=${DB_PATH}
68 changes: 24 additions & 44 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,60 +9,40 @@ import (
"github.com/brandonto/rest-api-microservice-demo/db"
)

const defaultDbBucketName = "DetailedMessageBucket"
const defaultPort = uint64(55555)

func main() {
// Outputs usage if number of arguments are off
//
if len(os.Args) > 4 || len(os.Args) < 2 {
fmt.Println("usage: " + os.Args[0] + " db_path [port] [db_bucket_name]")
return
}

// First argument is the filepath of the database
//
dbFile := os.Args[1]

// Second (optional) argument is the the port number
//
port := defaultPort
if len(os.Args) > 2 {
// Some sanity check for the port argument before using
//
var err error
port, err = strconv.ParseUint(os.Args[2], 10, 64)
if err != nil {
fmt.Println("port argument must be an unsigned integer")
return
}
const (
envDbBucketName = "BUCKET"
envPortName = "PORT"
dbPathName = "DB_PATH"
)

if port < uint64(49152) || port > uint64(65535) {
fmt.Println("port argument must be a value between 49152–65535")
return
}
func getConfig() core.Config {
envPort, cP := os.LookupEnv(envPortName)
bucket, cB := os.LookupEnv(envDbBucketName)
dbFile, cD := os.LookupEnv(dbPathName)
if !cP || !cB || !cD {
fmt.Errorf("enviroment variables: [%s, %s, %s] are required...", envDbBucketName, envPortName, dbPathName)
}

// Third (optional) argument is the the bucket name
//
dbBucketName := defaultDbBucketName
if len(os.Args) > 3 {
dbBucketName = os.Args[3]
port, err := strconv.ParseUint(envPort, 10, 64)
if err != nil {
fmt.Errorf("PORT environment variable must be an unsigned integer, value was: %s", envPort)
}

// Configure and run the application
//
dbCfg := db.Config{
FilePath: dbFile,
BucketName: dbBucketName,
if port < uint64(49152) || port > uint64(65535) {
fmt.Errorf("PORT environment variable must be a value between 49152–65535, value was: %s", envPort)
}

coreCfg := core.Config{
DbCfg: dbCfg,
DbCfg: db.Config{
FilePath: dbFile,
BucketName: bucket,
},
Port: port,
EnableLogger: true,
Standalone: true,
}
return coreCfg
}

core.Run(coreCfg)
func main() {
core.Run(getConfig())
}
106 changes: 106 additions & 0 deletions swagger.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
openapi: 3.0.3
info:
title: REST API Microservice Demo
description: A simple REST API microservice.
version: 1.0.0
servers:
- url: http://localhost:8080
description: Local server

paths:
/messages:
get:
summary: Get all messages
operationId: getMessages
responses:
'200':
description: A list of messages
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Message'
post:
summary: Create a new message
operationId: createMessage
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Message'
responses:
'201':
description: Message created
content:
application/json:
schema:
$ref: '#/components/schemas/DetailedMessage'

/messages/{messageId}:
get:
summary: Get a message by ID
operationId: getMessageById
parameters:
- name: messageId
in: path
required: true
schema:
type: integer
format: int64
responses:
'200':
description: A message with metadata
content:
application/json:
schema:
$ref: '#/components/schemas/DetailedMessage'
'404':
description: Message not found
delete:
summary: Delete a message by ID
operationId: deleteMessage
parameters:
- name: messageId
in: path
required: true
schema:
type: integer
format: int64
responses:
'204':
description: Message deleted
'404':
description: Message not found

components:
schemas:
Message:
type: object
properties:
id:
type: integer
format: int64
payload:
type: string
required:
- id
- payload

MessageMetadata:
type: object
properties:
palindrome:
type: boolean

DetailedMessage:
type: object
properties:
message:
$ref: '#/components/schemas/Message'
metadata:
$ref: '#/components/schemas/MessageMetadata'
required:
- message
- metadata