Skip to content

Commit b5e21b8

Browse files
committed
feat(api,cli,infra): add PostgreSQL as alternative database backend
Implement complete PostgreSQL support as an alternative to MongoDB, allowing users to select their preferred database backend via SHELLHUB_DATABASE environment variable. Infrastructure: - Add PostgreSQL 18.0 service in docker-compose.postgres.yml - Configure PostgreSQL environment variables (username, password, database) - Update bin/utils to recognize 'postgres' as valid database option - Add PostgreSQL healthcheck configuration Store Implementation: - Create pg store package with bun ORM and pgx driver - Implement all CRUD operations for all store interfaces - Add entity package with complete model-to-database mappings - Create 10 database migrations covering all tables and relations - Implement query system with pagination, sorting, and filtering - Add internal filter parser for complex queries (contains, eq, bool, gt, ne) - Implement SQL error mapping and handling utilities - Create transaction support with bun Entities and Relations: - Namespaces, Users, Memberships - Devices with status tracking - API Keys, Public Keys, Private Keys - Tags with many-to-many relations (device-tags, publickey-tags) - Sessions and Tunnels Testing Infrastructure: - Add testcontainers-based integration tests - Create YAML fixture system for test data - Implement comprehensive unit tests for all store operations - Add database test utilities and helpers Integration: - Update api/server.go to support database selection - Update cli/main.go to support database selection - Add bun, pgx, and testcontainers dependencies - Maintain backward compatibility with existing MongoDB implementation
1 parent 9c08fab commit b5e21b8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+4481
-24
lines changed

.env

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ SHELLHUB_AUTO_SSL=false
3636

3737
SHELLHUB_DATABASE=mongo
3838

39+
SHELLHUB_POSTGRES_HOST=postgres
40+
SHELLHUB_POSTGRES_PORT=5432
41+
SHELLHUB_POSTGRES_USERNAME=admin
42+
SHELLHUB_POSTGRES_PASSWORD=admin
43+
SHELLHUB_POSTGRES_DATABASE=main
44+
3945
# The domain of the server.
4046
# NOTICE: Required only if automatic HTTPS is enabled.
4147
# VALUES: A valid domain name

