Skip to content

Commit

Permalink
initial microservices strucutures
Browse files Browse the repository at this point in the history
  • Loading branch information
msfidelis committed Jan 6, 2022
0 parents commit eafeb31
Show file tree
Hide file tree
Showing 96 changed files with 7,083 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
tmp/*
tmp
# Dependency directories (remove the comment below to include it)
# vendor/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Matheus Fidelis

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
89 changes: 89 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# nutrition-overengineering
Application to calc healthcare and nutrition metrics, but using the most over engineered as possible

> "Caraca Matheus, tu é idiota???
> "Sim"
# API

## Usage

```bash
curl --location --request POST '0.0.0.0:8080/calculator' \
--header 'Content-Type: application/json' \
--data-raw '{
"age": 26,
"weight": 90.0,
"height": 1.77,
"gender": "M",
"activity_intensity": "very_active"
} ' --silent | jq .
```

```json
{
"status": 200,
"imc": {
"result": 28.72737719046251,
"class": "overweight"
},
"basal": {
"bmr": {
"value": 2011.7,
"unit": "kcal"
},
"necessity": {
"value": 3470.1825000000003,
"unit": "kcal"
}
},
"health_info": {
"age": 26,
"weight": 90,
"height": 1.77,
"gender": "M",
"activity_intensity": "very_active"
},
"recomendations": {
"protein": {
"value": 180,
"unit": "g"
},
"water": {
"value": 3150,
"unit": "ml"
},
"calories": {
"maintain_weight": {
"value": 3470.1825000000003,
"unit": "kcal"
},
"loss_weight": {
"value": 3123.1642500000003,
"unit": "kcal"
},
"gain_weight": {
"value": 5205.27375,
"unit": "kcal"
}
}
}
}
```

# TODO
* Create communication across services
* Refactor communication lib
* Customize log lib for trace id
* Implement Prometheus HTTP Endpoint
* Implement Jaeger Tracing
* Implement CI / CD
* Create Kubernetes Deployment

## Notes

```
brew install grpc protobuf protoc-gen-go-grpc
protoc --proto_path=proto/bmr --go_out=proto/bmr --go_out=plugins=grpc:bmr --go_opt=paths=source_relative proto/bmr/bmr.proto
protoc --go_out=plugins=grpc:bmr bmr.proto
```
17 changes: 17 additions & 0 deletions bmr-grpc-service/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
vendor/

tmp/
12 changes: 12 additions & 0 deletions bmr-grpc-service/Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM golang:1.15 AS builder

# Install Air
RUN go get -u github.com/cosmtrek/air

WORKDIR $GOPATH/src/bmr-grpc-service

COPY . ./

RUN pwd; ls -lha

CMD ["air"]
13 changes: 13 additions & 0 deletions bmr-grpc-service/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module bmr-grpc-service

go 1.17

require google.golang.org/grpc v1.43.0

require (
github.com/gin-gonic/examples v0.0.0-20211212053333-a61976f983ec
github.com/golang/protobuf v1.5.2 // indirect
github.com/rs/zerolog v1.26.1
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d
google.golang.org/protobuf v1.27.1
)
200 changes: 200 additions & 0 deletions bmr-grpc-service/go.sum

Large diffs are not rendered by default.

53 changes: 53 additions & 0 deletions bmr-grpc-service/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"bmr-grpc-service/pkg/logger"
"bmr-grpc-service/proto/bmr/service/bmr"
"net"
"os"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"

"google.golang.org/grpc"
)

func main() {

logInternal := logger.Instance()

zerolog.SetGlobalLevel(zerolog.DebugLevel)

log.Logger = log.Output(
zerolog.ConsoleWriter{
Out: os.Stderr,
NoColor: false,
},
)

lis, err := net.Listen("tcp", ":30000")
if err != nil {
log.Error().
Str("Error", err.Error()).
Msg("Failed to listen")
}

logInternal.Info().
Msg("Listener for bmr-grpc-service is created")

s := bmr.Server{}

grpcServer := grpc.NewServer()

bmr.RegisterBMRServiceServer(grpcServer, &s)

if err := grpcServer.Serve(lis); err != nil {
logInternal.Error().
Str("Error", err.Error()).
Msg("Failed to server gRPC server")
}

logInternal.Info().
Msg("Server bmr-grpc-service is enabled")

}
30 changes: 30 additions & 0 deletions bmr-grpc-service/pkg/bmr/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package bmr

import "strings"

func Calc(gender string, weight float64, height float64, age int64, activity string) (float64, float64) {
var basal float64
var necessity float64
if strings.ToUpper(gender) == "M" {
basal = 66 + (13.75 * weight) + (5.0 * (height * 100)) - (6.8 * float64(age))
} else {
basal = 665 + (9.56 * weight) + (1.8 * (height * 100)) - (4.7 * float64(age))
}

// Calc Necessity

switch strings.ToLower(activity) {
case "sedentary":
necessity = basal * 1.2
case "lightly_active":
necessity = basal * 1.375
case "moderately_active":
necessity = basal * 1.55
case "very_active":
necessity = basal * 1.725
case "extra_active":
necessity = basal * 1.9
}

return basal, necessity
}
13 changes: 13 additions & 0 deletions bmr-grpc-service/pkg/logger/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package logger

import (
"os"

"github.com/rs/zerolog"
)

func Instance() zerolog.Logger {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
logger := zerolog.New(os.Stderr).With().Timestamp().Logger()
return logger
}
22 changes: 22 additions & 0 deletions bmr-grpc-service/proto/bmr.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
syntax = "proto3";

package bmr;

option go_package = "service/bmr";

message Message {
string gender = 1;
double weight = 2;
double height = 3;
int64 age = 4;
string activity = 5;
}

message Response {
double bmr = 1 ;
double necessity = 2;
}

service BMRService {
rpc SayHello(Message) returns (Response) {}
}
38 changes: 38 additions & 0 deletions bmr-grpc-service/proto/bmr/service/bmr/bmr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package bmr

import (
calculator "bmr-grpc-service/pkg/bmr"

"bmr-grpc-service/pkg/logger"

"golang.org/x/net/context"
)

type Server struct {
}

func (s *Server) SayHello(ctx context.Context, in *Message) (*Response, error) {
log := logger.Instance()

log.Info().
Str("Gender", in.Gender).
Float64("Weight", in.Weight).
Float64("Height", in.Height).
Int64("Age", in.Age).
Str("Activiry Rate", in.Activity).
Msg("Calculating BMR and Calories Necessity")

bmrCalc, necessity := calculator.Calc(in.Gender, in.Weight, in.Height, in.Age, in.Activity)

log.Info().
Str("Gender", in.Gender).
Float64("Weight", in.Weight).
Float64("Height", in.Height).
Int64("Age", in.Age).
Str("Activiry Rate", in.Activity).
Float64("BMR", bmrCalc).
Float64("Calorical Necessity", necessity).
Msg("Basal Metabolical Rate Calculated")

return &Response{Bmr: bmrCalc, Necessity: necessity}, nil
}
Loading

0 comments on commit eafeb31

Please sign in to comment.