Skip to content

Commit

Permalink
Merge pull request #30 from evrone/develop
Browse files Browse the repository at this point in the history
Readme update
  • Loading branch information
golang-school authored Apr 13, 2021
2 parents 46ae5d6 + a767800 commit 0ee1386
Show file tree
Hide file tree
Showing 13 changed files with 271 additions and 144 deletions.
305 changes: 215 additions & 90 deletions README.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions integration-test/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ COPY go.mod go.sum /modules/
WORKDIR /modules
RUN go mod download

# Step 2: Builder
FROM golang:1.16.2-alpine3.13 as builder
# Step 2: Tests
FROM golang:1.16.2-alpine3.13
COPY --from=modules /go/pkg /go/pkg
COPY . /app
WORKDIR /app
Expand Down
2 changes: 1 addition & 1 deletion internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func Run(cfg *config.Config) {
logger.Fatal(err, "app - Run - rmqServer - server.NewServer")
}

// HTTP
// HTTP Server
handler := gin.New()
v1.NewRouter(handler, translationService)
httpServer := httpserver.NewServer(handler, httpserver.Port(cfg.HTTP.Port))
Expand Down
15 changes: 4 additions & 11 deletions internal/delivery/amqp_rpc/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,11 @@ import (
"github.com/evrone/go-service-template/pkg/rabbitmq/rmq_rpc/server"
)

type router struct {
translationService service.Translation
routerMap map[string]server.CallHandler
}

