-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement background service with Go (#59)
- Loading branch information
1 parent
b60e0c4
commit 00fc1c8
Showing
32 changed files
with
1,746 additions
and
21 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
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,23 @@ | ||
FROM golang:1.23.2-alpine AS builder | ||
|
||
WORKDIR /app | ||
|
||
COPY ./viot-background . | ||
|
||
RUN go mod download | ||
|
||
RUN go build -o main ./cmd/main.go | ||
|
||
|
||
FROM alpine:3.20 | ||
|
||
RUN addgroup -S viot && adduser -S viot -G viot | ||
|
||
WORKDIR /app | ||
|
||
COPY --from=builder /app/main /app/main | ||
|
||
RUN chown -R viot:viot /app | ||
USER viot | ||
|
||
CMD ["/app/main"] |
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,14 @@ | ||
ENVIRONMENT=development | ||
|
||
POSTGRES_DSN=postgres://postgres:postgres@localhost:5432/postgres | ||
|
||
MQTT_BROKER_URL=tcp://localhost:1883 | ||
MQTT_CLIENT_ID=b348a2b5-5e1f-47bb-95d0-452002a78611 | ||
MQTT_USERNAME=ABCD123 | ||
MQTT_PASSWORD=ABCD123 | ||
|
||
DEVICE_DATA_PROCESSOR_MAX_BATCH_SIZE=1000 | ||
DEVICE_DATA_PROCESSOR_MAX_BATCH_INTERVAL_MS=2000 | ||
|
||
DEVICE_ATTRIBUTE_PROCESSOR_MAX_BATCH_SIZE=1000 | ||
DEVICE_ATTRIBUTE_PROCESSOR_MAX_BATCH_INTERVAL_MS=2000 |
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,20 @@ | ||
# Compiled Object files, Static and Dynamic libs (Shared Objects) | ||
*.o | ||
*.a | ||
*.so | ||
|
||
# Folders | ||
_obj | ||
_test | ||
vendor | ||
|
||
*.exe | ||
*.test | ||
*.prof | ||
*.pprof | ||
*.out | ||
*.log | ||
|
||
/bin | ||
coverage.out | ||
coverage.html |
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,13 @@ | ||
sqlc: | ||
sqlc generate | ||
|
||
test: | ||
go test -v -cover -short ./... | ||
|
||
server: | ||
go run cmd/main.go | ||
|
||
mock: | ||
mockery --name=Store --dir=./db/repository --output=./db/mock --outpkg=mockdb --case=underscore | ||
|
||
.PHONY: sqlc test server mock |
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,97 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
mqtt "github.com/eclipse/paho.mqtt.golang" | ||
"github.com/jackc/pgx/v5/pgxpool" | ||
"go.uber.org/zap" | ||
|
||
"github.com/vuxmai/viot/viot-background/db/repository" | ||
"github.com/vuxmai/viot/viot-background/internal/processor" | ||
"github.com/vuxmai/viot/viot-background/pkg/config" | ||
"github.com/vuxmai/viot/viot-background/pkg/logger" | ||
) | ||
|
||
const ( | ||
DeviceDataMqttTopic = "v2/private/device_data" | ||
DeviceAttributeMqttTopic = "v2/private/device_attribute" | ||
) | ||
|
||
func NewMqttClient(cfg *config.MqttConfig, logger *zap.Logger) mqtt.Client { | ||
opts := mqtt.NewClientOptions() | ||
opts.AddBroker(cfg.BrokerUrl) | ||
opts.SetClientID(cfg.ClientId) | ||
opts.SetUsername(cfg.Username) | ||
opts.SetPassword(cfg.Password) | ||
opts.SetKeepAlive(time.Second * 60) | ||
|
||
opts.OnConnect = func(client mqtt.Client) { | ||
logger.Info("Connected to MQTT Broker") | ||
} | ||
opts.OnConnectionLost = func(client mqtt.Client, err error) { | ||
logger.Error("Connection lost: %v", zap.Error(err)) | ||
} | ||
|
||
client := mqtt.NewClient(opts) | ||
if token := client.Connect(); token.Wait() && token.Error() != nil { | ||
logger.Fatal("Failed to connect to MQTT Broker", zap.Error(token.Error())) | ||
} | ||
|
||
return client | ||
} | ||
|
||
func main() { | ||
// Load the configuration | ||
cfg := config.LoadConfig() | ||
|
||
logger := logger.NewZapLogger(cfg.Environment) | ||
|
||
// Context | ||
ctx := context.Background() | ||
|
||
// Database | ||
db, err := pgxpool.New(ctx, cfg.PostgresDsn) | ||
if err != nil { | ||
logger.Fatal(err.Error()) | ||
} | ||
|
||
// Repository | ||
store := repository.NewStore(db) | ||
|
||
// Channel | ||
deviceDataCh := make(chan []byte, 10000) | ||
deviceAttributeCh := make(chan []byte, 10000) | ||
|
||
// Worker | ||
deviceDataProcessor := processor.NewDeviceDataProcessor(deviceDataCh, logger) | ||
deviceAttributeProcessor := processor.NewDeviceAttributeProcessor(deviceAttributeCh, logger) | ||
|
||
deviceDataProcessor.Start(ctx, &cfg.DeviceDataProcessorConfig, store) | ||
deviceAttributeProcessor.Start(ctx, &cfg.DeviceAttributeProcessorConfig, store) | ||
|
||
// MQTT Client | ||
client := NewMqttClient(&cfg.MqttConfig, logger) | ||
|
||
// Subscribe to topic | ||
token := client.Subscribe(DeviceDataMqttTopic, 2, func(_ mqtt.Client, msg mqtt.Message) { | ||
deviceDataCh <- msg.Payload() | ||
}) | ||
if token.Wait() && token.Error() != nil { | ||
logger.Info("Subscribe error", | ||
zap.Error(token.Error()), | ||
zap.String("topic", DeviceDataMqttTopic)) | ||
} | ||
|
||
token = client.Subscribe(DeviceAttributeMqttTopic, 2, func(_ mqtt.Client, msg mqtt.Message) { | ||
deviceAttributeCh <- msg.Payload() | ||
}) | ||
if token.Wait() && token.Error() != nil { | ||
logger.Info("Subscribe error", | ||
zap.Error(token.Error()), | ||
zap.String("topic", DeviceAttributeMqttTopic)) | ||
} | ||
|
||
select {} | ||
} |
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,20 @@ | ||
coverage: | ||
status: | ||
project: | ||
default: | ||
target: auto | ||
threshold: 5% | ||
if_not_found: success | ||
patch: | ||
default: | ||
target: auto | ||
threshold: 5% | ||
if_not_found: success | ||
|
||
ignore: | ||
- "**/mocks/" | ||
- "cmd/" | ||
- "docs/" | ||
- "pkg/" | ||
- "vendor/" | ||
- "proto/" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.