api/go.mod

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,23 @@ require (
88
github.com/getsentry/sentry-go v0.36.2
99
github.com/golang-jwt/jwt/v4 v4.5.2
1010
github.com/gorilla/websocket v1.5.3
11+
github.com/jackc/pgx/v5 v5.7.6
1112
github.com/labstack/echo-contrib v0.17.4
1213
github.com/labstack/echo/v4 v4.13.4
1314
github.com/labstack/gommon v0.4.2
15+
github.com/oiime/logrusbun v0.1.2-0.20241011112815-4df3a0fb0e11
1416
github.com/pkg/errors v0.9.1
1517
github.com/shellhub-io/mongotest v0.0.0-20230928124937-e33b07010742
1618
github.com/shellhub-io/shellhub v0.13.4
1719
github.com/sirupsen/logrus v1.9.3
1820
github.com/spf13/cobra v1.10.1
1921
github.com/square/mongo-lock v0.0.0-20230808145049-cfcf499f6bf0
2022
github.com/stretchr/testify v1.11.1
23+
github.com/testcontainers/testcontainers-go v0.40.0
2124
github.com/testcontainers/testcontainers-go/modules/mongodb v0.40.0
25+
github.com/testcontainers/testcontainers-go/modules/postgres v0.40.0
26+
github.com/uptrace/bun v1.2.15
27+
github.com/uptrace/bun/dialect/pgdialect v1.2.15
2228
github.com/xakep666/mongo-migrate v0.3.2
2329
go.mongodb.org/mongo-driver v1.17.6
2430
golang.org/x/crypto v0.43.0
@@ -73,6 +79,10 @@ require (
7379
github.com/hashicorp/go-multierror v1.1.1 // indirect
7480
github.com/hibiken/asynq v0.24.1 // indirect
7581
github.com/inconshreveable/mousetrap v1.1.0 // indirect
82+
github.com/jackc/pgpassfile v1.0.0 // indirect
83+
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
84+
github.com/jackc/puddle/v2 v2.2.2 // indirect
85+
github.com/jinzhu/inflection v1.0.0 // indirect
7686
github.com/josharian/intern v1.0.0 // indirect
7787
github.com/klauspost/compress v1.18.0 // indirect
7888
github.com/klauspost/pgzip v1.2.5 // indirect
@@ -109,24 +119,25 @@ require (
109119
github.com/prometheus/client_model v0.6.2 // indirect
110120
github.com/prometheus/common v0.63.0 // indirect
111121
github.com/prometheus/procfs v0.16.1 // indirect
122+
github.com/puzpuzpuz/xsync/v3 v3.5.1 // indirect
112123
github.com/redis/go-redis/v9 v9.0.3 // indirect
113124
github.com/robfig/cron/v3 v3.0.1 // indirect
114125
github.com/sethvargo/go-envconfig v0.9.0 // indirect
115126
github.com/shirou/gopsutil/v4 v4.25.6 // indirect
116127
github.com/spf13/cast v1.3.1 // indirect
117128
github.com/spf13/pflag v1.0.9 // indirect
118129
github.com/stretchr/objx v0.5.2 // indirect
119-
github.com/testcontainers/testcontainers-go v0.40.0 // indirect
120130
github.com/therootcompany/xz v1.0.1 // indirect
121131
github.com/tklauser/go-sysconf v0.3.13 // indirect
122132
github.com/tklauser/numcpus v0.7.0 // indirect
123133
github.com/tkuchiki/go-timezone v0.2.2 // indirect
124134
github.com/tkuchiki/parsetime v0.3.0 // indirect
135+
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
125136
github.com/ulikunitz/xz v0.5.14 // indirect
126137
github.com/valyala/bytebufferpool v1.0.0 // indirect
127138
github.com/valyala/fasttemplate v1.2.2 // indirect
128139
github.com/vmihailenco/go-tinylfu v0.2.2 // indirect
129-
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
140+
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
130141
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
131142
github.com/woodsbury/decimal128 v1.3.0 // indirect
132143
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
@@ -142,7 +153,7 @@ require (
142153
go4.org v0.0.0-20200411211856-f5505b9728dd // indirect
143154
golang.org/x/net v0.45.0 // indirect
144155
golang.org/x/sync v0.17.0 // indirect
145-
golang.org/x/sys v0.37.0 // indirect
156+
golang.org/x/sys v0.38.0 // indirect
146157
golang.org/x/text v0.30.0 // indirect
147158
golang.org/x/time v0.12.0 // indirect
148159
google.golang.org/protobuf v1.36.6 // indirect

api/go.sum

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,16 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO
210210
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
211211
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
212212
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
213+
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
214+
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
215+
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
216+
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
217+
github.com/jackc/pgx/v5 v5.7.6 h1:rWQc5FwZSPX58r1OQmkuaNicxdmExaEz5A2DO2hUuTk=
218+
github.com/jackc/pgx/v5 v5.7.6/go.mod h1:aruU7o91Tc2q2cFp5h4uP3f6ztExVpyVv88Xl/8Vl8M=
219+
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
220+
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
221+
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
222+
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
213223
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
214224
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
215225
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
@@ -240,6 +250,8 @@ github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0
240250
github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU=
241251
github.com/leodido/go-urn v1.2.2 h1:7z68G0FCGvDk646jz1AelTYNYWrTNm0bEcFAo147wt4=
242252
github.com/leodido/go-urn v1.2.2/go.mod h1:kUaIbLZWttglzwNuG0pgsh5vuV6u2YcGBYz1hIPjtOQ=
253+
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
254+
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
243255
github.com/lufia/plan9stats v0.0.0-20240408141607-282e7b5d6b74 h1:1KuuSOy4ZNgW0KA2oYIngXVFhQcXxhLqCVK7cBcldkk=
244256
github.com/lufia/plan9stats v0.0.0-20240408141607-282e7b5d6b74/go.mod h1:ilwx/Dta8jXAgpFYFvSWEMwxmbWXyiUHkd5FwyKhb5k=
245257
github.com/magiconair/properties v1.8.10 h1:s31yESBquKXCV9a/ScB3ESkOjUYYv+X0rg8SYxI99mE=
@@ -252,6 +264,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
252264
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
253265
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
254266
github.com/mattn/goveralls v0.0.9/go.mod h1:FRbM1PS8oVsOe9JtdzAAXM+DsvDMMHcM1C7drGJD8HY=
267+
github.com/mdelapenya/tlscert v0.2.0 h1:7H81W6Z/4weDvZBNOfQte5GpIMo0lGYEeWbkGp5LJHI=
268+
github.com/mdelapenya/tlscert v0.2.0/go.mod h1:O4njj3ELLnJjGdkN7M/vIVCpZ+Cf0L6muqOG4tLSl8o=
255269
github.com/mholt/archiver/v4 v4.0.0-alpha.8 h1:tRGQuDVPh66WCOelqe6LIGh0gwmfwxUrSSDunscGsRM=
256270
github.com/mholt/archiver/v4 v4.0.0-alpha.8/go.mod h1:5f7FUYGXdJWUjESffJaYR4R60VhnHxb2X3T1teMyv5A=
257271
github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0=
@@ -288,6 +302,10 @@ github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037 h1:G7ERwszslrBzRxj//J
288302
github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037/go.mod h1:2bpvgLBZEtENV5scfDFEtB/5+1M4hkQhDQrccEJ/qGw=
289303
github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90 h1:bQx3WeLcUWy+RletIKwUIt4x3t8n2SxavmoclizMb8c=
290304
github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90/go.mod h1:y5+oSEHCPT/DGrS++Wc/479ERge0zTFxaF8PbGKcg2o=
305+
github.com/oiime/logrusbun v0.1.1 h1:o3aK0PGErb1G0JC43yAIhoGxSbgtYRHhlyTtq6o1rag=
306+
github.com/oiime/logrusbun v0.1.1/go.mod h1:HH9akx9teKgQPX41TYpLLRNxaL8q9R+ltzABnwUHfBM=
307+
github.com/oiime/logrusbun v0.1.2-0.20241011112815-4df3a0fb0e11 h1:rAqW9sGcM0VsfBwgeBzHk0yebrRwfeSJFy9Egqi0fmM=
308+
github.com/oiime/logrusbun v0.1.2-0.20241011112815-4df3a0fb0e11/go.mod h1:HH9akx9teKgQPX41TYpLLRNxaL8q9R+ltzABnwUHfBM=
291309
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
292310
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
293311
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
@@ -327,6 +345,8 @@ github.com/prometheus/common v0.63.0 h1:YR/EIY1o3mEFP/kZCD7iDMnLPlGyuU2Gb3HIcXnA
327345
github.com/prometheus/common v0.63.0/go.mod h1:VVFF/fBIoToEnWRVkYoXEkq3R3paCoxG9PXP74SnV18=
328346
github.com/prometheus/procfs v0.16.1 h1:hZ15bTNuirocR6u0JZ6BAHHmwS1p8B4P6MRqxtzMyRg=
329347
github.com/prometheus/procfs v0.16.1/go.mod h1:teAbpZRB1iIAJYREa1LsoWUXykVXA1KlTmWl8x/U+Is=
348+
github.com/puzpuzpuz/xsync/v3 v3.5.1 h1:GJYJZwO6IdxN/IKbneznS6yPkVC+c3zyY/j19c++5Fg=
349+
github.com/puzpuzpuz/xsync/v3 v3.5.1/go.mod h1:VjzYrABPabuM4KyBh1Ftq6u8nhwY5tBPKP9jpmh0nnA=
330350
github.com/redis/go-redis/v9 v9.0.3 h1:+7mmR26M0IvyLxGZUHxu4GiBkJkVDid0Un+j4ScYu4k=
331351
github.com/redis/go-redis/v9 v9.0.3/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk=
332352
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
@@ -345,6 +365,7 @@ github.com/shellhub-io/mongotest v0.0.0-20230928124937-e33b07010742 h1:sIFW1zdZv
345365
github.com/shellhub-io/mongotest v0.0.0-20230928124937-e33b07010742/go.mod h1:6J6yfW5oIvAZ6VjxmV9KyFZyPFVM3B4V3Epbb+1c0oo=
346366
github.com/shirou/gopsutil/v4 v4.25.6 h1:kLysI2JsKorfaFPcYmcJqbzROzsBWEOAtw6A7dIfqXs=
347367
github.com/shirou/gopsutil/v4 v4.25.6/go.mod h1:PfybzyydfZcN+JMMjkF6Zb8Mq1A/VcogFFg7hj50W9c=
368+
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
348369
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
349370
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
350371
github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng=
@@ -361,6 +382,7 @@ github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpE
361382
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
362383
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
363384
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
385+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
364386
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
365387
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
366388
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
@@ -374,6 +396,8 @@ github.com/testcontainers/testcontainers-go v0.40.0 h1:pSdJYLOVgLE8YdUY2FHQ1Fxu+
374396
github.com/testcontainers/testcontainers-go v0.40.0/go.mod h1:FSXV5KQtX2HAMlm7U3APNyLkkap35zNLxukw9oBi/MY=
375397
github.com/testcontainers/testcontainers-go/modules/mongodb v0.40.0 h1:z/1qHeliTLDKNaJ7uOHOx1FjwghbcbYfga4dTFkF0hU=
376398
github.com/testcontainers/testcontainers-go/modules/mongodb v0.40.0/go.mod h1:GaunAWwMXLtsMKG3xn2HYIBDbKddGArfcGsF2Aog81E=
399+
github.com/testcontainers/testcontainers-go/modules/postgres v0.40.0 h1:s2bIayFXlbDFexo96y+htn7FzuhpXLYJNnIuglNKqOk=
400+
github.com/testcontainers/testcontainers-go/modules/postgres v0.40.0/go.mod h1:h+u/2KoREGTnTl9UwrQ/g+XhasAT8E6dClclAADeXoQ=
377401
github.com/testcontainers/testcontainers-go/modules/redis v0.32.0 h1:HW5Qo9qfLi5iwfS7cbXwG6qe8ybXGePcgGPEmVlVDlo=
378402
github.com/testcontainers/testcontainers-go/modules/redis v0.32.0/go.mod h1:5kltdxVKZG0aP1iegeqKz4K8HHyP0wbkW5o84qLyMjY=
379403
github.com/therootcompany/xz v1.0.1 h1:CmOtsn1CbtmyYiusbfmhmkpAAETj0wBIH6kCYaX+xzw=
@@ -387,20 +411,27 @@ github.com/tkuchiki/go-timezone v0.2.2 h1:MdHR65KwgVTwWFQrota4SKzc4L5EfuH5SdZZGt
387411
github.com/tkuchiki/go-timezone v0.2.2/go.mod h1:oFweWxYl35C/s7HMVZXiA19Jr9Y0qJHMaG/J2TES4LY=
388412
github.com/tkuchiki/parsetime v0.3.0 h1:cvblFQlPeAPJL8g6MgIGCHnnmHSZvluuY+hexoZCNqc=
389413
github.com/tkuchiki/parsetime v0.3.0/go.mod h1:OJkQmIrf5Ao7R+WYIdITPOfDVj8LmnHGCfQ8DTs3LCA=
414+
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYmW5DyG0UqvY96Bu5QYsTLvCHdrgo=
415+
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc/go.mod h1:bciPuU6GHm1iF1pBvUfxfsH0Wmnc2VbpgvbI9ZWuIRs=
390416
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
391417
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
392418
github.com/ulikunitz/xz v0.5.8/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
393419
github.com/ulikunitz/xz v0.5.14 h1:uv/0Bq533iFdnMHZdRBTOlaNMdb1+ZxXIlHDZHIHcvg=
394420
github.com/ulikunitz/xz v0.5.14/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
421+
github.com/uptrace/bun v0.3.9/go.mod h1:aL6D9vPw8DXaTQTwGrEPtUderBYXx7ShUmPfnxnqscw=
422+
github.com/uptrace/bun v1.2.15 h1:Ut68XRBLDgp9qG9QBMa9ELWaZOmzHNdczHQdrOZbEFE=
423+
github.com/uptrace/bun v1.2.15/go.mod h1:Eghz7NonZMiTX/Z6oKYytJ0oaMEJ/eq3kEV4vSqG038=
424+
github.com/uptrace/bun/dialect/pgdialect v1.2.15 h1:er+/3giAIqpfrXJw+KP9B7ujyQIi5XkPnFmgjAVL6bA=
425+
github.com/uptrace/bun/dialect/pgdialect v1.2.15/go.mod h1:QSiz6Qpy9wlGFsfpf7UMSL6mXAL1jDJhFwuOVacCnOQ=
395426
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
396427
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
397428
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
398429
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
399430
github.com/vmihailenco/go-tinylfu v0.2.2 h1:H1eiG6HM36iniK6+21n9LLpzx1G9R3DJa2UjUjbynsI=
400431
github.com/vmihailenco/go-tinylfu v0.2.2/go.mod h1:CutYi2Q9puTxfcolkliPq4npPuofg9N9t8JVrjzwa3Q=
401432
github.com/vmihailenco/msgpack/v5 v5.3.4/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
402-
github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9znI5mJU=
403-
github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
433+
github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8=
434+
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
404435
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
405436
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
406437
github.com/woodsbury/decimal128 v1.3.0 h1:8pffMNWIlC0O5vbyHWFZAt5yWvWcrHA+3ovIIjVWss0=
@@ -572,6 +603,8 @@ golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
572603
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
573604
golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
574605
golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
606+
golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc=
607+
golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
575608
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
576609
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
577610
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=

api/server.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"errors"
56
"os"
67
"strings"
78

@@ -10,8 +11,11 @@ import (
1011
"github.com/shellhub-io/shellhub/api/routes"
1112
"github.com/shellhub-io/shellhub/api/routes/middleware"
1213
"github.com/shellhub-io/shellhub/api/services"
14+
"github.com/shellhub-io/shellhub/api/store"
1315
"github.com/shellhub-io/shellhub/api/store/mongo"
14-
"github.com/shellhub-io/shellhub/api/store/mongo/options"
16+
mongooptions "github.com/shellhub-io/shellhub/api/store/mongo/options"
17+
"github.com/shellhub-io/shellhub/api/store/pg"
18+
pgoptions "github.com/shellhub-io/shellhub/api/store/pg/options"
1519
"github.com/shellhub-io/shellhub/pkg/api/internalclient"
1620
"github.com/shellhub-io/shellhub/pkg/cache"
1721
"github.com/shellhub-io/shellhub/pkg/envs"
@@ -22,6 +26,19 @@ import (
2226
)
2327

2428
type env struct {
29+
Database string `env:"DATABASE,default=mongo"`
30+
31+
// PostgresHost specifies the host for PostgreSQL.
32+
PostgresHost string `env:"POSTGRES_HOST,default=postgres"`
33+
// PostgresPort specifies the port for PostgreSQL.
34+
PostgresPort string `env:"POSTGRES_PORT,default=5432"`
35+
// PostgresUsername specifies the username for authenticate PostgreSQL.
36+
PostgresUsername string `env:"POSTGRES_USERNAME,default=admin"`
37+
// PostgresUser specifies the password for authenticate PostgreSQL.
38+
PostgresPassword string `env:"POSTGRES_PASSWORD,default=admin"`
39+
// PostgresDatabase especifica o nome do banco de dados PostgreSQL a ser utilizado.
40+
PostgresDatabase string `env:"POSTGRES_DATABASE,default=main"`
41+
2542
// MongoURI specifies the connection string for MongoDB.
2643
MongoURI string `env:"MONGO_URI,default=mongodb://mongo:27017/main"`
2744

@@ -78,7 +95,17 @@ func (s *Server) Setup(ctx context.Context) error {
7895

7996
log.Debug("Redis cache initialized successfully")
8097

81-
store, err := mongo.NewStore(ctx, s.env.MongoURI, cache, options.RunMigatrions)
98+
var store store.Store
99+
switch s.env.Database {
100+
case "mongo":
101+
store, err = mongo.NewStore(ctx, s.env.MongoURI, cache, mongooptions.RunMigatrions)
102+
case "postgres":
103+
uri := pg.URI(s.env.PostgresHost, s.env.PostgresPort, s.env.PostgresUsername, s.env.PostgresPassword, s.env.PostgresDatabase)
104+
store, err = pg.New(ctx, uri, pgoptions.Log("INFO", true), pgoptions.Migrate()) // TODO: Log envs
105+
default:
106+
log.WithField("database", s.env.Database).Error("invalid database")
107+
return errors.New("invalid database")
108+
}
82109
if err != nil {
83110
log.
84111
WithError(err).

api/store/errors.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ const (
1313
)
1414

1515
var (
16-
ErrDuplicate = errors.New("document duplicate", ErrLayer, ErrCodeDuplicated)
17-
ErrNoDocuments = errors.New("no documents", ErrLayer, ErrCodeNoDocument)
18-
ErrInvalidHex = errors.New("the provided hex string is not a valid ObjectID", ErrLayer, ErrCodeInvalid)
16+
ErrDuplicate = errors.New("document duplicate", ErrLayer, ErrCodeDuplicated)
17+
ErrNoDocuments = errors.New("no documents", ErrLayer, ErrCodeNoDocument)
18+
ErrInvalidHex = errors.New("the provided hex string is not a valid ObjectID", ErrLayer, ErrCodeInvalid)
19+
ErrResolverNotFound = errors.New("resolver not found", ErrLayer, ErrCodeInvalid)
1920
)
2021

2122
// Errors used by Cloud.

0 commit comments

Comments
 (0)