Skip to content

Commit 6ce21bc

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 0ca9381 commit 6ce21bc

Some content is hidden

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

74 files changed

+4406
-41
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: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,30 @@ require (
77
github.com/getsentry/sentry-go v0.36.0
88
github.com/golang-jwt/jwt/v4 v4.5.2
99
github.com/gorilla/websocket v1.5.3
10+
github.com/jackc/pgx/v5 v5.7.6
1011
github.com/labstack/echo-contrib v0.17.4
1112
github.com/labstack/echo/v4 v4.13.4
1213
github.com/labstack/gommon v0.4.2
14+
github.com/oiime/logrusbun v0.1.2-0.20241011112815-4df3a0fb0e11
1315
github.com/pkg/errors v0.9.1
1416
github.com/shellhub-io/mongotest v0.0.0-20230928124937-e33b07010742
1517
github.com/shellhub-io/shellhub v0.13.4
1618
github.com/sirupsen/logrus v1.9.3
1719
github.com/spf13/cobra v1.10.1
1820
github.com/square/mongo-lock v0.0.0-20230808145049-cfcf499f6bf0
1921
github.com/stretchr/testify v1.11.1
22+
github.com/testcontainers/testcontainers-go v0.39.0
2023
github.com/testcontainers/testcontainers-go/modules/mongodb v0.38.0
24+
github.com/testcontainers/testcontainers-go/modules/postgres v0.39.0
25+
github.com/uptrace/bun v1.2.15
26+
github.com/uptrace/bun/dialect/pgdialect v1.2.15
2127
github.com/xakep666/mongo-migrate v0.3.2
2228
go.mongodb.org/mongo-driver v1.17.4
2329
golang.org/x/crypto v0.43.0
2430
)
2531

