Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
eminetto committed Apr 4, 2023
1 parent ac97c85 commit 79836c7
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 53 deletions.
3 changes: 1 addition & 2 deletions auth/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ module auth
go 1.12

require (
github.com/codegangsta/negroni v1.0.0
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/go-chi/chi/v5 v5.0.8
github.com/go-sql-driver/mysql v1.7.0
github.com/google/uuid v1.3.0
github.com/gorilla/context v1.1.1
github.com/gorilla/mux v1.7.3
github.com/stretchr/testify v1.4.0
)
6 changes: 2 additions & 4 deletions auth/go.sum
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
github.com/codegangsta/negroni v1.0.0 h1:+aYywywx4bnKXWvoWtRfJ91vC59NbEhEY03sZjQhbVY=
github.com/codegangsta/negroni v1.0.0/go.mod h1:v0y3T5G7Y1UlFfyxFn/QLRU4a2EuNau2iZY63YTKWo0=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0=
github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/context v1.1.1 h1:AWwleXJkX/nhcU9bZSnZoi3h/qGYqQAGhq6zZe/aQW8=
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
31 changes: 12 additions & 19 deletions auth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
"os"
"time"

"github.com/codegangsta/negroni" //@todo replace with Chi
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/context"
"github.com/gorilla/mux"
)

// @todo get this variables from env or config
Expand All @@ -36,18 +36,11 @@ func main() {
defer db.Close()
repo := mysql.NewUserMySQL(db)
uService := user.NewService(repo)
r := mux.NewRouter()
//handlers
n := negroni.New(
negroni.NewLogger(),
)
r.Handle("/v1/auth", n.With(
negroni.Wrap(userAuth(uService)),
)).Methods("POST", "OPTIONS")

r.Handle("/v1/validate-token", n.With(
negroni.Wrap(validateToken()),
)).Methods("POST", "OPTIONS")
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Post("/v1/auth", userAuth(uService))
r.Post("/v1/validate-token", validateToken())

http.Handle("/", r)
logger := log.New(os.Stderr, "logger: ", log.Lshortfile)
Expand All @@ -64,8 +57,8 @@ func main() {
}
}

func userAuth(uService user.UseCase) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
func userAuth(uService user.UseCase) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var param struct {
Email string `json:"email"`
Password string `json:"password"`
Expand Down Expand Up @@ -94,11 +87,11 @@ func userAuth(uService user.UseCase) http.Handler {
return
}
return
})
}
}

func validateToken() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
func validateToken() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var param struct {
Token string `json:"token"`
}
Expand Down Expand Up @@ -128,5 +121,5 @@ func validateToken() http.Handler {
return
}
return
})
}
}
5 changes: 1 addition & 4 deletions feedbacks/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ module feedbacks
go 1.12

require (
github.com/codegangsta/negroni v1.0.0
github.com/eminetto/talk-microservices-go v0.0.0-20190807013808-e8536eb92734
github.com/go-chi/chi/v5 v5.0.8
github.com/go-sql-driver/mysql v1.7.0
github.com/google/uuid v1.1.1
github.com/gorilla/context v1.1.1
github.com/gorilla/mux v1.7.3
)
10 changes: 2 additions & 8 deletions feedbacks/go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
github.com/codegangsta/negroni v1.0.0 h1:+aYywywx4bnKXWvoWtRfJ91vC59NbEhEY03sZjQhbVY=
github.com/codegangsta/negroni v1.0.0/go.mod h1:v0y3T5G7Y1UlFfyxFn/QLRU4a2EuNau2iZY63YTKWo0=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/eminetto/talk-microservices-go v0.0.0-20190807013808-e8536eb92734 h1:tyIwtofCljMO3NlMIm6sqnHxMIjGonZd89+jObAicsU=
github.com/eminetto/talk-microservices-go v0.0.0-20190807013808-e8536eb92734/go.mod h1:cjmfNGBcwKRs6aNxiiWpaYsFbv3qb4LP3B1JcCkUOUs=
github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0=
github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/context v1.1.1 h1:AWwleXJkX/nhcU9bZSnZoi3h/qGYqQAGhq6zZe/aQW8=
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
22 changes: 14 additions & 8 deletions feedbacks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"feedbacks/feedback"
"feedbacks/feedback/mysql"
"fmt"
"github.com/codegangsta/negroni"
"github.com/eminetto/talk-microservices-go/pkg/middleware"
"github.com/eminetto/api-o11y/pkg/middleware"
"github.com/go-chi/chi/v5"
chimiddleware "github.com/go-chi/chi/v5/middleware"
_ "github.com/go-sql-driver/mysql"
"github.com/google/uuid"
"github.com/gorilla/context"
"github.com/gorilla/mux"
"log"
"net/http"
"os"
Expand All @@ -37,7 +37,13 @@ func main() {
repo := mysql.NewUserMySQL(db)

fService := feedback.NewService(repo)
r := mux.NewRouter()

r := chi.NewRouter()
r.Use(chimiddleware.Logger)
r.Use(middleware.IsAuthenticated)
r.Post("/v1/feedback", storeFeedback(fService))

/*r := mux.NewRouter()
//handlers
n := negroni.New(
negroni.NewLogger(),
Expand All @@ -46,7 +52,7 @@ func main() {
negroni.HandlerFunc(middleware.IsAuthenticated()),
negroni.Wrap(storeFeedback(fService)),
)).Methods("POST", "OPTIONS")

*/
http.Handle("/", r)
logger := log.New(os.Stderr, "logger: ", log.Lshortfile)
srv := &http.Server{
Expand All @@ -62,8 +68,8 @@ func main() {
}
}

func storeFeedback(fService feedback.UseCase) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
func storeFeedback(fService feedback.UseCase) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var f feedback.Feedback
err := json.NewDecoder(r.Body).Decode(&f)
if err != nil {
Expand All @@ -84,5 +90,5 @@ func storeFeedback(fService feedback.UseCase) http.Handler {
return
}
return
})
}
}
15 changes: 7 additions & 8 deletions pkg/middleware/is_authenticated.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package middleware

import (
"context"
"encoding/json"
"errors"
"net/http"
"strconv"
"strings"

"github.com/codegangsta/negroni"
)

func IsAuthenticated() negroni.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
func IsAuthenticated(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
errorMessage := "Erro na autenticação"
tokenString := r.Header.Get("Authorization")
if tokenString == "" {
Expand All @@ -38,13 +37,13 @@ func IsAuthenticated() negroni.HandlerFunc {
respondWithError(rw, http.StatusUnauthorized, err.Error(), errorMessage)
return
}
r.Header.Add("email", res.Email)

next(rw, r)
}
ctx := context.WithValue(r.Context(), "email", res.Email)
next.ServeHTTP(rw, r.WithContext(ctx))
})
}

//RespondWithError return a http error
// RespondWithError return a http error
func respondWithError(w http.ResponseWriter, code int, e string, message string) {
respondWithJSON(w, code, map[string]string{"code": strconv.Itoa(code), "error": e, "message": message})
}
Expand Down

0 comments on commit 79836c7

Please sign in to comment.