Skip to content

Commit

Permalink
Merge pull request #21 from dami-pie/infra/restruct
Browse files Browse the repository at this point in the history
infra: modificações estruturais nas pastas
  • Loading branch information
Sevlak committed Jun 18, 2023
2 parents 2c8c468 + 6e3a44b commit f46dd03
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 45 deletions.
5 changes: 3 additions & 2 deletions src/config/config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package config

import (
"github.com/joho/godotenv"
"log"
"os"

"github.com/joho/godotenv"
)

var (
Expand All @@ -16,7 +17,7 @@ var (
KeyFile = ""
)

func init() {
func LoadEnv() {
err := godotenv.Load(".env")

if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions src/controllers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package controllers

import (
"errors"
"github.com/dami-pie/napi/models"
"github.com/dami-pie/napi/src/auth"
"github.com/dami-pie/napi/src/responses"
"log"
"net/http"

"github.com/dami-pie/napi/models"
"github.com/dami-pie/napi/src/responses"
auth "github.com/dami-pie/napi/src/services"
)

func Login(res http.ResponseWriter, req *http.Request) {
Expand Down
5 changes: 3 additions & 2 deletions src/middlewares/middlewares.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package middlewares

import (
"github.com/dami-pie/napi/src/auth"
"github.com/dami-pie/napi/src/responses"
"log"
"net/http"

"github.com/dami-pie/napi/src/responses"
auth "github.com/dami-pie/napi/src/services"
)

// Escreve as informações da requisição no terminal
Expand Down
19 changes: 16 additions & 3 deletions src/router/router.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
package router

import (
"github.com/dami-pie/napi/src/router/routes"
"github.com/dami-pie/napi/src/middlewares"
"github.com/dami-pie/napi/src/routes"
"github.com/gorilla/mux"
)

func AddRoutes() *mux.Router {
// Configurar coloca todas as rotas dentro do router
func ConfigRoutes() *mux.Router {
r := mux.NewRouter()
return routes.ConfigRoutes(r)
routes := routes.AuthRoutes

for _, route := range routes {
if route.RequerAutenticacao {
r.HandleFunc(route.URI, middlewares.Logger(middlewares.AuthenticateUser(route.Funcao))).Methods(route.Metodo)
} else {
r.HandleFunc(route.URI, middlewares.Logger(route.Funcao)).Methods(route.Metodo)
}

}

return r
}
30 changes: 0 additions & 30 deletions src/router/routes/routes.go

This file was deleted.

5 changes: 3 additions & 2 deletions src/router/routes/auth.go → src/routes/auth.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package routes

import (
"github.com/dami-pie/napi/src/controllers"
"net/http"

"github.com/dami-pie/napi/src/controllers"
)

var apiRoutes = []Route{
var AuthRoutes = []Route{
{
URI: "/authenticate",
Metodo: http.MethodPost,
Expand Down
10 changes: 10 additions & 0 deletions src/routes/routes_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package routes

import "net/http"

type Route struct {
URI string
Metodo string
Funcao func(http.ResponseWriter, *http.Request)
RequerAutenticacao bool
}
9 changes: 6 additions & 3 deletions src/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package server

import (
"fmt"
"log"
"net/http"

"github.com/dami-pie/napi/src/config"
"github.com/dami-pie/napi/src/router"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"log"
"net/http"
)

type Server struct {
Expand All @@ -16,9 +17,11 @@ type Server struct {
}

func NewServer() *Server {
config.LoadEnv()

return &Server{
port: config.Port,
routes: router.AddRoutes(),
routes: router.ConfigRoutes(),
}
}

Expand Down
File renamed without changes.

0 comments on commit f46dd03

Please sign in to comment.