diff options
author | Mike Crute <mike@crute.us> | 2023-08-03 13:48:28 -0700 |
---|---|---|
committer | Mike Crute <mike@crute.us> | 2023-08-03 13:48:28 -0700 |
commit | a09b5eb2a787fc6671ea1d2452452b1e58df6a96 (patch) | |
tree | 5dfd67d648363895b365003262cb92025561b59d | |
parent | 08c9439879329c653c2fabd7c8c9bf7f4c145d66 (diff) | |
download | websocket_proxy-a09b5eb2a787fc6671ea1d2452452b1e58df6a96.tar.bz2 websocket_proxy-a09b5eb2a787fc6671ea1d2452452b1e58df6a96.tar.xz websocket_proxy-a09b5eb2a787fc6671ea1d2452452b1e58df6a96.zip |
Migrate MongoDbBasicStore to golib version
-rw-r--r-- | app/models/auth_session_mongodb.go | 4 | ||||
-rw-r--r-- | cmd/register/register.go | 5 | ||||
-rw-r--r-- | cmd/web/server.go | 7 | ||||
-rw-r--r-- | db/mongodb_store.go | 72 | ||||
-rw-r--r-- | go.mod | 2 | ||||
-rw-r--r-- | go.sum | 4 |
6 files changed, 10 insertions, 84 deletions
diff --git a/app/models/auth_session_mongodb.go b/app/models/auth_session_mongodb.go index fc5f5dd..a5f0537 100644 --- a/app/models/auth_session_mongodb.go +++ b/app/models/auth_session_mongodb.go | |||
@@ -4,14 +4,14 @@ import ( | |||
4 | "context" | 4 | "context" |
5 | "time" | 5 | "time" |
6 | 6 | ||
7 | "code.crute.us/mcrute/ssh-proxy/db" | 7 | "code.crute.us/mcrute/golib/db/mongodb/v2" |
8 | 8 | ||
9 | "go.mongodb.org/mongo-driver/bson" | 9 | "go.mongodb.org/mongo-driver/bson" |
10 | "go.mongodb.org/mongo-driver/bson/primitive" | 10 | "go.mongodb.org/mongo-driver/bson/primitive" |
11 | ) | 11 | ) |
12 | 12 | ||
13 | type AuthSessionStoreMongodb struct { | 13 | type AuthSessionStoreMongodb struct { |
14 | *db.MongoDbBasicStore[*AuthSession] | 14 | *mongodb.MongoDbBasicStore[*AuthSession] |
15 | } | 15 | } |
16 | 16 | ||
17 | var _ AuthSessionStore = (*AuthSessionStoreMongodb)(nil) | 17 | var _ AuthSessionStore = (*AuthSessionStoreMongodb)(nil) |
diff --git a/cmd/register/register.go b/cmd/register/register.go index fdd083c..c00b54b 100644 --- a/cmd/register/register.go +++ b/cmd/register/register.go | |||
@@ -10,7 +10,6 @@ import ( | |||
10 | glecho "code.crute.us/mcrute/golib/echo" | 10 | glecho "code.crute.us/mcrute/golib/echo" |
11 | "code.crute.us/mcrute/ssh-proxy/app" | 11 | "code.crute.us/mcrute/ssh-proxy/app" |
12 | "code.crute.us/mcrute/ssh-proxy/app/models" | 12 | "code.crute.us/mcrute/ssh-proxy/app/models" |
13 | "code.crute.us/mcrute/ssh-proxy/db" | ||
14 | "github.com/spf13/cobra" | 13 | "github.com/spf13/cobra" |
15 | ) | 14 | ) |
16 | 15 | ||
@@ -43,13 +42,13 @@ func registerMain(cfg app.Config, username string) { | |||
43 | log.Fatalf("Error connecting to mongodb: %s", err) | 42 | log.Fatalf("Error connecting to mongodb: %s", err) |
44 | } | 43 | } |
45 | 44 | ||
46 | userStore := &db.MongoDbBasicStore[*models.User]{ | 45 | userStore := &mongodb.MongoDbBasicStore[*models.User]{ |
47 | Db: mongo, | 46 | Db: mongo, |
48 | CollectionName: "users", | 47 | CollectionName: "users", |
49 | } | 48 | } |
50 | 49 | ||
51 | authSessionStore := &models.AuthSessionStoreMongodb{ | 50 | authSessionStore := &models.AuthSessionStoreMongodb{ |
52 | MongoDbBasicStore: &db.MongoDbBasicStore[*models.AuthSession]{ | 51 | MongoDbBasicStore: &mongodb.MongoDbBasicStore[*models.AuthSession]{ |
53 | Db: mongo, | 52 | Db: mongo, |
54 | CollectionName: "oauth_sessions", | 53 | CollectionName: "oauth_sessions", |
55 | }, | 54 | }, |
diff --git a/cmd/web/server.go b/cmd/web/server.go index 83f4fc8..19c1bb7 100644 --- a/cmd/web/server.go +++ b/cmd/web/server.go | |||
@@ -14,7 +14,6 @@ import ( | |||
14 | "code.crute.us/mcrute/ssh-proxy/app/controllers" | 14 | "code.crute.us/mcrute/ssh-proxy/app/controllers" |
15 | "code.crute.us/mcrute/ssh-proxy/app/middleware" | 15 | "code.crute.us/mcrute/ssh-proxy/app/middleware" |
16 | "code.crute.us/mcrute/ssh-proxy/app/models" | 16 | "code.crute.us/mcrute/ssh-proxy/app/models" |
17 | "code.crute.us/mcrute/ssh-proxy/db" | ||
18 | 17 | ||
19 | "code.crute.us/mcrute/golib/cli" | 18 | "code.crute.us/mcrute/golib/cli" |
20 | "code.crute.us/mcrute/golib/clients/autocert/v2" | 19 | "code.crute.us/mcrute/golib/clients/autocert/v2" |
@@ -127,18 +126,18 @@ func webMain(cfg app.Config, embeddedTemplates, embeddedClients fs.FS, appVersio | |||
127 | log.Fatalf("Error creating session store: %s", err) | 126 | log.Fatalf("Error creating session store: %s", err) |
128 | } | 127 | } |
129 | 128 | ||
130 | userStore := &db.MongoDbBasicStore[*models.User]{ | 129 | userStore := &mongodb.MongoDbBasicStore[*models.User]{ |
131 | Db: mongo, | 130 | Db: mongo, |
132 | CollectionName: "users", | 131 | CollectionName: "users", |
133 | } | 132 | } |
134 | 133 | ||
135 | oauthClientStore := &db.MongoDbBasicStore[*models.OauthClient]{ | 134 | oauthClientStore := &mongodb.MongoDbBasicStore[*models.OauthClient]{ |
136 | Db: mongo, | 135 | Db: mongo, |
137 | CollectionName: "oauth_clients", | 136 | CollectionName: "oauth_clients", |
138 | } | 137 | } |
139 | 138 | ||
140 | authSessionStore := &models.AuthSessionStoreMongodb{ | 139 | authSessionStore := &models.AuthSessionStoreMongodb{ |
141 | MongoDbBasicStore: &db.MongoDbBasicStore[*models.AuthSession]{ | 140 | MongoDbBasicStore: &mongodb.MongoDbBasicStore[*models.AuthSession]{ |
142 | Db: mongo, | 141 | Db: mongo, |
143 | CollectionName: "oauth_sessions", | 142 | CollectionName: "oauth_sessions", |
144 | }, | 143 | }, |
diff --git a/db/mongodb_store.go b/db/mongodb_store.go deleted file mode 100644 index 2822de4..0000000 --- a/db/mongodb_store.go +++ /dev/null | |||
@@ -1,72 +0,0 @@ | |||
1 | package db | ||
2 | |||
3 | import ( | ||
4 | "context" | ||
5 | "time" | ||
6 | |||
7 | "code.crute.us/mcrute/golib/db/mongodb/v2" | ||
8 | "go.mongodb.org/mongo-driver/bson" | ||
9 | "go.mongodb.org/mongo-driver/bson/primitive" | ||
10 | ) | ||
11 | |||
12 | type MongoDbBasicModel interface { | ||
13 | MarkDeleted(time.Time) | ||
14 | RecordId() string | ||
15 | } | ||
16 | |||
17 | type MongoDbBasicStore[T MongoDbBasicModel] struct { | ||
18 | Db *mongodb.Mongo | ||
19 | CollectionName string | ||
20 | } | ||
21 | |||
22 | func (s *MongoDbBasicStore[T]) List(ctx context.Context) ([]T, error) { | ||
23 | out := []T{} | ||
24 | |||
25 | filter := bson.M{} | ||
26 | filter["deleted"] = primitive.Null{} | ||
27 | |||
28 | if err := s.Db.FindAllByFilter(ctx, s.CollectionName, filter, &out); err != nil { | ||
29 | return nil, err | ||
30 | } | ||
31 | |||
32 | return out, nil | ||
33 | } | ||
34 | |||
35 | func (s *MongoDbBasicStore[T]) ListAll(ctx context.Context) ([]T, error) { | ||
36 | out := []T{} | ||
37 | |||
38 | if err := s.Db.FindAllByFilter(ctx, s.CollectionName, bson.M{}, &out); err != nil { | ||
39 | return nil, err | ||
40 | } | ||
41 | |||
42 | return out, nil | ||
43 | } | ||
44 | |||
45 | func (s *MongoDbBasicStore[T]) Get(ctx context.Context, name string) (T, error) { | ||
46 | var out T | ||
47 | |||
48 | filter := bson.M{} | ||
49 | filter["_id"] = name | ||
50 | |||
51 | if err := s.Db.FindOneByFilter(ctx, s.CollectionName, filter, &out); err != nil { | ||
52 | return out, err | ||
53 | } | ||
54 | |||
55 | return out, nil | ||
56 | } | ||
57 | |||
58 | func (s *MongoDbBasicStore[T]) Upsert(ctx context.Context, m T) error { | ||
59 | if err := s.Db.ReplaceOneById(ctx, s.CollectionName, m.RecordId(), m); err != nil { | ||
60 | return err | ||
61 | } | ||
62 | return nil | ||
63 | } | ||
64 | |||
65 | func (s *MongoDbBasicStore[T]) Delete(ctx context.Context, m T) error { | ||
66 | k, err := s.Get(ctx, m.RecordId()) | ||
67 | if err != nil { | ||
68 | return err | ||
69 | } | ||
70 | k.MarkDeleted(time.Now()) | ||
71 | return s.Upsert(ctx, k) | ||
72 | } | ||
@@ -6,7 +6,7 @@ require ( | |||
6 | code.crute.us/mcrute/golib/cli v0.2.2 | 6 | code.crute.us/mcrute/golib/cli v0.2.2 |
7 | code.crute.us/mcrute/golib/clients/autocert/v2 v2.2.0 | 7 | code.crute.us/mcrute/golib/clients/autocert/v2 v2.2.0 |
8 | code.crute.us/mcrute/golib/clients/netbox/v3 v3.2.0 | 8 | code.crute.us/mcrute/golib/clients/netbox/v3 v3.2.0 |
9 | code.crute.us/mcrute/golib/db/mongodb/v2 v2.0.0 | 9 | code.crute.us/mcrute/golib/db/mongodb/v2 v2.1.0 |
10 | code.crute.us/mcrute/golib/echo v0.9.4 | 10 | code.crute.us/mcrute/golib/echo v0.9.4 |
11 | code.crute.us/mcrute/golib/secrets v0.4.0 | 11 | code.crute.us/mcrute/golib/secrets v0.4.0 |
12 | github.com/go-webauthn/webauthn v0.8.4 | 12 | github.com/go-webauthn/webauthn v0.8.4 |
@@ -42,8 +42,8 @@ code.crute.us/mcrute/golib/clients/netbox v0.1.0 h1:7ae676WtINm2oSLoUw1ERIZ2ndLD | |||
42 | code.crute.us/mcrute/golib/clients/netbox v0.1.0/go.mod h1:csRsnmAwenAz8Pbo7CcQTWzn6uaXadELIdB81JxsacY= | 42 | code.crute.us/mcrute/golib/clients/netbox v0.1.0/go.mod h1:csRsnmAwenAz8Pbo7CcQTWzn6uaXadELIdB81JxsacY= |
43 | code.crute.us/mcrute/golib/clients/netbox/v3 v3.2.0 h1:g3+vdaUeoVg+Qx++bGi2j2AVBOFpRC6b+uTGw6oM9wc= | 43 | code.crute.us/mcrute/golib/clients/netbox/v3 v3.2.0 h1:g3+vdaUeoVg+Qx++bGi2j2AVBOFpRC6b+uTGw6oM9wc= |
44 | code.crute.us/mcrute/golib/clients/netbox/v3 v3.2.0/go.mod h1:Wjh2iiFDkRuYN1TBgYrRpwT5Xp8i+2+TWJrbdG3JWWk= | 44 | code.crute.us/mcrute/golib/clients/netbox/v3 v3.2.0/go.mod h1:Wjh2iiFDkRuYN1TBgYrRpwT5Xp8i+2+TWJrbdG3JWWk= |
45 | code.crute.us/mcrute/golib/db/mongodb/v2 v2.0.0 h1:v4AYsbesoDeAMMbwS43WzqywNm0w09yKs6731DrdWVY= | 45 | code.crute.us/mcrute/golib/db/mongodb/v2 v2.1.0 h1:Qw5g1R6vaJluHJoufNj+tjdLoHH0Z6TrNKDLK3h1hbE= |
46 | code.crute.us/mcrute/golib/db/mongodb/v2 v2.0.0/go.mod h1:3dFJwm2MtCb312eHdHnK/w8D1lwgCeewa/2hztw89kE= | 46 | code.crute.us/mcrute/golib/db/mongodb/v2 v2.1.0/go.mod h1:xoML+iNm/O3OgRJ5Uaie+SAEvczR/LrvCMsZm6MWiPI= |
47 | code.crute.us/mcrute/golib/echo v0.9.4 h1:C2z7RRIksT2K0Fr4LZHGISRMXooNT1qaEV9NZ7LK5fs= | 47 | code.crute.us/mcrute/golib/echo v0.9.4 h1:C2z7RRIksT2K0Fr4LZHGISRMXooNT1qaEV9NZ7LK5fs= |
48 | code.crute.us/mcrute/golib/echo v0.9.4/go.mod h1:mcmhqsSWD/+ECdrd0Sh9u9XGtukXdLPVHc88sKg/gJo= | 48 | code.crute.us/mcrute/golib/echo v0.9.4/go.mod h1:mcmhqsSWD/+ECdrd0Sh9u9XGtukXdLPVHc88sKg/gJo= |
49 | code.crute.us/mcrute/golib/secrets v0.4.0 h1:tZzQEOnJshDGuzvvr0n0BMWZbu3ZMB5QRqIDa4PwE3U= | 49 | code.crute.us/mcrute/golib/secrets v0.4.0 h1:tZzQEOnJshDGuzvvr0n0BMWZbu3ZMB5QRqIDa4PwE3U= |