Skip to content

Commit

Permalink
feature: image_service
Browse files Browse the repository at this point in the history
  • Loading branch information
brian030128 committed Jun 17, 2024
1 parent 762d94b commit ff43bfc
Show file tree
Hide file tree
Showing 35 changed files with 2,267 additions and 496 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
)

require (
github.com/aws/aws-sdk-go v1.54.2 // indirect
github.com/cockroachdb/cockroach-go/v2 v2.1.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
Expand All @@ -24,6 +25,7 @@ require (
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/klauspost/compress v1.15.11 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/pierrec/lz4/v4 v4.1.16 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg6
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
github.com/aws/aws-sdk-go v1.54.2 h1:Wo6AVWcleNHrYa48YzfYz60hzxGRqsJrK5s/qePe+3I=
github.com/aws/aws-sdk-go v1.54.2/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU=
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
github.com/cockroachdb/cockroach-go/v2 v2.1.1 h1:3XzfSMuUT0wBe1a3o5C0eOTcArhmmFAg2Jzh/7hhKqo=
github.com/cockroachdb/cockroach-go/v2 v2.1.1/go.mod h1:7NtUnP6eK+l6k483WSYNrq3Kb23bWV10IRV1TyeSpwM=
Expand Down Expand Up @@ -93,6 +95,9 @@ github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/jmoiron/sqlx v1.3.1/go.mod h1:2BljVx/86SuTyjE+aPYlHCTNvZrnJXghYGpNiXLBMCQ=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
Expand Down Expand Up @@ -304,6 +309,7 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EV
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
10 changes: 10 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHG
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/apache/arrow/go/v10 v10.0.1/go.mod h1:YvhnlEePVnBS4+0z3fhPfUy7W1Ikj0Ih0vcRo/gZ1M0=
github.com/apache/thrift v0.16.0/go.mod h1:PHK3hniurgQaNMZYaCLEqXKsYK8upmhPbmdP2FXSqgU=
github.com/aws/aws-sdk-go v1.49.6 h1:yNldzF5kzLBRvKlKz1S0bkvc2+04R1kt13KfBWQBfFA=
github.com/aws/aws-sdk-go v1.49.6/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
github.com/aws/aws-sdk-go-v2 v1.16.16/go.mod h1:SwiyXi/1zTUZ6KIAmLK5V5ll8SiURNUYOqTerZPaF9k=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.8/go.mod h1:JTnlBSot91steJeti4ryyu/tLd4Sk84O5W22L7O2EQU=
Expand Down Expand Up @@ -70,12 +71,21 @@ github.com/gorilla/handlers v1.4.2/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/
github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c/go.mod h1:NMPJylDgVpX0MLRlPy15sqSwOFv/U1GZ2m21JhFfek0=
github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed/go.mod h1:tMWxXQ9wFIaZeTI9F+hmhFiGpFmhOHzyShyFUhRm0H4=
github.com/jackc/chunkreader v1.0.0 h1:4s39bBR8ByfqH+DKm8rQA3E1LHZWB9XWcrz8fqaZbe0=
github.com/jackc/chunkreader/v2 v2.0.1 h1:i+RDz65UE+mmpjTfyz0MoVTnzeYxroil2G82ki7MGG8=
github.com/jackc/pgconn v1.14.3 h1:bVoTr12EGANZz66nZPkMInAV/KHD2TxH9npjXXgiB3w=
github.com/jackc/pgconn v1.14.3/go.mod h1:RZbme4uasqzybK2RK5c65VsHxoyaml09lx3tXOcO/VM=
github.com/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa/go.mod h1:a/s9Lp5W7n/DD0VrVoyJ00FbP2ytTPDVOivvn2bMlds=
github.com/jackc/pgio v1.0.0 h1:g12B9UwVnzGhueNavwioyEEpAmqMe1E/BN9ES+8ovkE=
github.com/jackc/pgproto3 v1.1.0 h1:FYYE4yRw+AgI8wXIinMlNjBbp/UitDJwfj5LqqewP1A=
github.com/jackc/pgproto3/v2 v2.3.3 h1:1HLSx5H+tXR9pW3in3zaztoEwQYRC9SQaYUHjTSUOag=
github.com/jackc/pgproto3/v2 v2.3.3/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA=
github.com/jackc/pgtype v1.14.0 h1:y+xUdabmyMkJLyApYuPj38mW+aAIqCe5uuBB51rH3Vw=
github.com/jackc/pgtype v1.14.0/go.mod h1:LUMuVrfsFfdKGLw+AFFVv6KtHOFMwRgDDzBt76IqCA4=
github.com/jackc/pgx/v4 v4.18.2 h1:xVpYkNR5pk5bMCZGfClbO962UIqVABcAGt7ha1s/FeU=
github.com/jackc/pgx/v4 v4.18.2/go.mod h1:Ey4Oru5tH5sB6tV7hDmfWFahwF15Eb7DNXlRKx2CkVw=
github.com/jackc/puddle v1.1.3 h1:JnPg/5Q9xVJGfjsO5CPUOjnJps1JaRUm8I9FXVCFK94=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/k0kubun/pp v2.3.0+incompatible/go.mod h1:GWse8YhT0p8pT4ir3ZgBbfZild3tgzSScAn6HmfYukg=
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0/go.mod h1:1NbS8ALrpOvjt0rHPNLyCIeMtbizbir8U//inJ+zuB8=
Expand Down
77 changes: 77 additions & 0 deletions lib/auth/auth_middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package auth

import (
"context"
"errors"
"github.com/golang-jwt/jwt/v5"
"github.com/google/uuid"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"monify/lib"
"net/http"
"strings"
)

type AuthMiddleware struct {
JwtSecret string
}

func (m AuthMiddleware) HttpMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
auth := r.Header.Get("Authorization")
if auth != "" {
token, err := validateBearerToken(auth, m.JwtSecret)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
ctx := context.WithValue(r.Context(), lib.UserIdContextKey{}, token)
r = r.WithContext(ctx)
}
next.ServeHTTP(w, r)
})
}