func NewRouter(translationService service.Translation) map[string]server.CallHandler {
r := &router{
translationService: translationService,
routerMap: make(map[string]server.CallHandler),
routes := make(map[string]server.CallHandler)
{
newTranslationRoutes(routes, translationService)
}

r.translationRoutes()

return r.routerMap
return routes
}
16 changes: 12 additions & 4 deletions internal/delivery/amqp_rpc/translation.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,30 @@ import (
"github.com/streadway/amqp"

"github.com/evrone/go-service-template/internal/domain"
"github.com/evrone/go-service-template/internal/service"
"github.com/evrone/go-service-template/pkg/rabbitmq/rmq_rpc/server"
)

func (r *router) translationRoutes() {
r.routerMap["getHistory"] = r.getHistory()
type translationRoutes struct {
translationService service.Translation
}

func newTranslationRoutes(routes map[string]server.CallHandler, ts service.Translation) {
r := &translationRoutes{ts}
{
routes["getHistory"] = r.getHistory()
}
}

type historyResponse struct {
History []domain.Translation `json:"history"`
}

func (r *router) getHistory() server.CallHandler {
func (r *translationRoutes) getHistory() server.CallHandler {
return func(d *amqp.Delivery) (interface{}, error) {
translations, err := r.translationService.History()
if err != nil {
return nil, errors.Wrap(err, "amqp_rpc - router - getHistory - r.translationService.History")
return nil, errors.Wrap(err, "amqp_rpc - translationRoutes - getHistory - r.translationService.History")
}

response := historyResponse{translations}
Expand Down
12 changes: 4 additions & 8 deletions internal/delivery/http/v1/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,8 @@ import (
// @host localhost:8080
// @BasePath /api/v1

type router struct {
translationService service.Translation
}

func NewRouter(handler *gin.Engine, translationService service.Translation) {
r := &router{translationService}

// Options
handler.Use(gin.Logger())
handler.Use(gin.Recovery())

Expand All @@ -37,8 +32,9 @@ func NewRouter(handler *gin.Engine, translationService service.Translation) {
// K8s probe
handler.GET("/healthz", func(c *gin.Context) { c.Status(http.StatusOK) })

api := handler.Group("/api/v1")
// Routers
h := handler.Group("/api/v1")
{
r.translationRouts(api)
newTranslationRoutes(h, translationService)
}
}
19 changes: 13 additions & 6 deletions internal/delivery/http/v1/translation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ import (
"github.com/gin-gonic/gin"

"github.com/evrone/go-service-template/internal/domain"
"github.com/evrone/go-service-template/internal/service"
)

func (r *router) translationRouts(api *gin.RouterGroup) {
translation := api.Group("/translation")
type translationRoutes struct {
translationService service.Translation
}

func newTranslationRoutes(handler *gin.RouterGroup, ts service.Translation) {
r := &translationRoutes{ts}

h := handler.Group("/translation")
{
translation.GET("/history", r.history)
translation.POST("/do-translate", r.doTranslate)
h.GET("/history", r.history)
h.POST("/do-translate", r.doTranslate)
}
}

Expand All @@ -29,7 +36,7 @@ type historyResponse struct {
// @Success 200 {object} historyResponse
// @Failure 400 {object} response
// @Router /translation/history [get].
func (r *router) history(c *gin.Context) {
func (r *translationRoutes) history(c *gin.Context) {
translations, err := r.translationService.History()
if err != nil {
errorResponse(c, http.StatusBadRequest, err, "database problems")
Expand All @@ -56,7 +63,7 @@ type doTranslateRequest struct {
// @Success 200 {object} domain.Translation
// @Failure 400 {object} response
// @Router /translation/do-translate [post].
func (r *router) doTranslate(c *gin.Context) {
func (r *translationRoutes) doTranslate(c *gin.Context) {
var request doTranslateRequest
if err := c.ShouldBindJSON(&request); err != nil {
errorResponse(c, http.StatusBadRequest, err, "invalid request body")
Expand Down
13 changes: 7 additions & 6 deletions internal/service/repo/translation_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package repo

import (
"context"
"fmt"

"github.com/pkg/errors"

"github.com/evrone/go-service-template/internal/domain"
"github.com/evrone/go-service-template/pkg/postgres"
Expand All @@ -22,12 +23,12 @@ func (r *TranslationRepo) GetHistory(ctx context.Context) ([]domain.Translation,
From("history").
ToSql()
if err != nil {
return nil, fmt.Errorf("TranslationRepo - GetHistory - r.Builder: %w", err)
return nil, errors.Wrap(err, "TranslationRepo - GetHistory - r.Builder")
}

rows, err := r.Pool.Query(ctx, sql)
if err != nil {
return nil, fmt.Errorf("TranslationRepo - GetHistory - r.Pool.Query: %w", err)
return nil, errors.Wrap(err, "TranslationRepo - GetHistory - r.Pool.Query")
}
defer rows.Close()

Expand All @@ -38,7 +39,7 @@ func (r *TranslationRepo) GetHistory(ctx context.Context) ([]domain.Translation,

err = rows.Scan(&e.Source, &e.Destination, &e.Original, &e.Translation)
if err != nil {
return nil, fmt.Errorf("TranslationRepo - GetHistory - rows.Scan: %w", err)
return nil, errors.Wrap(err, "TranslationRepo - GetHistory - rows.Scan")
}

entities = append(entities, e)
Expand All @@ -54,12 +55,12 @@ func (r *TranslationRepo) Store(ctx context.Context, entity domain.Translation)
Values(entity.Source, entity.Destination, entity.Original, entity.Translation).
ToSql()
if err != nil {
return fmt.Errorf("TranslationRepo - Store - r.Builder: %w", err)
return errors.Wrap(err, "TranslationRepo - Store - r.Builder")
}

_, err = r.Pool.Exec(ctx, sql, args...)
if err != nil {
return fmt.Errorf("TranslationRepo - Store - r.Pool.Exec: %w", err)
return errors.Wrap(err, "TranslationRepo - Store - r.Pool.Exec")
}

return nil
Expand Down
9 changes: 5 additions & 4 deletions internal/service/translation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package service

import (
"context"
"fmt"

"github.com/pkg/errors"

"github.com/evrone/go-service-template/internal/domain"
)
Expand All @@ -22,7 +23,7 @@ func NewTranslationService(r TranslationRepo, w TranslationWebAPI) *TranslationS
func (s *TranslationService) History() ([]domain.Translation, error) {
translations, err := s.repo.GetHistory(context.Background())
if err != nil {
return nil, fmt.Errorf("TranslationService - History - s.repo.GetHistory: %w", err)
return nil, errors.Wrap(err, "TranslationService - History - s.repo.GetHistory")
}

return translations, nil
Expand All @@ -31,12 +32,12 @@ func (s *TranslationService) History() ([]domain.Translation, error) {
func (s *TranslationService) Translate(translation domain.Translation) (domain.Translation, error) {
translation, err := s.webAPI.Translate(translation)
if err != nil {
return domain.Translation{}, fmt.Errorf("TranslationService - Translate - s.webAPI.Translate: %w", err)
return domain.Translation{}, errors.Wrap(err, "TranslationService - Translate - s.webAPI.Translate")
}

err = s.repo.Store(context.Background(), translation)
if err != nil {
return domain.Translation{}, fmt.Errorf("TranslationService - Translate - s.repo.Store: %w", err)
return domain.Translation{}, errors.Wrap(err, "TranslationService - Translate - s.repo.Store")
}

return translation, nil
Expand Down
5 changes: 2 additions & 3 deletions internal/service/webapi/translation_google.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package webapi

import (
"fmt"

translator "github.com/Conight/go-googletrans"
"github.com/pkg/errors"

"github.com/evrone/go-service-template/internal/domain"
)
Expand All @@ -26,7 +25,7 @@ func (t *TranslationWebAPI) Translate(translation domain.Translation) (domain.Tr

result, err := trans.Translate(translation.Original, translation.Source, translation.Destination)
if err != nil {
return domain.Translation{}, fmt.Errorf("TranslationWebAPI - Translate - trans.Translate: %w", err)
return domain.Translation{}, errors.Wrap(err, "TranslationWebAPI - Translate - trans.Translate")
}

translation.Translation = result.Text
Expand Down
9 changes: 4 additions & 5 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package logger

import (
"fmt"

"github.com/pkg/errors"
"go.uber.org/zap"
)

var appLogger Logger //nolint:gochecknoglobals // it's necessary
var appLogger Logger //nolint:gochecknoglobals // it's ok

func NewAppLogger(zapLogger *ZapLogger, appName, appVersion string) {
fields := []Field{
Expand Down Expand Up @@ -57,14 +56,14 @@ func (l *logger) warn(msg string, fields ...Field) {
}

func (l *logger) error(err error, msg string, fields ...Field) {
err = fmt.Errorf("%s: %w", msg, err)
err = errors.Wrap(err, msg)

fields = append(l.defaultFields, fields...)
l.zap.Error(err.Error(), zapFields(fields)...)
}

func (l *logger) fatal(err error, msg string, fields ...Field) {
err = fmt.Errorf("%s: %w", msg, err)
err = errors.Wrap(err, msg)

fields = append(l.defaultFields, fields...)
l.zap.Fatal(err.Error(), zapFields(fields)...) // os.Exit()
Expand Down
3 changes: 1 addition & 2 deletions pkg/rabbitmq/rmq_rpc/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package client

import (
"encoding/json"
"fmt"
"sync"
"time"

Expand Down Expand Up @@ -232,7 +231,7 @@ func (c *Client) Shutdown() error {

err := c.conn.Connection.Close()
if err != nil {
return fmt.Errorf("rmq_rpc client - Client - Shutdown - c.Connection.Close: %w", err)
return errors.Wrap(err, "rmq_rpc client - Client - Shutdown - c.Connection.Close")
}

return nil
Expand Down
3 changes: 1 addition & 2 deletions pkg/rabbitmq/rmq_rpc/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package server

import (
"encoding/json"
"fmt"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -148,7 +147,7 @@ func (s *Server) Shutdown() error {

err := s.conn.Connection.Close()
if err != nil {
return fmt.Errorf("rmq_rpc server - Server - Shutdown - s.Connection.Close: %w", err)
return errors.Wrap(err, "rmq_rpc server - Server - Shutdown - s.Connection.Close")
}

return nil
Expand Down

0 comments on commit 0ee1386

Please sign in to comment.