Skip to content

Commit

Permalink
🚧 wip (router): Basic router function with the API and app's routes s…
Browse files Browse the repository at this point in the history
…eparated into groups
  • Loading branch information
kevinmarquesp committed Jul 20, 2024
1 parent 44c56b3 commit 3646536
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions internal/router/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package router

import (
"errors"
"net/http"

"github.com/kevinmarquesp/go-postr/internal/controllers"
"github.com/kevinmarquesp/go-postr/internal/repositories"
"github.com/kevinmarquesp/go-postr/internal/services"
)

func StartRouter(port string) error {
if port == "" {
return errors.New("Empty port value error.")
}

// Enabled repositories.

userRepository, err := repositories.NewSqliteUserRepository("./tmp/database.sqlite3")
if err != nil {
return err
}

// Enalbed services.

authenticationService := services.NewGopostrAuthenticationService(userRepository)

// Routes definition.

apiRouter := http.NewServeMux()

apiUserHandler := controllers.NewApiUserHandler(authenticationService)

apiRouter.HandleFunc("POST /user", apiUserHandler.RegisterNewUserWithCredentials)
apiRouter.HandleFunc("GET /user/{username}", apiUserHandler.FetchUserDataByUsername)
apiRouter.HandleFunc("PUT /user", apiUserHandler.UpdateProfileDetails)
apiRouter.HandleFunc("DELETE /user", apiUserHandler.RemoveRegisteredUser)

router := http.NewServeMux()

router.Handle("/api/v1/", http.StripPrefix("/api/v1", apiRouter))

// Server initialization.

server := http.Server{
Addr: ":" + port,
Handler: router,
}

return server.ListenAndServe()
}

0 comments on commit 3646536

Please sign in to comment.