func (m AuthMiddleware) GrpcExtractUserId(ctx context.Context, req any, info *grpc.UnaryServerInfo) (uuid.UUID, error) {
md, exists := metadata.FromIncomingContext(ctx)
if !exists {
return uuid.Nil, nil
}
auths := md.Get("authorization")
if len(auths) == 0 {
return uuid.Nil, nil
}
auth := auths[0]
token, err := validateBearerToken(auth, m.JwtSecret)
if err != nil {
return uuid.Nil, status.Error(codes.Unauthenticated, err.Error())
}
return token, nil

}

func validateBearerToken(token string, secret string) (uuid.UUID, error) {
if strings.HasPrefix(token, "Bearer ") {
tokenStr := token[7:]
token, err := jwt.ParseWithClaims(tokenStr, &jwt.RegisteredClaims{}, func(token *jwt.Token) (interface{}, error) {
return []byte(secret), nil
})
if claims, ok := token.Claims.(*jwt.RegisteredClaims); ok {
userId, err := uuid.Parse(claims.Subject)
if err != nil {
return uuid.Nil, errors.New("invalid user id in token")
}
return userId, nil
} else {
switch err {
case nil:
return uuid.Nil, errors.New("invalid token")
default:
return uuid.Nil, err
}
}
}
return uuid.Nil, errors.New("invalid token")
}
2 changes: 2 additions & 0 deletions lib/context_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ type UserIdContextKey struct{}
type DatabaseContextKey struct{}
type LoggerContextKey struct{}
type KafkaWriterContextKey struct{}
type ConfigContextKey struct{}
type ImageStorageContextKey struct{}
10 changes: 10 additions & 0 deletions lib/media/image_storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package media

type ImageStorage interface {
// Store
// Stores the image in the storage and returns the URL
// if image id is empty, a random one will be generated
Store(fileSuffix string, imageData []byte, imageId string) (string, error)

Delete(path string) error
}
20 changes: 20 additions & 0 deletions lib/media/media_usage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package media

type Usage int

const (
Undefined Usage = iota
UserAvatar
GroupAvatar
)

func Parse(str string) Usage {
switch str {
case "userAvatar":
return UserAvatar
case "groupAvatar":
return GroupAvatar
default:
return Undefined
}
}
23 changes: 23 additions & 0 deletions lib/media/tmp_image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package media

import (
"github.com/google/uuid"
"time"
)

type TmpImage struct {
Id uuid.UUID
ExpectedUsage Usage
Uploader uuid.UUID
UploadedAt time.Time
URL string
}

type ConfirmedImage struct {
Id uuid.UUID
Usage Usage
Uploader uuid.UUID
UploadedAt time.Time
URL string
ConfirmedAt time.Time
}
2 changes: 2 additions & 0 deletions migrations/2406170339_create_image_table.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP TABLE TmpImage;
DROP TABLE ConfirmedImage;
16 changes: 16 additions & 0 deletions migrations/2406170339_create_image_table.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CREATE TABLE TmpImage(
imgId uuid PRIMARY KEY,
url varchar(100) NOT NULL ,
expected_usage int8 NOT NULL ,
uploader uuid references user_identity(user_id) NOT NULL,
uploaded_at timestamp NOT NULL
);

CREATE TABLE ConfirmedImage(
imgId uuid PRIMARY KEY,
url varchar(100) NOT NULL ,
usage int8 NOT NULL ,
uploader uuid references user_identity(user_id) NOT NULL,
uploaded_at timestamp NOT NULL,
confirmed_at timestamp NOT NULL
)
2 changes: 1 addition & 1 deletion migrations/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func main() {
_ = godotenv.Load()
secrets, err := utils.LoadSecrets(utils.LoadEnv())
if err != nil {
panic(secrets)
panic(err)
}

db, err := sql.Open("pgx", secrets["POSTGRES_URI"])
Expand Down
6 changes: 3 additions & 3 deletions protobuf/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
MODULES = $(wildcard *.proto)

SWAGGER_MODULES = group.proto user.proto auth.proto group_bill.proto
O_DIR = gen/go
SWAGGER_DIR = gen
OUTPUT = $(MODULES:%.proto=$(O_DIR)/%.pb.go)
Expand All @@ -12,12 +12,12 @@ all: $(OUTPUT) $(SWAGGER_DIR)/monify.swagger.json



$(SWAGGER_DIR)/monify.swagger.json: $(MODULES)
$(SWAGGER_DIR)/monify.swagger.json:
-mkdir $(SWAGGER_DIR)
protoc -I . --openapiv2_out $(SWAGGER_DIR) --openapiv2_opt=preserve_rpc_order=true \
--openapiv2_opt=allow_merge=true \
--openapiv2_opt=disable_default_errors=true \
--openapiv2_opt=merge_file_name=monify $(MODULES)
--openapiv2_opt=merge_file_name=monify $(SWAGGER_MODULES)

$(O_DIR)/%.pb.go: %.proto $(O_DIR)
protoc --go_out=./gen/go --go_opt=paths=source_relative $<
Expand Down
28 changes: 14 additions & 14 deletions protobuf/gen/go/auth.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ff43bfc

Please sign in to comment.