2632
require (
27-
dario.cat/mergo v1.0.1 // indirect
33+
dario.cat/mergo v1.0.2 // indirect
2834
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
2935
github.com/Microsoft/go-winio v0.6.2 // indirect
3036
github.com/adhocore/gronx v1.8.1 // indirect
@@ -45,7 +51,7 @@ require (
4551
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
4652
github.com/distribution/reference v0.6.0 // indirect
4753
github.com/docker/docker v28.3.3+incompatible // indirect
48-
github.com/docker/go-connections v0.5.0 // indirect
54+
github.com/docker/go-connections v0.6.0 // indirect
4955
github.com/docker/go-units v0.5.0 // indirect
5056
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 // indirect
5157
github.com/ebitengine/purego v0.8.4 // indirect
@@ -70,6 +76,10 @@ require (
7076
github.com/hashicorp/go-multierror v1.1.1 // indirect
7177
github.com/hibiken/asynq v0.24.1 // indirect
7278
github.com/inconshreveable/mousetrap v1.1.0 // indirect
79+
github.com/jackc/pgpassfile v1.0.0 // indirect
80+
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
81+
github.com/jackc/puddle/v2 v2.2.2 // indirect
82+
github.com/jinzhu/inflection v1.0.0 // indirect
7383
github.com/klauspost/compress v1.18.0 // indirect
7484
github.com/klauspost/pgzip v1.2.5 // indirect
7585
github.com/leodido/go-urn v1.2.2 // indirect
@@ -100,24 +110,25 @@ require (
100110
github.com/prometheus/client_model v0.6.2 // indirect
101111
github.com/prometheus/common v0.63.0 // indirect
102112
github.com/prometheus/procfs v0.16.1 // indirect
113+
github.com/puzpuzpuz/xsync/v3 v3.5.1 // indirect
103114
github.com/redis/go-redis/v9 v9.0.3 // indirect
104115
github.com/robfig/cron/v3 v3.0.1 // indirect
105116
github.com/sethvargo/go-envconfig v0.9.0 // indirect
106-
github.com/shirou/gopsutil/v4 v4.25.5 // indirect
117+
github.com/shirou/gopsutil/v4 v4.25.6 // indirect
107118
github.com/spf13/cast v1.3.1 // indirect
108119
github.com/spf13/pflag v1.0.9 // indirect
109120
github.com/stretchr/objx v0.5.2 // indirect
110-
github.com/testcontainers/testcontainers-go v0.38.0 // indirect
111121
github.com/therootcompany/xz v1.0.1 // indirect
112122
github.com/tklauser/go-sysconf v0.3.13 // indirect
113123
github.com/tklauser/numcpus v0.7.0 // indirect
114124
github.com/tkuchiki/go-timezone v0.2.2 // indirect
115125
github.com/tkuchiki/parsetime v0.3.0 // indirect
126+
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
116127
github.com/ulikunitz/xz v0.5.14 // indirect
117128
github.com/valyala/bytebufferpool v1.0.0 // indirect
118129
github.com/valyala/fasttemplate v1.2.2 // indirect
119130
github.com/vmihailenco/go-tinylfu v0.2.2 // indirect
120-
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
131+
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
121132
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
122133
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
123134
github.com/xdg-go/scram v1.1.2 // indirect

api/go.sum

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ cloud.google.com/go/pubsub v1.0.1/go.mod h1:R0Gpsv3s54REJCy4fxDixWD93lHJMoZTyQ2k
1414
cloud.google.com/go/pubsub v1.1.0/go.mod h1:EwwdRX2sKPjnvnqCa270oGRyludottCI76h+R3AArQw=
1515
cloud.google.com/go/storage v1.0.0/go.mod h1:IhtSnM/ZTZV8YYJWCY8RULGVqBDmpoyjwiyrjsg+URw=
1616
cloud.google.com/go/storage v1.5.0/go.mod h1:tpKbwo567HUNpVclU5sGELwQWBDZ8gh0ZeosJ0Rtdos=
17-
dario.cat/mergo v1.0.1 h1:Ra4+bf83h2ztPIQYNP99R6m+Y7KfnARDfID+a+vLl4s=
18-
dario.cat/mergo v1.0.1/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
17+
dario.cat/mergo v1.0.2 h1:85+piFYR1tMbRrLcDwR18y4UKJ3aH1Tbzi24VRW1TK8=
18+
dario.cat/mergo v1.0.2/go.mod h1:E/hbnu0NxMFBjpMIE34DRGLWqDy0g5FuKDhCb31ngxA=
1919
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
2020
github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6 h1:He8afgbRMd7mFxO99hRNu+6tazq8nFF9lIwo9JFroBk=
2121
github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6/go.mod h1:8o94RPi1/7XTJvwPpRSzSUedZrtlirdB3r9Z20bi2f8=
@@ -81,8 +81,8 @@ github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5Qvfr
8181
github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
8282
github.com/docker/docker v28.3.3+incompatible h1:Dypm25kh4rmk49v1eiVbsAtpAsYURjYkaKubwuBdxEI=
8383
github.com/docker/docker v28.3.3+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
84-
github.com/docker/go-connections v0.5.0 h1:USnMq7hx7gwdVZq1L49hLXaFtUdTADjXGp+uj1Br63c=
85-
github.com/docker/go-connections v0.5.0/go.mod h1:ov60Kzw0kKElRwhNs9UlUHAE/F9Fe6GLaXnqyDdmEXc=
84+
github.com/docker/go-connections v0.6.0 h1:LlMG9azAe1TqfR7sO+NJttz1gy6KO7VJBh+pMmjSD94=
85+
github.com/docker/go-connections v0.6.0/go.mod h1:AahvXYshr6JgfUJGdDCs2b5EZG/vmaMAntpSFH5BFKE=
8686
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
8787
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
8888
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 h1:iFaUwBSo5Svw6L7HYpRu/0lE3e0BaElwnNO1qkNQxBY=
@@ -203,6 +203,16 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO
203203
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
204204
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
205205
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
206+
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
207+
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
208+
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
209+
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
210+
github.com/jackc/pgx/v5 v5.7.6 h1:rWQc5FwZSPX58r1OQmkuaNicxdmExaEz5A2DO2hUuTk=
211+
github.com/jackc/pgx/v5 v5.7.6/go.mod h1:aruU7o91Tc2q2cFp5h4uP3f6ztExVpyVv88Xl/8Vl8M=
212+
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
213+
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
214+
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
215+
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
206216
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
207217
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
208218
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
@@ -232,6 +242,8 @@ github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0
232242
github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU=
233243
github.com/leodido/go-urn v1.2.2 h1:7z68G0FCGvDk646jz1AelTYNYWrTNm0bEcFAo147wt4=
234244
github.com/leodido/go-urn v1.2.2/go.mod h1:kUaIbLZWttglzwNuG0pgsh5vuV6u2YcGBYz1hIPjtOQ=
245+
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
246+
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
235247
github.com/lufia/plan9stats v0.0.0-20240408141607-282e7b5d6b74 h1:1KuuSOy4ZNgW0KA2oYIngXVFhQcXxhLqCVK7cBcldkk=
236248
github.com/lufia/plan9stats v0.0.0-20240408141607-282e7b5d6b74/go.mod h1:ilwx/Dta8jXAgpFYFvSWEMwxmbWXyiUHkd5FwyKhb5k=
237249
github.com/magiconair/properties v1.8.10 h1:s31yESBquKXCV9a/ScB3ESkOjUYYv+X0rg8SYxI99mE=
@@ -242,6 +254,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
242254
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
243255
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
244256
github.com/mattn/goveralls v0.0.9/go.mod h1:FRbM1PS8oVsOe9JtdzAAXM+DsvDMMHcM1C7drGJD8HY=
257+
github.com/mdelapenya/tlscert v0.2.0 h1:7H81W6Z/4weDvZBNOfQte5GpIMo0lGYEeWbkGp5LJHI=
258+
github.com/mdelapenya/tlscert v0.2.0/go.mod h1:O4njj3ELLnJjGdkN7M/vIVCpZ+Cf0L6muqOG4tLSl8o=
245259
github.com/mholt/archiver/v4 v4.0.0-alpha.8 h1:tRGQuDVPh66WCOelqe6LIGh0gwmfwxUrSSDunscGsRM=
246260
github.com/mholt/archiver/v4 v4.0.0-alpha.8/go.mod h1:5f7FUYGXdJWUjESffJaYR4R60VhnHxb2X3T1teMyv5A=
247261
github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0=
@@ -272,6 +286,8 @@ github.com/nwaples/rardecode/v2 v2.2.0/go.mod h1:7uz379lSxPe6j9nvzxUZ+n7mnJNgjsR
272286
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
273287
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
274288
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
289+
github.com/oiime/logrusbun v0.1.2-0.20241011112815-4df3a0fb0e11 h1:rAqW9sGcM0VsfBwgeBzHk0yebrRwfeSJFy9Egqi0fmM=
290+
github.com/oiime/logrusbun v0.1.2-0.20241011112815-4df3a0fb0e11/go.mod h1:HH9akx9teKgQPX41TYpLLRNxaL8q9R+ltzABnwUHfBM=
275291
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
276292
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
277293
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
@@ -309,6 +325,8 @@ github.com/prometheus/common v0.63.0 h1:YR/EIY1o3mEFP/kZCD7iDMnLPlGyuU2Gb3HIcXnA
309325
github.com/prometheus/common v0.63.0/go.mod h1:VVFF/fBIoToEnWRVkYoXEkq3R3paCoxG9PXP74SnV18=
310326
github.com/prometheus/procfs v0.16.1 h1:hZ15bTNuirocR6u0JZ6BAHHmwS1p8B4P6MRqxtzMyRg=
311327
github.com/prometheus/procfs v0.16.1/go.mod h1:teAbpZRB1iIAJYREa1LsoWUXykVXA1KlTmWl8x/U+Is=
328+
github.com/puzpuzpuz/xsync/v3 v3.5.1 h1:GJYJZwO6IdxN/IKbneznS6yPkVC+c3zyY/j19c++5Fg=
329+
github.com/puzpuzpuz/xsync/v3 v3.5.1/go.mod h1:VjzYrABPabuM4KyBh1Ftq6u8nhwY5tBPKP9jpmh0nnA=
312330
github.com/redis/go-redis/v9 v9.0.3 h1:+7mmR26M0IvyLxGZUHxu4GiBkJkVDid0Un+j4ScYu4k=
313331
github.com/redis/go-redis/v9 v9.0.3/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk=
314332
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
@@ -325,8 +343,9 @@ github.com/sethvargo/go-envconfig v0.9.0 h1:Q6FQ6hVEeTECULvkJZakq3dZMeBQ3JUpcKMf
325343
github.com/sethvargo/go-envconfig v0.9.0/go.mod h1:Iz1Gy1Sf3T64TQlJSvee81qDhf7YIlt8GMUX6yyNFs0=
326344
github.com/shellhub-io/mongotest v0.0.0-20230928124937-e33b07010742 h1:sIFW1zdZvMTAvpHYOphDoWSh4tiGloK0El2GZni4E+U=
327345
github.com/shellhub-io/mongotest v0.0.0-20230928124937-e33b07010742/go.mod h1:6J6yfW5oIvAZ6VjxmV9KyFZyPFVM3B4V3Epbb+1c0oo=
328-
github.com/shirou/gopsutil/v4 v4.25.5 h1:rtd9piuSMGeU8g1RMXjZs9y9luK5BwtnG7dZaQUJAsc=
329-
github.com/shirou/gopsutil/v4 v4.25.5/go.mod h1:PfybzyydfZcN+JMMjkF6Zb8Mq1A/VcogFFg7hj50W9c=
346+
github.com/shirou/gopsutil/v4 v4.25.6 h1:kLysI2JsKorfaFPcYmcJqbzROzsBWEOAtw6A7dIfqXs=
347+
github.com/shirou/gopsutil/v4 v4.25.6/go.mod h1:PfybzyydfZcN+JMMjkF6Zb8Mq1A/VcogFFg7hj50W9c=
348+
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
330349
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
331350
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
332351
github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng=
@@ -343,6 +362,7 @@ github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpE
343362
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
344363
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
345364
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
365+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
346366
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
347367
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
348368
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
@@ -352,10 +372,12 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
352372
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
353373
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
354374
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
355-
github.com/testcontainers/testcontainers-go v0.38.0 h1:d7uEapLcv2P8AvH8ahLqDMMxda2W9gQN1nRbHS28HBw=
356-
github.com/testcontainers/testcontainers-go v0.38.0/go.mod h1:C52c9MoHpWO+C4aqmgSU+hxlR5jlEayWtgYrb8Pzz1w=
375+
github.com/testcontainers/testcontainers-go v0.39.0 h1:uCUJ5tA+fcxbFAB0uP3pIK3EJ2IjjDUHFSZ1H1UxAts=
376+
github.com/testcontainers/testcontainers-go v0.39.0/go.mod h1:qmHpkG7H5uPf/EvOORKvS6EuDkBUPE3zpVGaH9NL7f8=
357377
github.com/testcontainers/testcontainers-go/modules/mongodb v0.38.0 h1:A+YGYRoNLjDcYYnupsZBj3O3OfgEnS/o/MbQjiTqQwo=
358378
github.com/testcontainers/testcontainers-go/modules/mongodb v0.38.0/go.mod h1:4PMThrMlJpuUqLG+sCca3pWJKuReeQGioszuESf+uO0=
379+
github.com/testcontainers/testcontainers-go/modules/postgres v0.39.0 h1:REJz+XwNpGC/dCgTfYvM4SKqobNqDBfvhq74s2oHTUM=
380+
github.com/testcontainers/testcontainers-go/modules/postgres v0.39.0/go.mod h1:4K2OhtHEeT+JSIFX4V8DkGKsyLa96Y2vLdd3xsxD5HE=
359381
github.com/testcontainers/testcontainers-go/modules/redis v0.32.0 h1:HW5Qo9qfLi5iwfS7cbXwG6qe8ybXGePcgGPEmVlVDlo=
360382
github.com/testcontainers/testcontainers-go/modules/redis v0.32.0/go.mod h1:5kltdxVKZG0aP1iegeqKz4K8HHyP0wbkW5o84qLyMjY=
361383
github.com/therootcompany/xz v1.0.1 h1:CmOtsn1CbtmyYiusbfmhmkpAAETj0wBIH6kCYaX+xzw=
@@ -369,18 +391,25 @@ github.com/tkuchiki/go-timezone v0.2.2 h1:MdHR65KwgVTwWFQrota4SKzc4L5EfuH5SdZZGt
369391
github.com/tkuchiki/go-timezone v0.2.2/go.mod h1:oFweWxYl35C/s7HMVZXiA19Jr9Y0qJHMaG/J2TES4LY=
370392
github.com/tkuchiki/parsetime v0.3.0 h1:cvblFQlPeAPJL8g6MgIGCHnnmHSZvluuY+hexoZCNqc=
371393
github.com/tkuchiki/parsetime v0.3.0/go.mod h1:OJkQmIrf5Ao7R+WYIdITPOfDVj8LmnHGCfQ8DTs3LCA=
394+
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYmW5DyG0UqvY96Bu5QYsTLvCHdrgo=
395+
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc/go.mod h1:bciPuU6GHm1iF1pBvUfxfsH0Wmnc2VbpgvbI9ZWuIRs=
372396
github.com/ulikunitz/xz v0.5.8/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
373397
github.com/ulikunitz/xz v0.5.14 h1:uv/0Bq533iFdnMHZdRBTOlaNMdb1+ZxXIlHDZHIHcvg=
374398
github.com/ulikunitz/xz v0.5.14/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
399+
github.com/uptrace/bun v0.3.9/go.mod h1:aL6D9vPw8DXaTQTwGrEPtUderBYXx7ShUmPfnxnqscw=
400+
github.com/uptrace/bun v1.2.15 h1:Ut68XRBLDgp9qG9QBMa9ELWaZOmzHNdczHQdrOZbEFE=
401+
github.com/uptrace/bun v1.2.15/go.mod h1:Eghz7NonZMiTX/Z6oKYytJ0oaMEJ/eq3kEV4vSqG038=
402+
github.com/uptrace/bun/dialect/pgdialect v1.2.15 h1:er+/3giAIqpfrXJw+KP9B7ujyQIi5XkPnFmgjAVL6bA=
403+
github.com/uptrace/bun/dialect/pgdialect v1.2.15/go.mod h1:QSiz6Qpy9wlGFsfpf7UMSL6mXAL1jDJhFwuOVacCnOQ=
375404
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
376405
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
377406
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
378407
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
379408
github.com/vmihailenco/go-tinylfu v0.2.2 h1:H1eiG6HM36iniK6+21n9LLpzx1G9R3DJa2UjUjbynsI=
380409
github.com/vmihailenco/go-tinylfu v0.2.2/go.mod h1:CutYi2Q9puTxfcolkliPq4npPuofg9N9t8JVrjzwa3Q=
381410
github.com/vmihailenco/msgpack/v5 v5.3.4/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
382-
github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9znI5mJU=
383-
github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
411+
github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8=
412+
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
384413
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
385414
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
386415
github.com/xakep666/mongo-migrate v0.3.2 h1:qmDtIGiMRIwMvc84fOlsDoP+08S6NWLJDPqa4wPfQ1U=

api/server.go

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

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

78
"github.com/getsentry/sentry-go"
89
"github.com/labstack/echo/v4"
910
"github.com/shellhub-io/shellhub/api/routes"
1011
"github.com/shellhub-io/shellhub/api/services"
12+
"github.com/shellhub-io/shellhub/api/store"
1113
"github.com/shellhub-io/shellhub/api/store/mongo"
12-
"github.com/shellhub-io/shellhub/api/store/mongo/options"
14+
mongooptions "github.com/shellhub-io/shellhub/api/store/mongo/options"
15+
"github.com/shellhub-io/shellhub/api/store/pg"
16+
pgoptions "github.com/shellhub-io/shellhub/api/store/pg/options"
1317
"github.com/shellhub-io/shellhub/pkg/api/internalclient"
1418
"github.com/shellhub-io/shellhub/pkg/cache"
1519
"github.com/shellhub-io/shellhub/pkg/geoip/geolite2"
@@ -19,9 +23,22 @@ import (
1923
)
2024

2125
type env struct {
26+
Database string `env:"DATABASE,default=mongo"`
27+
2228
// MongoURI specifies the connection string for MongoDB.
2329
MongoURI string `env:"MONGO_URI,default=mongodb://mongo:27017/main"`
2430

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
// RedisURI specifies the connection string for Redis.
2643
RedisURI string `env:"REDIS_URI,default=redis://redis:6379"`
2744
// RedisCachePoolSize defines the maximum number of concurrent connections to Redis cache.
@@ -75,14 +92,22 @@ func (s *Server) Setup(ctx context.Context) error {
7592

7693
log.Debug("Redis cache initialized successfully")
7794

78-
store, err := mongo.NewStore(ctx, s.env.MongoURI, cache, options.RunMigatrions)
79-
if err != nil {
80-
log.
81-
WithError(err).
82-
Fatal("failed to create the store")
95+
var store store.Store
96+
switch s.env.Database {
97+
case "mongodb":
98+
store, err = mongo.NewStore(ctx, s.env.MongoURI, cache, mongooptions.RunMigatrions)
99+
case "postgres":
100+
uri := pg.URI(s.env.PostgresHost, s.env.PostgresPort, s.env.PostgresUsername, s.env.PostgresPassword, s.env.PostgresDatabase)
101+
store, err = pg.New(ctx, uri, pgoptions.Log("INFO", true), pgoptions.Migrate()) // TODO: Log envs
102+
default:
103+
log.WithField("database", s.env.Database).Error("invalid database")
104+
return errors.New("invalid database")
83105
}
84106

85-
log.Debug("MongoDB store connected successfully")
107+
if err != nil {
108+
log.WithError(err).Error("failed to create the store")
109+
return err
110+
}
86111

87112
apiClient, err := internalclient.NewClient(nil, internalclient.WithAsynqWorker(s.env.RedisURI))
88113
if err != nil {

0 commit comments

Comments
 (0)