solrac97gr / go-jwt-auth

Template create for use as CookieCutter for my Golang projects, I decided to create a template with everything already working. 🚀
88 stars 16 forks source link

Where to define the interfaces please? #3

Open suntong opened 1 year ago

suntong commented 1 year ago

https://github.com/solrac97gr/go-jwt-auth/blob/f52fb4aa4bb191850660c83132d3926bd86c7dad/README.md?plain=1#L60-L73

Define your interfaces inside the repository.go,handlers.go and application.go file.

Hi, I don't quite get where exactly the above interfaces code should be put -- it should be put in only one of the above three files, not all of them right? If so, which one then?

suntong commented 1 year ago

Oh, it seems that the PostRepository should go into the repository.go, while handlers.go and application.go will have their own interfaces too, right? Maybe the interface code for handlers.go and application.go are missing from the README?

UPDATE: the handlers.go file seems to be:

package ports

import (
    "github.com/gofiber/fiber/v2"
)

type PostHandler interface {
    CreatePost(ctx *fiber.Ctx) error
}

and I have not been able to figured out what to put into application.go yet.

BTW, it would be really great if the result of the 14 steps in README can be published into a branch. Thanks.

solrac97gr commented 1 year ago

Hi, @suntong since the interfaces are core part of the package (define the behavior of how the parts of the app should interact) they are inside of the internal/user/domain/ports, in recent projects I start to define the handlers interface outside of the domain since not always the app will communicate throw HTTP it can be also console app, etc.

In the case you wanna add a new context for example blogs the interface most be in internal/blogs/domain/ports I like to be very explicit creating a package specific for the interfaces, in other projects I also notice people define the interfaces only inside of domain, its also ok but when projects are huge models and interfaces start to be more difficult to find if they are not separeted.

suntong commented 1 year ago

I haven't been able to make it work yet,

Oh, it seems that the PostRepository should go into the repository.go

I put the above quoted code there, and here are what I've changed to pkg/server/server.go:

--- a/blog/pkg/server/server.go
+++ b/blog/pkg/server/server.go
@@ -9,6 +9,7 @@ import (
        "github.com/gofiber/swagger"
        "github.com/golang-jwt/jwt/v4"
        userHdl "backend/internal/user/domain/ports"
+       postHdl "backend/internal/post/domain/ports"
        configurator "backend/pkg/config/domain/ports"
        mdl "backend/pkg/middleware/domain/ports"
 )
@@ -16,6 +17,7 @@ import (
 type Server struct {
        mdl          mdl.MiddlewareHandlers
        user         userHdl.UserHandlers
+       postHandler  postHdl.HTTPPostHandlers
        configurator configurator.ConfigApplication
 }

@@ -42,6 +44,7 @@ func (s *Server) Run(port string) error {

        v1Private := app.Group("/api/v1").Use(s.mdl.Authenticate())
        // Test Endpoint
+       v1Private.Post("/posts", s.postHandler.CreatePost)
        v1Private.Get("/secret", func(c *fiber.Ctx) error {
                user := c.Locals("user").(*jwt.Token)
                claims := user.Claims.(jwt.MapClaims)

and I'm getting

pkg/server/server.go:20:23: undefined: postHdl.HTTPPostHandlers

If I use PostHandlers there, then the error is:

pkg/server/server.go:20:23: undefined: postHdl.PostHandlers

For the s.postHandler.CreatePost in the README it was: h.postHandler.CreatePost yet I don't see any variable named h in the function. IE I was trying to fill the missing pieces/gaps myself, but I'm a bit lost now when putting the puzzles together.

BTW, it would be really great if the result of the 14 steps in README can be published into a branch.

Or would you put back the missing pieces and correct the mistakes in the README file please?

Thanks.