Skip to content

Commit

Permalink
Merge pull request #2 from Yinebeb-01/mongo-repo
Browse files Browse the repository at this point in the history
Mongo repo
  • Loading branch information
yinebebt authored Mar 8, 2024
2 parents 0be5244 + 069ede2 commit 4dc2ed2
Show file tree
Hide file tree
Showing 30 changed files with 467 additions and 344 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ RUN go mod download
# Copy all the app sources (recursively copies files and directories from the host into the image)
COPY . .

# Set http port
# Set rest port
ENV PORT 5000

# Build the app
Expand Down
1 change: 0 additions & 1 deletion Procfile

This file was deleted.

56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,59 @@ go install github.com/cucumber/godog/cmd/godog@latest
```

Use `go test` command to run feature tests since godog's cli is deprecated.


## Project structure

- `bin/`: Compiled binaries are stored in this directory after building the project.

- `cmd/`: Contains command-line application entry points.

- `internal/`: Internal packages of the project are located here.

- `adapter/`: Contains adapters for external systems.

- `handler/`: Handler adapters for different protocols (e.g., HTTP(REST), gRPC) are stored here.

- `rest/`: HTTP handler adapter implementation is located in this directory.

- `repository/`: Repository adapters for various databases are stored here.

- `gorm(sqlite)/`: Gorm(sqlite) repository adapter implementation resides here.

- `core/`: Contains the core business logic of the application.

- `entity/`: Domain entities representing specific objects within the application's domain are stored here.

- `port/`: Ports (interfaces) defining interactions with adapters are stored in this directory.

- `service/`: Core application services are located here.

- `util/`: Utility functions and helpers used across the project are stored in this directory.


```
/app
|-- /cmd
| |-- main.go
|-- /docs
|-- /internal
| |-- /adapter
| | |-- /handler
| | | |-- /rest
| | | |-- /gRPC
| | |-- /reository
| | | |-- /gorm
| | | |-- /mongo
| | |-- /glue
| | | |-- /route
| | | |-- route.go
| | |-- /dto
| | |-- /templates
| |-- /core
| | |-- /entity
| | |-- /port
| | |-- /service
| | | |-- /test
| | |-- /util
```
56 changes: 56 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"gitlab.com/Yinebeb-01/hexagonalarch/internal/adapter/glue/route"
"gitlab.com/Yinebeb-01/hexagonalarch/internal/adapter/handler/middleware"
"gitlab.com/Yinebeb-01/hexagonalarch/internal/adapter/handler/rest"
"gitlab.com/Yinebeb-01/hexagonalarch/internal/adapter/repository/gorm"
"gitlab.com/Yinebeb-01/hexagonalarch/internal/core/service"
"io"
"log"
"os"

"github.com/gin-gonic/gin"
dump "github.com/tpkeeper/gin-dump"
)

var (
videoRepository = gorm.NewVideoRepository()
loginService = service.NewLoginService()
jwtService = service.NewJWTService()

videoService = service.New(videoRepository)
videoHandler = rest.InitVideo(videoService)
loginHandler = rest.InitLogin(loginService, jwtService)
)

func main() {
configOutput()

router := gin.New()
// dump is an alise of gin-dum, used for debugging.
router.Use(gin.Recovery(), middleware.Logger(), dump.Dump())
router.Static("/css", "./internal/adapter/templates/css")
router.LoadHTMLGlob("./internal/adapter/templates/*.html")

v1 := router.Group("/v1")
route.InitVideoRoute(v1, videoHandler)
route.InitLoginRoute(v1, loginHandler)

log.Println("router initialized")

port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
router.Run(":" + port)
}

// configOutput create a custom logger file to see debugging outputs.
func configOutput() {
writer, err := os.Create("hexagonal_arch.log")
if err != nil {
log.Println("unable to create log file")
}
gin.DefaultWriter = io.MultiWriter(os.Stdout, writer)
}
36 changes: 0 additions & 36 deletions controller/login_controller.go

This file was deleted.

106 changes: 0 additions & 106 deletions controller/video_controller.go

This file was deleted.

File renamed without changes.
20 changes: 20 additions & 0 deletions internal/adapter/glue/route.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package glue

import (
"github.com/gin-gonic/gin"
)

type Router struct {
Method string
Path string
Handler gin.HandlerFunc
}

func RegisterRoutes(group *gin.RouterGroup, routes []Router, middleware []gin.HandlerFunc) {
for _, route := range routes {
var handler []gin.HandlerFunc
handler = append(handler, middleware...)
handler = append(handler, route.Handler)
group.Handle(route.Method, route.Path, handler...)
}
}
21 changes: 21 additions & 0 deletions internal/adapter/glue/route/login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package route

import (
"github.com/gin-gonic/gin"
"gitlab.com/Yinebeb-01/hexagonalarch/internal/adapter/glue"
"gitlab.com/Yinebeb-01/hexagonalarch/internal/core/port"
"net/http"
)

func InitLoginRoute(grp *gin.RouterGroup, handler port.LoginHandler) {
loginRoutes := []glue.Router{
{
Method: http.MethodPost,
Path: "/login",
Handler: handler.Login,
},
}

// Login Endpoint: Authentication
glue.RegisterRoutes(grp.Group(""), loginRoutes, []gin.HandlerFunc{})
}
50 changes: 50 additions & 0 deletions internal/adapter/glue/route/video.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package route

import (
"github.com/gin-gonic/gin"
"gitlab.com/Yinebeb-01/hexagonalarch/internal/adapter/glue"
middlewares2 "gitlab.com/Yinebeb-01/hexagonalarch/internal/adapter/handler/middleware"
"gitlab.com/Yinebeb-01/hexagonalarch/internal/core/port"
"net/http"
)

func InitVideoRoute(grp *gin.RouterGroup, video port.VideoHandler) {
videoRoutes := []glue.Router{
{
Method: http.MethodPost,
Path: "/videos",
Handler: video.Save,
},
{
Method: http.MethodDelete,
Path: "/videos/:id",
Handler: video.Delete,
},
{
Method: http.MethodPut,
Path: "/videos/:id",
Handler: video.Update,
},
{
Method: http.MethodGet,
Path: "/test",
Handler: func(ctx *gin.Context) {
ctx.JSON(http.StatusOK, gin.H{
"Message": "hello",
})
},
},
}
viewRoutes := []glue.Router{
{
Method: http.MethodGet,
Handler: video.ShowAll,
Path: "/videos",
},
}

//apiRoute group used to group 'api/*' endpoints.
glue.RegisterRoutes(grp.Group("/api"), videoRoutes, []gin.HandlerFunc{middlewares2.AuthorizeJWT()})
//viewRoute Group will use to render static files
glue.RegisterRoutes(grp.Group("/view"), viewRoutes, []gin.HandlerFunc{})
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package middlewares
package middleware

import (
"gitlab.com/Yinebeb-01/hexagonalarch/internal/core/service"
"log"
"net/http"

"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
"gitlab.com/Yinebeb-01/hexagonalarch/services"
)

// AuthorizeJWT validates the token from the http request, returning a 401 if it's not valid
Expand All @@ -16,7 +16,7 @@ func AuthorizeJWT() gin.HandlerFunc {
authHeader := c.GetHeader("Authorization")
tokenString := authHeader[len(BearerSchema):]

token, err := services.NewJWTService().ValidateToken(tokenString)
token, err := service.NewJWTService().ValidateToken(tokenString)

if token.Valid {
claims := token.Claims.(jwt.MapClaims)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package middlewares
package middleware

import (
"fmt"
Expand Down
Loading

0 comments on commit 4dc2ed2

Please sign in to comment.