-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improv: removed resource signature from sub functions
rely con context DI instead
- Loading branch information
1 parent
82dee9c
commit 8d22aa8
Showing
18 changed files
with
301 additions
and
257 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,23 @@ | ||
package infra | ||
|
||
import ( | ||
"database/sql" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type Resources struct { | ||
DBConn *sql.DB | ||
Logger *zap.Logger | ||
} | ||
|
||
func SetupResources(config Config) Resources { | ||
dbConn := SetupConnection(config.PostgresURI) | ||
logger, err := zap.NewProduction() | ||
if err != nil { | ||
panic(err) | ||
} | ||
return Resources{ | ||
DBConn: dbConn, | ||
Logger: logger, | ||
} | ||
} | ||
package infra | ||
|
||
import ( | ||
"database/sql" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type Resources struct { | ||
DBConn *sql.DB | ||
Logger *zap.Logger | ||
} | ||
|
||
func SetupResources(config Config) Resources { | ||
dbConn := SetupConnection(config.PostgresURI) | ||
logger, err := zap.NewProduction() | ||
if err != nil { | ||
panic(err) | ||
} | ||
return Resources{ | ||
DBConn: dbConn, | ||
Logger: logger, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,5 @@ | ||
package middlewares | ||
|
||
type UserIdContextKey struct{} | ||
type ConfigContextKey struct{} | ||
type StorageContextKey struct{} | ||
type CacheContextKey struct{} | ||
type RequestIdContextKey struct{} | ||
type LoggerContextKey struct{} | ||
package middlewares | ||
|
||
type UserIdContextKey struct{} | ||
type DatabaseContextKey struct{} | ||
type LoggerContextKey struct{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,59 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"monify/internal/middlewares" | ||
monify "monify/protobuf/gen/go" | ||
|
||
"github.com/google/uuid" | ||
"go.uber.org/zap" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
func get_userID(ctx context.Context, db *sql.DB, refreshToken string) (uuid.UUID, error) { | ||
|
||
query, err := db.QueryContext(ctx, ` | ||
SELECT user_id | ||
FROM user_identity | ||
WHERE refresh_token= $1`, refreshToken) | ||
defer query.Close() | ||
if err != nil { | ||
return uuid.Nil, err | ||
} | ||
if !query.Next() { | ||
return uuid.Nil, nil | ||
} | ||
var userId uuid.UUID | ||
err = query.Scan(&userId) | ||
if err != nil { | ||
return uuid.Nil, err | ||
} | ||
return userId, nil | ||
|
||
} | ||
|
||
func (s Service) RefreshToken(ctx context.Context, req *monify.RefreshTokenRequest) (*monify.RefreshTokenResponse, error) { | ||
db := ctx.Value(middlewares.StorageContextKey{}).(*sql.DB) | ||
logger := ctx.Value(middlewares.LoggerContextKey{}).(*zap.Logger) | ||
userId, err := get_userID(ctx, db, req.RefreshToken) | ||
|
||
//它給我refresh token,我要給refresh token + access token | ||
refreshToken, err := generateAndInsertRefreshToken(ctx, userId, db) | ||
if err != nil { | ||
logger.Error("failed to get refresh token", zap.Error(err)) | ||
return nil, status.Error(codes.Internal, "internal err.") | ||
} | ||
accessToken, err := GenerateAccessToken(ctx, userId, s.Secret) | ||
|
||
if err != nil { | ||
logger.Error("failed to get access token", zap.Error(err)) | ||
return nil, status.Error(codes.Internal, "internal err.") | ||
|
||
} | ||
return &monify.RefreshTokenResponse{ | ||
AccessToken: accessToken, | ||
RefreshToken: refreshToken, | ||
}, nil | ||
} | ||
package auth | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"monify/internal/middlewares" | ||
monify "monify/protobuf/gen/go" | ||
|
||
"github.com/google/uuid" | ||
"go.uber.org/zap" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
func get_userID(ctx context.Context, refreshToken string) (uuid.UUID, error) { | ||
db := ctx.Value(middlewares.DatabaseContextKey{}).(*sql.DB) | ||
query, err := db.QueryContext(ctx, ` | ||
SELECT user_id | ||
FROM user_identity | ||
WHERE refresh_token= $1`, refreshToken) | ||
defer query.Close() | ||
if err != nil { | ||
return uuid.Nil, err | ||
} | ||
if !query.Next() { | ||
return uuid.Nil, nil | ||
} | ||
var userId uuid.UUID | ||
err = query.Scan(&userId) | ||
if err != nil { | ||
return uuid.Nil, err | ||
} | ||
return userId, nil | ||
|
||
} | ||
|
||
func (s Service) RefreshToken(ctx context.Context, req *monify.RefreshTokenRequest) (*monify.RefreshTokenResponse, error) { | ||
|
||
logger := ctx.Value(middlewares.LoggerContextKey{}).(*zap.Logger) | ||
userId, err := get_userID(ctx, req.RefreshToken) | ||
|
||
//它給我refresh token,我要給refresh token + access token | ||
refreshToken, err := generateAndInsertRefreshToken(ctx, userId) | ||
if err != nil { | ||
logger.Error("failed to get refresh token", zap.Error(err)) | ||
return nil, status.Error(codes.Internal, "internal err.") | ||
} | ||
accessToken, err := GenerateAccessToken(ctx, userId, s.Secret) | ||
|
||
if err != nil { | ||
logger.Error("failed to get access token", zap.Error(err)) | ||
return nil, status.Error(codes.Internal, "internal err.") | ||
|
||
} | ||
return &monify.RefreshTokenResponse{ | ||
AccessToken: accessToken, | ||
RefreshToken: refreshToken, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.