Skip to content

andrimuhayat/sensor-service

Repository files navigation

Sensor Service (Microservice B)

version: 1.0.0

Sensor service or microservice B is service that consume and process the data from microservices A, also in this service has REST API sensor data, authentication & authorization check. please be make sure the Sensor streaming(A) running first.

Code Architecture

This service made to containerize the allcare core service make a clear separation of concern within the architecture with Uncle Bob Clean Architecture interpretation.

Build with

Dependency Rule

Based on Uncle Bob, there are 4 layers:

  • Entity
  • Use Cases
  • Interface Adapters
  • Frameworks and Drivers.

In this repository, we also using 4 layers (with modification) like this:

  • Entity.
  • Use Cases (Implementation).
  • Interface Adapters. Will be splitted into two:
    • Repository Interface. Bridging repository implementation and use cases layer.
    • Use Case Interface. Bridging handler and use cases layer.
  • Driver Layer
    • Handler
    • Repository implementation

Standardization used to build this project

Guidelines

Step-by-step writing code using this pattern

  • Setup skeleton of the microservices (including: main.go, cmd, server, migrations, config, platform. handler)
  • Defining the entity
  • Defining usecase (interface & implementation) in folder module/sensor/useaces. We're gonna focus in this folder since the business logic will be written here.
  • When the usecase need to communicate to the external agency (Database) then write it to the repository interface
  • After the usecase layer was done, now time to write repository implementation
  • Put it up together + register to the module/sensor/service.go and init module dependency in server.go

Environment

Environment App

Please setup the backend environment in this file app.yaml.

app:
  port: 8080
  name: sensor_streaming
  secret_key: "oksecret!1"
mysql:
  host: host.docker.internal
  port: 3306
  driver_name: mysql
  username: cabin
  password: cabinpw
  charset: utf8
  db_name: sensor
  max_open_connection: 5
  max_idle_connection: 2
  max_lifetime_connection: 60s
  db_migrate: false
kafka:
  host: host.docker.internal
  port: 9092
mqtt:
  host: host.docker.internal
  port: 1883

Environment docker

Please setup the docker environment in this file .env.docker.

  MYSQL_ROOT_PASSWORD=rootpw
  MYSQL_DATABASE=sensor
  MYSQL_USER=cabin
  MYSQL_PASSWORD=cabinpw

Migration tools

As a standalone tool

dbconfig.yml config file should specify like this,

development:
dialect: mysql
datasource: dbname=postgres port=5432 user=routesroot sslmode=disable
dir: internal/platform/storage/migration

command migration,

$ sql-migrate --help
usage: sql-migrate [--version] [--help] <command> [<args>]

Available commands are:
down      Undo a database migration
new       Create a new migration
redo      Reapply the last migration
status    Show migration status
up        Migrates the database to the most recent version available

last, set the db_migrate flag in app.yaml of the migration it will be execute when app is starting

db_migrate: true

🏗️ How To Run

open terminal cd to this project's root folder and run script below

swag init
docker-compose --env-file .docker.env  up

Swagger URL

http://0.0.0.0:8080/swagger/index.html

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published