dinheiro

commit a20f3b4bd0470befe7022618a61865815cba9ac1

Author: Pedro Lucas Porcellis <porcellis@eletrotupi.com>

db: flesh out a very barebones database middleware

 db/middleware.go | 37 +++++++++++++++++++++++++++++++++++++
 go.mod | 2 ++
 go.sum | 10 ++++++++++
 types/user.go | 14 ++++++++++++++


diff --git a/db/middleware.go b/db/middleware.go
new file mode 100644
index 0000000000000000000000000000000000000000..31f677a87acdb9892567144c94c68924e2d5fd19
--- /dev/null
+++ b/db/middleware.go
@@ -0,0 +1,37 @@
+package db
+
+import (
+	"context"
+	"database/sql"
+	"errors"
+	"net/http"
+)
+
+var dbCtxKey = &contextKey{"db"}
+
+type contextKey struct {
+	name string
+}
+
+func Middleware(db *sql.DB) func(http.Handler) http.Handler {
+	return func(next http.Handler) http.Handler {
+		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+			r = r.WithContext(Context(r.Context(), db))
+			next.ServeHTTP(w, r)
+		})
+	}
+}
+
+func Context(ctx context.Context, db *sql.DB) context.Context {
+	return context.WithValue(ctx, dbCtxKey, db)
+}
+
+func ForContext(ctx context.Context) (*sql.DB) {
+	raw, ok := ctx.Value(dbCtxKey).(*sql.DB)
+
+	if !ok {
+		panic(errors.New("Invalid db context"))
+	}
+
+	return raw
+}




diff --git a/go.mod b/go.mod
index 2b957585277f6e6bf9ba59bea5c68ce100f5f5b3..c035ff8fb6192a29add238c08eb04b9cc6030af4 100644
--- a/go.mod
+++ b/go.mod
@@ -3,8 +3,10 @@
 go 1.16
 
 require (
+	github.com/Masterminds/squirrel v1.5.0 // indirect
 	github.com/fernet/fernet-go v0.0.0-20191111064656-eff2850e6001 // indirect
 	github.com/go-chi/chi/v5 v5.0.3
 	github.com/go-redis/redis/v8 v8.9.0 // indirect
+	github.com/lib/pq v1.10.2 // indirect
 	github.com/vaughan0/go-ini v0.0.0-20130923145212-a98ad7ee00ec // indirect
 )




diff --git a/go.sum b/go.sum
index c4471760036e080c1d6476ea5973333495fbe08a..1845be0f720dd971af642d44c2f261d8fe593839 100644
--- a/go.sum
+++ b/go.sum
@@ -1,6 +1,9 @@
+github.com/Masterminds/squirrel v1.5.0 h1:JukIZisrUXadA9pl3rMkjhiamxiB0cXiu+HGp/Y8cY8=
+github.com/Masterminds/squirrel v1.5.0/go.mod h1:NNaOrjSoIDfDA40n7sr2tPNZRfjzjA400rg+riTZj10=
 github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
 github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
 github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
 github.com/fernet/fernet-go v0.0.0-20191111064656-eff2850e6001 h1:/UMxx5lGDg30aioUL9e7xJnbJfJeX7vhcm57fa5udaI=
@@ -23,6 +26,12 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
 github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
 github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
 github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
+github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 h1:SOEGU9fKiNWd/HOJuq6+3iTQz8KNCLtVX6idSoTLdUw=
+github.com/lann/builder v0.0.0-20180802200727-47ae307949d0/go.mod h1:dXGbAdH5GtBTC4WfIxhKZfyBF/HBFgRZSWwZ9g/He9o=
+github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 h1:P6pPBnrTSX3DEVR4fDembhRWSsG5rVo6hYhAB/ADZrk=
+github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0/go.mod h1:vmVJ0l/dxyfGW6FmdpVm2joNMFikkuWg0EoCKLGUMNw=
+github.com/lib/pq v1.10.2 h1:AqzbZs4ZoCBp+GtejcpCpcxM3zlSMx29dXbUSeVtJb8=
+github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
 github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
 github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
 github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
@@ -32,6 +41,7 @@ github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
 github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7mt48=
 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=
+github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
 github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
 github.com/vaughan0/go-ini v0.0.0-20130923145212-a98ad7ee00ec h1:DGmKwyZwEB8dI7tbLt/I/gQuP559o/0FrAkHKlQM/Ks=
 github.com/vaughan0/go-ini v0.0.0-20130923145212-a98ad7ee00ec/go.mod h1:owBmyHYMLkxyrugmfwE/DLJyW8Ro9mkphwuVErQ0iUw=




diff --git a/types/user.go b/types/user.go
new file mode 100644
index 0000000000000000000000000000000000000000..d0875c4528ba4e4ca9bbd1d19c529169072971d1
--- /dev/null
+++ b/types/user.go
@@ -0,0 +1,14 @@
+package types
+
+import (
+	"time"
+)
+
+type User struct {
+	ID int
+	FirstName string
+	LastName string
+	Username string
+	CreatedAt time.Time
+	UpdatedAt time.Time
+}