Skip to content

Commit

Permalink
feat: implementando contexts, SendString, interface e struct
Browse files Browse the repository at this point in the history
  • Loading branch information
victoriuptec committed Jan 30, 2024
1 parent c2af89b commit 7bd661e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 11 deletions.
48 changes: 37 additions & 11 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,27 @@ import (
"github.com/rs/cors"
)

type Controller struct{}
type (
Context interface {
Request() *http.Request
Response() http.ResponseWriter
SendString(status int, s string) error
}

type APIFunc func(context.Context, http.ResponseWriter, *http.Request) error
TupaContext struct {
request *http.Request
response http.ResponseWriter
context.Context
}
)

type APIFunc func(*TupaContext) error

type APIServer struct {
listenAddr string
Router *mux.Router
server *http.Server
}

type HTTPMethod string

type DefaultController struct {
router *mux.Router
}

var (
globalRouter *mux.Router
globalRouterOnce sync.Once
Expand All @@ -47,6 +52,10 @@ const (
MethodOptions HTTPMethod = http.MethodOptions
)

type DefaultController struct {
router *mux.Router
}

var AllowedMethods = map[HTTPMethod]bool{
MethodGet: true,
MethodPost: true,
Expand Down Expand Up @@ -131,9 +140,12 @@ func WriteJSONHelper(w http.ResponseWriter, status int, v any) error {

func (dc *DefaultController) MakeHTTPHandlerFuncHelper(f APIFunc, httpMethod HTTPMethod) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ctx := &TupaContext{
request: r,
response: w,
}
if r.Method == string(httpMethod) {
if err := f(ctx, w, r); err != nil {
if err := f(ctx); err != nil {
if err := WriteJSONHelper(w, http.StatusInternalServerError, APIError{Error: err.Error()}); err != nil {
fmt.Println("Erro ao escrever resposta JSON:", err)
}
Expand All @@ -154,3 +166,17 @@ func getGlobalRouter() *mux.Router {
})
return globalRouter
}

func (tc *TupaContext) Request() *http.Request {
return tc.request
}

func (tc *TupaContext) Response() *http.ResponseWriter {
return &tc.response
}

func (tc *TupaContext) SendString(status int, s string) error {
(tc.response).WriteHeader(status)
_, err := (tc.response).Write([]byte(s))
return err
}
29 changes: 29 additions & 0 deletions controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package tupa

import (
"net/http"
"net/http/httptest"
"testing"
"time"
)

func BenchmarkDirectAccessSendString(b *testing.B) {
req, _ := http.NewRequest(http.MethodGet, "/", nil)
w := httptest.NewRecorder()
ctx := &TupaContext{
request: req,
response: w,
}

start := time.Now()

b.ResetTimer()

for i := 0; i < b.N; i++ {
ctx.SendString(http.StatusOK, "Hello, World!")
}

elapsed := time.Since(start)
opsPerSec := float64(b.N) / elapsed.Seconds()
b.ReportMetric(opsPerSec, "ops/sec")
}
2 changes: 2 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ package tupa
type APIError struct {
Error string
}

type HTTPMethod string

0 comments on commit 7bd661e

Please sign in to comment.