Skip to content

Commit 0640694

Browse files
committed
update di
1 parent e029fb8 commit 0640694

File tree

16 files changed

+104
-96
lines changed

16 files changed

+104
-96
lines changed

cmd/mail.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,11 @@ func init() {
1818

1919
func mailFunc(_ *cobra.Command, _ []string) {
2020
a, err := app.New()
21-
if err != nil {
22-
panic(fmt.Sprintf("%+v\n", err))
23-
}
2421
defer a.Close()
25-
if err = a.Init(); err != nil {
22+
if err != nil {
2623
panic(fmt.Sprintf("%+v\n", err))
2724
}
28-
fmt.Printf("%+v\n", a.Config)
29-
a.Mailer.Send("[email protected]", "Hello", "Hello from the other side!")
25+
fmt.Printf("%+v\n", a.Container.Config)
26+
a.Container.Mailer.Send("[email protected]", "Hello", "Hello from the other side!")
3027
fmt.Println("Done!")
3128
}

cmd/serve.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@ func init() {
1818

1919
func serveFunc(_ *cobra.Command, _ []string) {
2020
a, err := app.New()
21-
if err != nil {
22-
panic(fmt.Sprintf("%+v\n", err))
23-
}
2421
defer a.Close()
25-
if err = a.Init(); err != nil {
22+
if err != nil {
2623
panic(fmt.Sprintf("%+v\n", err))
2724
}
2825
a.HttpServer.Serve()

internal/app/app.go

+35-34
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import (
44
"context"
55
"github.com/cockroachdb/errors"
66
"github.com/neatplex/nightell-core/internal/config"
7+
"github.com/neatplex/nightell-core/internal/container"
78
"github.com/neatplex/nightell-core/internal/database"
89
"github.com/neatplex/nightell-core/internal/gc"
910
httpServer "github.com/neatplex/nightell-core/internal/http/server"
1011
"github.com/neatplex/nightell-core/internal/logger"
1112
"github.com/neatplex/nightell-core/internal/mailer"
1213
"github.com/neatplex/nightell-core/internal/s3"
13-
"github.com/neatplex/nightell-core/internal/services/container"
1414
"go.uber.org/zap"
1515
"os"
1616
"os/signal"
@@ -20,50 +20,51 @@ import (
2020
// App integrates the modules to serve.
2121
type App struct {
2222
context context.Context
23-
Config *config.Config
24-
Logger *logger.Logger
25-
S3 *s3.S3
2623
HttpServer *httpServer.Server
27-
MySQL *database.Database
2824
Container *container.Container
29-
Mailer *mailer.Mailer
30-
Gc *gc.Gc
3125
}
3226

3327
// New creates an app from the given configuration file.
3428
func New() (a *App, err error) {
3529
a = &App{}
3630

37-
a.Config = config.New()
38-
if err = a.Config.Init(); err != nil {
39-
return nil, errors.WithStack(err)
31+
c := config.New()
32+
if err = c.Init(); err != nil {
33+
return a, errors.WithStack(err)
4034
}
41-
a.Logger = logger.New(a.Config.Logger.Level, a.Config.Logger.Format, a.Config.Development)
42-
if err = a.Logger.Init(); err != nil {
43-
return nil, errors.WithStack(err)
35+
36+
l := logger.New(c.Logger.Level, c.Logger.Format, c.Development)
37+
if err = l.Init(); err != nil {
38+
return a, errors.WithStack(err)
39+
}
40+
41+
db := database.New(c, l)
42+
if err = db.Init(); err != nil {
43+
return a, errors.WithStack(err)
4444
}
45-
a.Logger.Debug("app: Config & LoggerProxy initialized")
46-
47-
a.Mailer = mailer.New(a.Config, a.Logger)
48-
a.MySQL = database.New(a.Config, a.Logger)
49-
a.S3 = s3.New(a.Config, a.Logger)
50-
a.Container = container.New(a.MySQL, a.S3, a.Mailer)
51-
a.Gc = gc.New(a.MySQL, a.S3, a.Logger)
52-
a.HttpServer = httpServer.New(a.Config, a.Logger, a.Container, a.Mailer)
53-
a.Logger.Debug("app: application modules initialized")
45+
46+
awsS3 := s3.New(c, l)
47+
if err = awsS3.Init(); err != nil {
48+
return a, errors.WithStack(err)
49+
}
50+
51+
s3gc := gc.New(db, awsS3, l)
52+
s3gc.Init()
53+
54+
m := mailer.New(c, l)
55+
56+
a.Container = container.New(c, l, awsS3, db, m, s3gc)
57+
a.HttpServer = httpServer.New(a.Container)
58+
5459
a.setupSignalListener()
5560

61+
l.Debug("app: modules initialized")
62+
5663
return a, nil
5764
}
5865

5966
func (a *App) Init() error {
60-
if err := a.MySQL.Init(); err != nil {
61-
return errors.WithStack(err)
62-
}
63-
if err := a.S3.Init(); err != nil {
64-
return errors.WithStack(err)
65-
}
66-
a.Gc.Init()
67+
6768
return nil
6869
}
6970

@@ -77,7 +78,7 @@ func (a *App) setupSignalListener() {
7778

7879
go func() {
7980
s := <-signalChannel
80-
a.Logger.Info("app: system call", zap.String("signal", s.String()))
81+
a.Container.Logger.Info("app: system call", zap.String("signal", s.String()))
8182
cancel()
8283
}()
8384
}
@@ -86,11 +87,11 @@ func (a *App) Close() {
8687
if a.HttpServer != nil {
8788
a.HttpServer.Close()
8889
}
89-
if a.MySQL != nil {
90-
a.MySQL.Close()
90+
if a.Container.DB != nil {
91+
a.Container.DB.Close()
9192
}
92-
if a.Logger != nil {
93-
a.Logger.Close()
93+
if a.Container.Logger != nil {
94+
a.Container.Logger.Close()
9495
}
9596
}
9697

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package container
22

33
import (
4+
"github.com/neatplex/nightell-core/internal/config"
45
"github.com/neatplex/nightell-core/internal/database"
6+
"github.com/neatplex/nightell-core/internal/gc"
7+
"github.com/neatplex/nightell-core/internal/logger"
58
"github.com/neatplex/nightell-core/internal/mailer"
69
"github.com/neatplex/nightell-core/internal/s3"
710
"github.com/neatplex/nightell-core/internal/services/file"
@@ -15,6 +18,12 @@ import (
1518
)
1619

1720
type Container struct {
21+
Config *config.Config
22+
Logger *logger.Logger
23+
S3 *s3.S3
24+
DB *database.Database
25+
Mailer *mailer.Mailer
26+
GC *gc.Gc
1827
UserService *user.Service
1928
TokenService *token.Service
2029
RemoveService *remove.Service
@@ -25,15 +34,28 @@ type Container struct {
2534
OtpService *otp.Service
2635
}
2736

28-
func New(d *database.Database, s3 *s3.S3, m *mailer.Mailer) *Container {
37+
func New(
38+
config *config.Config,
39+
logger *logger.Logger,
40+
s3 *s3.S3,
41+
db *database.Database,
42+
mailer *mailer.Mailer,
43+
gc *gc.Gc,
44+
) *Container {
2945
return &Container{
30-
UserService: user.New(d, m),
31-
TokenService: token.New(d),
32-
RemoveService: remove.New(d),
33-
PostService: post.New(d),
34-
FileService: file.New(d, s3),
35-
LikeService: like.New(d),
36-
FollowshipService: followship.New(d),
37-
OtpService: otp.New(m),
46+
Config: config,
47+
Logger: logger,
48+
S3: s3,
49+
DB: db,
50+
Mailer: mailer,
51+
GC: gc,
52+
UserService: user.New(db, mailer),
53+
TokenService: token.New(db),
54+
RemoveService: remove.New(db),
55+
PostService: post.New(db),
56+
FileService: file.New(db, s3),
57+
LikeService: like.New(db),
58+
FollowshipService: followship.New(db),
59+
OtpService: otp.New(mailer),
3860
}
3961
}

internal/http/server/handlers/delete.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@ import (
44
"fmt"
55
"github.com/cockroachdb/errors"
66
"github.com/labstack/echo/v4"
7-
"github.com/neatplex/nightell-core/internal/config"
8-
"github.com/neatplex/nightell-core/internal/mailer"
9-
"github.com/neatplex/nightell-core/internal/services/container"
7+
"github.com/neatplex/nightell-core/internal/container"
108
"net/http"
119
)
1210

1311
type deleteRequest struct {
1412
Email string `json:"email" validate:"required,email"`
1513
}
1614

17-
func DeleteRequest(ctr *container.Container, cfg *config.Config, mailer *mailer.Mailer) echo.HandlerFunc {
15+
func DeleteRequest(ctr *container.Container) echo.HandlerFunc {
1816
return func(ctx echo.Context) error {
1917
var r deleteRequest
2018
if err := ctx.Bind(&r); err != nil {
@@ -38,8 +36,8 @@ func DeleteRequest(ctr *container.Container, cfg *config.Config, mailer *mailer.
3836
if err != nil {
3937
return errors.WithStack(err)
4038
}
41-
link := fmt.Sprintf("%s/delete-account?q=%s", cfg.URL, code)
42-
mailer.SendDeleteAccount(user.Email, user.Username, link)
39+
link := fmt.Sprintf("%s/delete-account?q=%s", ctr.Config.URL, code)
40+
ctr.Mailer.SendDeleteAccount(user.Email, user.Username, link)
4341
}
4442

4543
return ctx.JSON(200, map[string]string{

internal/http/server/handlers/v1/auth.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ package v1
33
import (
44
"github.com/cockroachdb/errors"
55
"github.com/labstack/echo/v4"
6-
"github.com/neatplex/nightell-core/internal/config"
6+
"github.com/neatplex/nightell-core/internal/container"
77
"github.com/neatplex/nightell-core/internal/models"
8-
"github.com/neatplex/nightell-core/internal/services/container"
98
"github.com/neatplex/nightell-core/internal/utils"
109
"golang.org/x/crypto/bcrypt"
1110
"google.golang.org/api/idtoken"
@@ -288,7 +287,7 @@ func AuthSignInUsername(ctr *container.Container) echo.HandlerFunc {
288287
}
289288
}
290289

291-
func AuthSignInGoogle(ctr *container.Container, cfg *config.Config) echo.HandlerFunc {
290+
func AuthSignInGoogle(ctr *container.Container) echo.HandlerFunc {
292291
return func(ctx echo.Context) error {
293292
var r signInGoogleRequest
294293
if err := ctx.Bind(&r); err != nil {
@@ -302,7 +301,7 @@ func AuthSignInGoogle(ctr *container.Container, cfg *config.Config) echo.Handler
302301
})
303302
}
304303

305-
payload, err := idtoken.Validate(ctx.Request().Context(), r.Token, cfg.Google.OAuthClientId)
304+
payload, err := idtoken.Validate(ctx.Request().Context(), r.Token, ctr.Config.Google.OAuthClientId)
306305
if err != nil {
307306
return ctx.JSON(http.StatusUnauthorized, map[string]string{
308307
"message": "Cannot fetch account from Google.",

internal/http/server/handlers/v1/feed.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package v1
33
import (
44
"github.com/cockroachdb/errors"
55
"github.com/labstack/echo/v4"
6+
"github.com/neatplex/nightell-core/internal/container"
67
"github.com/neatplex/nightell-core/internal/models"
7-
"github.com/neatplex/nightell-core/internal/services/container"
88
"github.com/neatplex/nightell-core/internal/utils"
99
"net/http"
1010
)

internal/http/server/handlers/v1/files.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@ import (
44
"fmt"
55
"github.com/cockroachdb/errors"
66
"github.com/labstack/echo/v4"
7-
"github.com/neatplex/nightell-core/internal/logger"
7+
"github.com/neatplex/nightell-core/internal/container"
88
"github.com/neatplex/nightell-core/internal/models"
9-
"github.com/neatplex/nightell-core/internal/services/container"
10-
"go.uber.org/zap"
119
"net/http"
1210
)
1311

14-
func FilesStore(ctr *container.Container, l *logger.Logger) echo.HandlerFunc {
12+
func FilesStore(ctr *container.Container) echo.HandlerFunc {
1513
return func(ctx echo.Context) error {
1614
user := ctx.Get("user").(*models.User)
1715

@@ -24,7 +22,6 @@ func FilesStore(ctr *container.Container, l *logger.Logger) echo.HandlerFunc {
2422

2523
formFile, err := ctx.FormFile("file")
2624
if err != nil {
27-
l.Debug("cannot get form file", zap.Error(err))
2825
return ctx.JSON(http.StatusUnprocessableEntity, map[string]string{
2926
"message": "File is not uploaded.",
3027
})

internal/http/server/handlers/v1/likes.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package v1
33
import (
44
"github.com/cockroachdb/errors"
55
"github.com/labstack/echo/v4"
6+
"github.com/neatplex/nightell-core/internal/container"
67
"github.com/neatplex/nightell-core/internal/models"
7-
"github.com/neatplex/nightell-core/internal/services/container"
88
"github.com/neatplex/nightell-core/internal/utils"
99
"net/http"
1010
)

internal/http/server/handlers/v1/posts.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package v1
33
import (
44
"github.com/cockroachdb/errors"
55
"github.com/labstack/echo/v4"
6+
"github.com/neatplex/nightell-core/internal/container"
67
"github.com/neatplex/nightell-core/internal/models"
7-
"github.com/neatplex/nightell-core/internal/services/container"
88
"github.com/neatplex/nightell-core/internal/utils"
99
"net/http"
1010
)

internal/http/server/handlers/v1/profile.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package v1
33
import (
44
"github.com/cockroachdb/errors"
55
"github.com/labstack/echo/v4"
6+
"github.com/neatplex/nightell-core/internal/container"
67
"github.com/neatplex/nightell-core/internal/models"
7-
"github.com/neatplex/nightell-core/internal/services/container"
88
userService "github.com/neatplex/nightell-core/internal/services/user"
99
"github.com/neatplex/nightell-core/internal/utils"
1010
"net/http"

internal/http/server/handlers/v1/search.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package v1
33
import (
44
"github.com/cockroachdb/errors"
55
"github.com/labstack/echo/v4"
6+
"github.com/neatplex/nightell-core/internal/container"
67
"github.com/neatplex/nightell-core/internal/models"
7-
"github.com/neatplex/nightell-core/internal/services/container"
88
"github.com/neatplex/nightell-core/internal/utils"
99
"net/http"
1010
)

internal/http/server/handlers/v1/users.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package v1
33
import (
44
"github.com/cockroachdb/errors"
55
"github.com/labstack/echo/v4"
6+
"github.com/neatplex/nightell-core/internal/container"
67
"github.com/neatplex/nightell-core/internal/models"
7-
"github.com/neatplex/nightell-core/internal/services/container"
88
"github.com/neatplex/nightell-core/internal/utils"
99
"net/http"
1010
)

internal/http/server/middleware/auth.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package middleware
33
import (
44
"github.com/cockroachdb/errors"
55
"github.com/labstack/echo/v4"
6-
"github.com/neatplex/nightell-core/internal/services/container"
6+
"github.com/neatplex/nightell-core/internal/container"
77
)
88

99
func Authorize(ctr *container.Container) func(echo.HandlerFunc) echo.HandlerFunc {

internal/http/server/routes.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func (s *Server) registerRoutes() {
1111
s.E.GET("/healthz", handlers.Healthz)
1212

1313
// delete-account
14-
s.E.POST("/delete-request", handlers.DeleteRequest(s.container, s.config, s.mailer))
14+
s.E.POST("/delete-request", handlers.DeleteRequest(s.container))
1515
s.E.GET("/delete-account", handlers.DeleteAccount(s.container))
1616

1717
v1Api := s.E.Group("/api/v1", middleware.RateLimiter(middleware.NewRateLimiterMemoryStore(5)))
@@ -22,7 +22,7 @@ func (s *Server) registerRoutes() {
2222
public.POST("auth/sign-up", v1.AuthSignUp(s.container))
2323
public.POST("auth/sign-in/email", v1.AuthSignInEmail(s.container))
2424
public.POST("auth/sign-in/username", v1.AuthSignInUsername(s.container))
25-
public.POST("auth/sign-in/google", v1.AuthSignInGoogle(s.container, s.config))
25+
public.POST("auth/sign-in/google", v1.AuthSignInGoogle(s.container))
2626
public.POST("auth/otp/email/generate", v1.AuthOtpEmailGenerate(s.container))
2727
public.POST("auth/otp/email/submit", v1.AuthOtpEmailSubmit(s.container))
2828
}
@@ -53,7 +53,7 @@ func (s *Server) registerRoutes() {
5353
private.POST("posts/:postId/likes", v1.LikesStore(s.container))
5454
private.DELETE("likes/:likeId", v1.LikesDelete(s.container))
5555
// files
56-
private.POST("files", v1.FilesStore(s.container, s.l))
56+
private.POST("files", v1.FilesStore(s.container))
5757
// search
5858
private.GET("search", v1.SearchPosts(s.container))
5959
private.GET("search/posts", v1.SearchPosts(s.container))

0 commit comments

Comments
 (0)