feat: domain finished

This commit is contained in:
Elton Minetto
2020-06-25 11:46:45 -03:00
commit 3db4550ee0
47 changed files with 2646 additions and 0 deletions
+17
View File
@@ -0,0 +1,17 @@
package middleware
import (
"net/http"
)
//Cors adiciona os headers para suportar o CORS nos navegadores
func Cors(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Accept, Authorization, Content-Type")
w.Header().Set("Content-Type", "application/json")
if r.Method == "OPTIONS" {
return
}
next(w, r)
}
+23
View File
@@ -0,0 +1,23 @@
package middleware
import (
"net/http"
"strconv"
"github.com/eminetto/clean-architecture-go/pkg/metric"
"github.com/codegangsta/negroni"
)
//Metrics to prometheus
func Metrics(mService metric.UseCase) negroni.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
appMetric := metric.NewHTTP(r.URL.Path, r.Method)
appMetric.Started()
next(w, r)
res := w.(negroni.ResponseWriter)
appMetric.Finished()
appMetric.StatusCode = strconv.Itoa(res.Status())
mService.SaveHTTP(appMetric)
}
}
+29
View File
@@ -0,0 +1,29 @@
package middleware
import (
"context"
"encoding/json"
"github.com/eminetto/clean-architecture-go/pkg/entity"
"net/http"
"github.com/codegangsta/negroni"
)
//Validate
func Validate(e entity.Validable) negroni.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
err := json.NewDecoder(r.Body).Decode(&e)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
err = e.Validate()
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
ctx := context.WithValue(r.Context(), "InputParam", e)
next(w, r.WithContext(ctx))
}
}