Skip to content

Commit

Permalink
Add some api for use s3 in project (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
ripls56 authored Oct 11, 2024
2 parents 1123d7c + dc0a010 commit 3c5c299
Show file tree
Hide file tree
Showing 22 changed files with 360 additions and 151 deletions.
8 changes: 7 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ SMTP_HOST=smtp.yandex.ru
SMTP_PORT=587
SMTP_URL=${SMTP_HOST}:${SMTP_PORT}
SMTP_USERNAME=YOUR_USERNAME
SMTP_PASSWORD=YOUR_PASSWORD
SMTP_PASSWORD=YOUR_PASSWORD

S3_ACCESS_TOKEN=YOUR_KEY_OR_USERNAME
S3_SECRET_TOKEN=YOUR_KEY_OR_PASSWORD
S3_REGION=eu-north-1
S3_HOST=http://minio.kissota.ru:9001
S3_BUCKET=taskem
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ coverage: test
submodule:
@git submodule update --remote --recursive

server-compose:
docker compose build server && docker compose up server

run-server:
go run apps/server/cmd/server/main.go

gen:
@./scripts/gen_proto.sh

Expand Down
19 changes: 10 additions & 9 deletions apps/notification/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ ARG GO_VERSION=1.23.0
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION} AS build
WORKDIR /src

ARG PROJECT_PATH=apps/notification

LABEL org.opencontainers.image.source="https://github.com/taskemapp/notification"

# Download dependencies as a separate step to take advantage of Docker's caching.
# Leverage a cache mount to /go/pkg/mod/ to speed up subsequent builds.
# Leverage bind mounts to go.sum and go.mod to avoid having to copy them into
# the container.
RUN --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=bind,source=go.sum,target=go.sum \
--mount=type=bind,source=go.mod,target=go.mod \
RUN --mount=type=bind,source=${PROJECT_PATH}/go.sum,target=go.sum \
--mount=type=bind,source=${PROJECT_PATH}/go.mod,target=go.mod \
go mod download -x


# This is the architecture you're building for, which is passed in by the builder.
# Placing it here allows the previous steps to be cached across architectures.
ARG TARGETARCH
Expand All @@ -31,9 +33,8 @@ ARG TARGETARCH
# Leverage a cache mount to /go/pkg/mod/ to speed up subsequent builds.
# Leverage a bind mount to the current directory to avoid having to copy the
# source code into the container.
RUN --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=bind,target=. \
CGO_ENABLED=0 GOARCH=$TARGETARCH go build -o /bin/server ./cmd/server
RUN --mount=type=bind,source=./${PROJECT_PATH}/,target=. \
CGO_ENABLED=0 GOARCH=$TARGETARCH go build -o /bin/notification ./cmd/notification

################################################################################
# Create a new stage for running the application that contains the minimal
Expand Down Expand Up @@ -71,9 +72,9 @@ RUN adduser \
USER appuser

# Copy the executable from the "build" stage.
COPY --from=build /bin/server /bin/
COPY --from=build /bin/notification /bin/
# Expose the port that the application listens on.
EXPOSE 50051
EXPOSE 50052

# What the container should run when it is started.
ENTRYPOINT [ "/bin/server" ]
ENTRYPOINT [ "/bin/notification" ]
File renamed without changes.
16 changes: 9 additions & 7 deletions apps/server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ ARG GO_VERSION=1.23.0
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION} AS build
WORKDIR /src

ARG PROJECT_PATH=apps/server

LABEL org.opencontainers.image.source="https://github.com/taskemapp/server"

# Download dependencies as a separate step to take advantage of Docker's caching.
# Leverage a cache mount to /go/pkg/mod/ to speed up subsequent builds.
# Leverage bind mounts to go.sum and go.mod to avoid having to copy them into
# the container.
RUN --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=bind,source=go.sum,target=go.sum \
--mount=type=bind,source=go.mod,target=go.mod \
RUN --mount=type=bind,source=${PROJECT_PATH}/go.sum,target=go.sum \
--mount=type=bind,source=${PROJECT_PATH}/go.mod,target=go.mod \
go mod download -x


# This is the architecture you're building for, which is passed in by the builder.
# Placing it here allows the previous steps to be cached across architectures.
ARG TARGETARCH
Expand All @@ -31,8 +33,7 @@ ARG TARGETARCH
# Leverage a cache mount to /go/pkg/mod/ to speed up subsequent builds.
# Leverage a bind mount to the current directory to avoid having to copy the
# source code into the container.
RUN --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=bind,target=. \
RUN --mount=type=bind,source=./${PROJECT_PATH}/,target=. \
CGO_ENABLED=0 GOARCH=$TARGETARCH go build -o /bin/server ./cmd/server

################################################################################
Expand Down Expand Up @@ -73,8 +74,9 @@ USER appuser
# Copy the executable from the "build" stage.
COPY --from=build /bin/server /bin/

