From 5cbb3237e0341304a317a2caaabc6abc1234c4b7 Mon Sep 17 00:00:00 2001 From: valitovgaziz Date: Mon, 12 Aug 2024 08:49:04 +0500 Subject: [PATCH] mdidle ware set --- .env | 2 ++ api/cmd/main.go | 2 ++ api/go.mod | 3 +++ api/go.sum | 6 ++++++ api/src/auth/jwt.go | 8 ++++++++ api/src/initializers/initializers.go | 10 ++++++++++ api/src/models/authDataStructs.go | 19 +++++++++++++++++++ api/src/models/user.go | 2 +- docker-compose.yaml | 3 --- 9 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 api/src/auth/jwt.go create mode 100644 api/src/models/authDataStructs.go diff --git a/.env b/.env index 4fd85c3..ba2a0e4 100644 --- a/.env +++ b/.env @@ -8,9 +8,11 @@ PGURL='postgres://postgres:postgres@db:5432/postgres?sslmode=disable' # SERVER SERVER_PORT=8000 +SECRET_KEY=my_very_secret_key # MIGRATOR MIGRATOR_PORT=3000 GOOSE_DRIVER=postgres GOOSE_DBSTRING='user=postgres dbname=postgres sslmode=disable' GOOSE_MIGRATION_DIR=migrations + diff --git a/api/cmd/main.go b/api/cmd/main.go index afb5bf4..daa4793 100644 --- a/api/cmd/main.go +++ b/api/cmd/main.go @@ -4,10 +4,12 @@ import ( "api/src/configs" "api/src/initializers" "log/slog" + "os" ) var APIServerCnf configs.APIserver var PSQLCnf configs.PSQLConfig +var SecretKey = []byte(os.Getenv("SECRET_KEY")) func main() { slog.Info("Start") diff --git a/api/go.mod b/api/go.mod index aea01b5..aa16a40 100644 --- a/api/go.mod +++ b/api/go.mod @@ -5,6 +5,7 @@ go 1.22.5 require github.com/go-chi/chi/v5 v5.1.0 require ( + github.com/golang-jwt/jwt/v5 v5.2.1 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/jackc/pgx/v5 v5.6.0 // indirect @@ -18,6 +19,8 @@ require ( ) require ( + github.com/golang-jwt/jwt v3.2.2+incompatible + github.com/golang-jwt/jwt/v4 v4.5.0 github.com/google/uuid v1.6.0 gorm.io/driver/postgres v1.5.9 gorm.io/gorm v1.25.11 diff --git a/api/go.sum b/api/go.sum index 5537f09..b0bba6f 100644 --- a/api/go.sum +++ b/api/go.sum @@ -3,6 +3,12 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= +github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY= +github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= +github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg= +github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= +github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk= +github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= diff --git a/api/src/auth/jwt.go b/api/src/auth/jwt.go new file mode 100644 index 0000000..15fe43e --- /dev/null +++ b/api/src/auth/jwt.go @@ -0,0 +1,8 @@ +package auth + +import ( +) + +func Register() { + +} \ No newline at end of file diff --git a/api/src/initializers/initializers.go b/api/src/initializers/initializers.go index 5ea2c8a..1ac1884 100644 --- a/api/src/initializers/initializers.go +++ b/api/src/initializers/initializers.go @@ -5,6 +5,7 @@ import ( "fmt" "log/slog" "os" + "time" "gorm.io/driver/postgres" "gorm.io/gorm" @@ -53,7 +54,16 @@ func InitDBconnection() { func InitChiRouting() { slog.Info("Init routing") r := chi.NewRouter() + + // middlewares r.Use(middleware.Logger) + r.Use(middleware.Timeout(60 * time.Second)) + r.Use(middleware.RequestID) + r.Use(middleware.CleanPath) + r.Use(middleware.Heartbeat("/ping")) + r.Use(middleware.NoCache) + r.Use(middleware.Recoverer) + r.Get("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("welcome developer! Cool.")) }) diff --git a/api/src/models/authDataStructs.go b/api/src/models/authDataStructs.go new file mode 100644 index 0000000..aa848ad --- /dev/null +++ b/api/src/models/authDataStructs.go @@ -0,0 +1,19 @@ +package models + +import ( + "os" + + "github.com/golang-jwt/jwt/v4" +) + +var jwtKey = []byte(os.Getenv("SECRET_KEY")) + +type Crenetials struct { + Email string `json:"email"` + Password string `json:"password"` +} + +type Claims struct { + jwt.RegisteredClaims + Email string `json:"email"` +} diff --git a/api/src/models/user.go b/api/src/models/user.go index a50cd2e..598c9ad 100644 --- a/api/src/models/user.go +++ b/api/src/models/user.go @@ -8,4 +8,4 @@ type User struct { Email string `json:"email" gorm:"type:string;index"` Password string `json:"password" gorm:"type:string;index"` Phone string `json:"phone" gorm:"type:string;index"` -} +} \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index 36f7d61..c7ff219 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -38,9 +38,6 @@ services: - db volumes: - goose:/migrations - environment: - - GOOSE_DRIVER=${GOOSE_DRIVER} - - GOOSE_DBSTRING=${GOOSE_DBSTRING} command: goose up volumes: