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

1075: Write Messages Locally When There's No RS #29

Merged
merged 5 commits into from
May 22, 2024
Merged
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 Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ dockerBuild:

dockerRun:
docker run -it reportstream-sftp-ingestion

dockerComposeRun:
docker compose up --build
7 changes: 5 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ services:
rs-sftp:
build: .
environment:
AZURE_BLOB_CONNECTION_STRING: DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://sftp-Azurite:10000/devstoreaccount1;
AZURE_BLOB_CONNECTION_STRING: DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://sftp-Azurite:10000/devstoreaccount1; # pragma: allowlist secret
ENV: local
# Uncomment the line below to call local report stream. Otherwise we'll use a mock response
#REPORT_STREAM_URL_PREFIX: http://localhost:7071
volumes:
# map to Azurite data objects to the build directory
- ./localdata/reportstream:/localdata
ports:
- "9090:9090" # default api endpoint port
platform: linux/amd64
Expand All @@ -31,4 +34,4 @@ services:


networks:
sftp:
sftp:
Empty file added localdata/.gitkeep
Empty file.
23 changes: 12 additions & 11 deletions src/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"github.com/CDCgov/reportstream-sftp-ingestion/azure"
"github.com/CDCgov/reportstream-sftp-ingestion/local"
"github.com/CDCgov/reportstream-sftp-ingestion/report_stream"
"log/slog"
"os"
Expand All @@ -28,22 +29,22 @@ func main() {
}

reportStreamBaseUrl := os.Getenv("REPORT_STREAM_URL_PREFIX")
var messageSender MessageSender

if reportStreamBaseUrl == "" {
// Do something with mock response

slog.Info("Mock message sent to Mock RS.")
messageSender = local.FileSender{}
} else {
apiHandler := report_stream.ApiHandler{BaseUrl: reportStreamBaseUrl}
reportId, err := apiHandler.SendReport(content)

if err != nil {
slog.Error("Failed to send the file to ReportStream", slog.Any("error", err))
os.Exit(1)
}
slog.Info("File sent to ReportStream", slog.String("reportId", reportId))
messageSender = report_stream.Sender{BaseUrl: reportStreamBaseUrl}
}

reportId, err := messageSender.SendMessage(content)
if err != nil {
slog.Error("Failed to send the file to ReportStream", slog.Any("error", err))
os.Exit(1)
}

slog.Info("File sent to ReportStream", slog.String("reportId", reportId))

for {
t := time.Now()
slog.Info(t.Format("2006-01-02T15:04:05Z07:00"))
Expand Down
5 changes: 5 additions & 0 deletions src/cmd/sender.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

type MessageSender interface {
SendMessage(message []byte) (string, error)
}
1 change: 1 addition & 0 deletions src/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22
require (
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.3.2
github.com/stretchr/testify v1.9.0
github.com/google/uuid v1.6.0
)

require (
Expand Down
30 changes: 30 additions & 0 deletions src/local/sender.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package local

import (
"fmt"
"github.com/google/uuid"
"os"
"path/filepath"
)

type FileSender struct {
}

func (receiver FileSender) SendMessage(message []byte) (string, error) {
folder := "localdata"

err := os.MkdirAll(folder, 0755)
if err != nil {
return "", err
}

randomUuid := uuid.NewString()

filePath := filepath.Join(folder, fmt.Sprintf("%s.txt", randomUuid))
err = os.WriteFile(filePath, message, 0644) // permissions = owner read/write, group read, other read
if err != nil {
return "", err
}

return randomUuid, nil
}
8 changes: 3 additions & 5 deletions src/report_stream/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@ type Report struct {
ReportId string `json:"reportId"`
}

type ApiHandler struct {
type Sender struct {
BaseUrl string
}

//func (apiHandler *ApiHandler) Login {}

func (apiHandler *ApiHandler) SendReport(hl7message []byte) (string, error) {
func (apiHandler Sender) SendMessage(message []byte) (string, error) {

client := http.Client{}
req, err := http.NewRequest("POST", apiHandler.BaseUrl+"/api/reports", bytes.NewBuffer(hl7message))
req, err := http.NewRequest("POST", apiHandler.BaseUrl+"/api/reports", bytes.NewBuffer(message))

if err != nil {
return "", err
Expand Down
Loading