COPY ../ .
#COPY migrations migrations
# Copy the migrations directory
COPY migrations migrations

# Expose the port that the application listens on.
EXPOSE 50051

Expand Down
1 change: 1 addition & 0 deletions apps/server/cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"

"github.com/taskemapp/server/apps/server/internal/app"
"go.uber.org/fx"
)
Expand Down
9 changes: 7 additions & 2 deletions apps/server/e2e_tests/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@ package e2e_tests
import (
"context"
"fmt"
"testing"

"github.com/brianvoe/gofakeit/v7"
"github.com/stretchr/testify/require"
"github.com/taskemapp/server/apps/server/internal/config"
"github.com/taskemapp/server/apps/server/internal/pkg/s3"
v1 "github.com/taskemapp/server/apps/server/tools/gen/grpc/v1"
"github.com/taskemapp/server/libs/queue"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"testing"
)

var (
cfg, _ = config.New()
qCfg, _ = queue.NewConfig()
s3Cfg, _ = s3.NewConfig()
cfg, _ = config.New(qCfg, s3Cfg)
testEmail = gofakeit.Email()
testPassword = gofakeit.Password(true, true, true, true, false, 8)
)
Expand Down
4 changes: 2 additions & 2 deletions apps/server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ require (
github.com/rabbitmq/amqp091-go v1.10.0
github.com/samber/lo v1.47.0
github.com/stretchr/testify v1.9.0
github.com/taskemapp/server/apps/notification v0.0.0-20240823134806-8d5115da9e2c
github.com/taskemapp/server/libs/queue v0.0.0-20240831183951-3fedede642c3
github.com/taskemapp/server/apps/notification v0.0.0-20240920224238-1123d7cd13f6
github.com/taskemapp/server/libs/queue v0.0.0-20240906120508-8de35b503387
github.com/taskemapp/server/libs/template v0.0.0-20240912173021-e7f2908c811b
go.uber.org/fx v1.22.2
go.uber.org/multierr v1.11.0
Expand Down
8 changes: 4 additions & 4 deletions apps/server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/taskemapp/server/apps/notification v0.0.0-20240823134806-8d5115da9e2c h1:L4DI7XVHfb2xj4PgxT3d//Lc5g1o/nfsE7dthXRm1cQ=
github.com/taskemapp/server/apps/notification v0.0.0-20240823134806-8d5115da9e2c/go.mod h1:bWtPKaT7sO840/AcRDyMcCBhWXUGqlsOcBdh+7TLxqI=
github.com/taskemapp/server/libs/queue v0.0.0-20240831183951-3fedede642c3 h1:HN5SgxFwgMPb0nB/lSPnDPADRNkytR0S5MymoN0vWOU=
github.com/taskemapp/server/libs/queue v0.0.0-20240831183951-3fedede642c3/go.mod h1:nwYETy6ruFQx4XwfJr7K1cQ3hnf1oEgpP4ds7KJqaxE=
github.com/taskemapp/server/apps/notification v0.0.0-20240920224238-1123d7cd13f6 h1:1W8X3MvcpRiBV1Wj2gBqe5HtomjQHHvVsn4WQ+N09N0=
github.com/taskemapp/server/apps/notification v0.0.0-20240920224238-1123d7cd13f6/go.mod h1:ZbvCuDqNFM8pgNFYsJG5b7QfSNOrqg4SWd/IO814Ttc=
github.com/taskemapp/server/libs/queue v0.0.0-20240906120508-8de35b503387 h1:SMkXkBnYpmTujOTycchi4jpfsgqKDhBLuA0es8/ICRs=
github.com/taskemapp/server/libs/queue v0.0.0-20240906120508-8de35b503387/go.mod h1:nwYETy6ruFQx4XwfJr7K1cQ3hnf1oEgpP4ds7KJqaxE=
github.com/taskemapp/server/libs/template v0.0.0-20240912173021-e7f2908c811b h1:vYfuwHvrq+OabVziP87hQTOc+ta5xasi6yOxFaIbT04=
github.com/taskemapp/server/libs/template v0.0.0-20240912173021-e7f2908c811b/go.mod h1:c0QW0dsaeGAHACHELm5FruaoWCHZ5FYGvo67xx+J1+U=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
Expand Down
87 changes: 27 additions & 60 deletions apps/server/internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ package app

import (
"context"
"database/sql"
"fmt"
"github.com/taskemapp/server/apps/server/internal/pkg/notifier"
"net/url"

"github.com/go-redis/redis/v8"
"github.com/jackc/pgx/v5/pgxpool"
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/pressly/goose/v3"
amqp "github.com/rabbitmq/amqp091-go"
"github.com/taskemapp/server/apps/server/internal/app/auth"
"github.com/taskemapp/server/apps/server/internal/app/grpc"
grpcsrv "github.com/taskemapp/server/apps/server/internal/app/grpc"
"github.com/taskemapp/server/apps/server/internal/app/task"
"github.com/taskemapp/server/apps/server/internal/app/team"
"github.com/taskemapp/server/apps/server/internal/config"
"github.com/taskemapp/server/apps/server/internal/grpc/interceptors"
"github.com/taskemapp/server/apps/server/internal/pkg/migrations"
"github.com/taskemapp/server/apps/server/internal/pkg/s3"
"github.com/taskemapp/server/libs/queue"
"go.uber.org/fx"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"net"
"net/url"
)

const (
Expand All @@ -35,11 +35,18 @@ var App = fx.Options(
fx.Provide(setupPgPool),
fx.Provide(setupRabbitMq),
fx.Provide(setupRedisClient),
fx.Provide(fx.Annotate(func(cfg config.Config) *notifier.BasicGenerator {
return &notifier.BasicGenerator{HostDomain: cfg.HostDomain}
}, fx.As(new(notifier.LinkGenerator)))),

//RabbitMq
fx.Provide(queue.NewConfig),
fx.Provide(fx.Annotate(queue.NewMQ, fx.As(new(queue.Queue)))),

//S3
fx.Provide(s3.NewConfig),
fx.Provide(s3.New),

//General app
auth.App,
team.App,
Expand All @@ -48,64 +55,24 @@ var App = fx.Options(
fx.Provide(grpcsrv.New),

fx.Invoke(
func(p *pgxpool.Pool, c config.Config, log *zap.Logger) error {
if err := goose.SetDialect("pgx"); err != nil {
log.Sugar().Error("Failed to set dialect: ", err)
return err
}
db, err := sql.Open("pgx", c.PostgresUrl)
if err != nil {
log.Sugar().Error("Failed to open db conn: ", err)
return err
}
defer db.Close()

log.Sugar().Info("Run migrations")
err = goose.Up(db, "migrations")
if err != nil {
log.Sugar().Error("Migration failed: ", err)
return err
}

return nil
},
func(lc fx.Lifecycle, log *zap.Logger, c config.Config, srv *grpc.Server) {
lc.Append(
fx.Hook{
OnStart: func(ctx context.Context) error {
log.Sugar().Infof("Server starting on port %d", c.GrpcPort)

l, err := net.Listen("tcp", fmt.Sprintf(":%d", c.GrpcPort))
if err != nil {
return err
}

reflection.Register(srv)

go func() {
err = srv.Serve(l)
if err != nil {
log.Error(err.Error())
return
}
}()

return nil
},
OnStop: func(ctx context.Context) error {
log.Sugar().Info("Gracefully stopping grpc server")
srv.GracefulStop()

return nil
},
},
)
},
migrations.Invoke,
s3.Invoke,
grpc.Invoke,
),
)

func setupConfig() (config.Config, error) {
cfg, err := config.New()
var cfg config.Config
qCfg, err := queue.NewConfig()
if err != nil {
return cfg, err
}
s3Cfg, err := s3.NewConfig()
if err != nil {
return cfg, err
}

cfg, err = config.New(qCfg, s3Cfg)
if err != nil {
return cfg, err
}
Expand Down
37 changes: 37 additions & 0 deletions apps/server/internal/app/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package grpc
import (
"context"
"fmt"
"net"

authMd "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/auth"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/recovery"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/selector"
"github.com/taskemapp/server/apps/server/internal/config"
"github.com/taskemapp/server/apps/server/internal/grpc/auth"
"github.com/taskemapp/server/apps/server/internal/grpc/interceptors"
"github.com/taskemapp/server/apps/server/internal/grpc/team"
Expand All @@ -15,6 +18,7 @@ import (
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/reflection"
"google.golang.org/grpc/status"
)

Expand Down Expand Up @@ -65,6 +69,39 @@ func New(opts Opts) App {
return App{Srv: srv}
}

func Invoke(lc fx.Lifecycle, log *zap.Logger, c config.Config, srv *grpc.Server) {
lc.Append(
fx.Hook{
OnStart: func(ctx context.Context) error {
log.Sugar().Infof("Server starting on port %d", c.GrpcPort)

l, err := net.Listen("tcp", fmt.Sprintf(":%d", c.GrpcPort))
if err != nil {
return err
}

reflection.Register(srv)

go func() {
err = srv.Serve(l)
if err != nil {
log.Error(err.Error())
return
}
}()

return nil
},
OnStop: func(ctx context.Context) error {
log.Sugar().Info("Gracefully stopping grpc server")
srv.GracefulStop()

return nil
},
},
)
}

// interceptorLogger Retrieved from
// https://github.com/grpc-ecosystem/go-grpc-middleware/blob/62b7de50cda5a5d633f1013bfbe50e0f38db34ef/interceptors/logging/examples/zap/example_test.go#L17
func interceptorLogger(l *zap.Logger) logging.Logger {
Expand Down
Loading

0 comments on commit 3c5c299

Please sign in to comment.