diff --git a/.air.toml b/.air.toml new file mode 100644 index 0000000..fce3716 --- /dev/null +++ b/.air.toml @@ -0,0 +1,45 @@ +root = "." +testdata_dir = "testdata" +tmp_dir = "tmp" + +[build] + args_bin = [] + bin = "./tmp/main" + cmd = "go build -o ./tmp/main src/main.go" + delay = 0 + exclude_dir = ["assets", "tmp", "vendor", "testdata"] + exclude_file = [] + exclude_regex = ["_test.go"] + exclude_unchanged = false + follow_symlink = false + full_bin = "" + include_dir = [] + include_ext = ["go", "tpl", "tmpl", "html"] + include_file = [] + kill_delay = "0s" + log = "build-errors.log" + poll = false + poll_interval = 0 + rerun = false + rerun_delay = 500 + send_interrupt = false + stop_on_error = false + +[color] + app = "" + build = "yellow" + main = "magenta" + runner = "green" + watcher = "cyan" + +[log] + main_only = false + time = false + +[misc] + clean_on_exit = false + +[screen] + clear_on_rebuild = false + keep_scroll = true + diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..e5c60ab --- /dev/null +++ b/.dockerignore @@ -0,0 +1,32 @@ +# Include any files or directories that you don't want to be copied to your +# container here (e.g., local build artifacts, temporary files, etc.). +# +# For more help, visit the .dockerignore file reference guide at +# https://docs.docker.com/go/build-context-dockerignore/ + +**/.DS_Store +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/bin +**/charts +**/docker-compose* +**/compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..94f480d --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=lf \ No newline at end of file diff --git a/.github/workflows/release-staging.yml b/.github/workflows/release-staging.yml new file mode 100644 index 0000000..c4da81a --- /dev/null +++ b/.github/workflows/release-staging.yml @@ -0,0 +1,67 @@ +name: Deploy to Staging + +on: + workflow_dispatch: + push: + branches: + - develop + - yash/group + +jobs: + deploy: + name: Deploy + runs-on: ubuntu-latest + environment: staging + + env: + AWS_REGION: ${{ vars.AWS_REGION }} + ECR_REPOSITORY: ${{ vars.ECR_REPOSITORY }} + ECS_SERVICE: ${{ vars.ECS_SERVICE }} + ECS_CLUSTER: ${{ vars.ECS_CLUSTER }} + ECS_TASK_DEFINITION: ${{ vars.ECS_TASK_DEFINITION }} + CONTAINER_NAME: ${{ vars.CONTAINER_NAME }} + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: ${{ env.AWS_REGION }} + + - name: Download task definition + run: | + aws ecs describe-task-definition --task-definition wisee-backend --query taskDefinition > task-definition.json + + - name: Login to Amazon ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@v1 + + - name: Build + id: build-image + env: + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} + IMAGE_TAG: ${{ github.sha }} + run: | + docker build -f docker/release.dockerfile -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . + docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG + echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" + + - name: Fill in the new image ID in the Amazon ECS task definition + id: task-def + uses: aws-actions/amazon-ecs-render-task-definition@v1 + with: + task-definition: ${{ env.ECS_TASK_DEFINITION }} + container-name: ${{ env.CONTAINER_NAME }} + image: ${{ steps.build-image.outputs.image }} + + - name: Deploy Amazon ECS task definition + uses: aws-actions/amazon-ecs-deploy-task-definition@v1 + with: + task-definition: ${{ steps.task-def.outputs.task-definition }} + service: ${{ env.ECS_SERVICE }} + cluster: ${{ env.ECS_CLUSTER }} + wait-for-service-stability: false diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..fcdf47a --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,47 @@ +name: Tests + +on: + pull_request: + branches: ["*"] + +jobs: + tests: + runs-on: ubuntu-latest + + services: + postgres: + image: postgres:latest + env: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + POSTGRES_DB: wisee_core_test + ports: + - 5432:5432 + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + + name: Run tests + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Setup Go + uses: actions/setup-go@v4 + with: + go-version-file: "./go.mod" + + - run: go version + + - name: Copy Env + run: cp ./environments/test.env .env + + - name: Run Unit tests + run: make test_unit + + - name: Run Integration tests + run: make test_integration diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..88cb96b --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +build +TODO +tmp +.env +.env.local +bin +database/pgdata diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..a172d20 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,6 @@ +{ + "recommendations": [ + "esbenp.prettier-vscode", + "streetsidesoftware.code-spell-checker" + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..5824121 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,29 @@ +{ + /** + Editor rules + ------------ + */ + "editor.formatOnSave": true, + "editor.tabSize": 4, + "prettier.tabWidth": 4, + "prettier.printWidth": 120, + "editor.rulers": [ + { + "column": 120 + } + ], + /** + Formatters + ------------ + */ + "editor.defaultFormatter": "esbenp.prettier-vscode", + /** + New Lines + ------------ + */ + "files.eol": "\n", + "files.insertFinalNewline": true, + "[go]": { + "editor.defaultFormatter": "golang.go" + }, +} diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..aeb2520 --- /dev/null +++ b/Makefile @@ -0,0 +1,109 @@ +BINARY_NAME := "wisee" +DEV_DATABASE_URL := "postgres://postgres:postgres@localhost:5432/wisee_core?sslmode=disable" + +ARCH := $(or $(GOARCH),$(shell uname -m)) +OS := $(or $(GOOS),$(shell uname)) + +ifneq (,$(filter $(OS),Darwin darwin MacOS macos)) + OS := darwin +else ifneq (,$(filter $(OS),Linux linux)) + OS := linux +else + OS := windows +endif + +ifeq ($(ARCH),x86_64) + ARCH := amd64 +else ifeq ($(ARCH),i386) + ARCH := 386 +else ifeq ($(ARCH),aarch64) + ARCH := arm64 +endif + +build: + @echo "Building $(OS) $(ARCH) binary..." + @GOOS=$(OS) GOARCH=$(ARCH) CGO_ENABLED=0 go build $(ARGS) -o "bin/$(BINARY_NAME)" ./src + +test_unit: + @echo "Running tests..." + @@GOOS=$(OS) GOARCH=$(ARCH) ENV=test go test -race -covermode=atomic -v -coverpkg=./src/... ./tests/unit/... ./src/... + +test_integration: + @echo "Running tests..." + @@GOOS=$(OS) GOARCH=$(ARCH) ENV=test go test -race -covermode=atomic -v -coverpkg=./src/... ./tests/integration/... ./src/... + +test: test_unit test_integration + +clean: + @echo "Cleaning..." + @go clean + @rm -rf bin/* + +# Live Reload +watch: + @if command -v air > /dev/null; then \ + air; \ + echo "Watching...";\ + else \ + read -p "Go's 'air' is not installed on your machine. Do you want to install it? [Y/n] " choice; \ + if [ "$$choice" != "n" ] && [ "$$choice" != "N" ]; then \ + go install github.com/cosmtrek/air@latest; \ + air; \ + echo "Watching...";\ + else \ + echo "You chose not to install air. Exiting..."; \ + exit 1; \ + fi; \ + fi + +# Up all migrations +migrate-all-up: + @if command -v migrate > /dev/null; then \ + migrate -database $(DEV_DATABASE_URL) -path ./database/migrations up; \ + else \ + echo "Golang Migrate cli is not installed on your machine. Exiting..."; \ + exit 1; \ + fi + +# Drop all migrations when in development +migrate-all-down: + @if command -v migrate > /dev/null; then \ + migrate -database $(DEV_DATABASE_URL) -path ./database/migrations down; \ + else \ + echo "Golang Migrate cli is not installed on your machine. Exiting..."; \ + exit 1; \ + fi + +# Setup Databse and PGAdmin +docker-run: + @if command -v docker > /dev/null; then \ + docker-compose -f docker/dev-docker-compose.yaml up -d; \ + else \ + echo "Docker is not installed on your machine. Exiting..."; \ + exit 1; \ + fi + +# Down Database and PGAdmin +docker-down: + @if command -v docker > /dev/null; then \ + docker-compose -f docker/dev-docker-compose.yaml down; \ + else \ + echo "Docker is not installed on your machine. Exiting..."; \ + exit 1; \ + fi + +# Setup development environment +setup: + @echo "--- Copying .env files ---" + @cp environments/dev.env .env + + @echo "--- Setting up docker ---" + @make docker-run + + @echo "--- Waiting for database to setup ---" + @sleep 3 + + @echo "--- Running all migrations ---" + @make migrate-all-up + @echo "\n" + @echo "Setup complete. To start the server, run 'make watch'" diff --git a/README.md b/README.md index f6507de..0c12d70 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,78 @@ -# website-template -A template to create all public facing sites +# wisee backend + +## Setup + +To setup this project, you need to install the following tools: + +- [Go - 1.21.0](https://go.dev/dl/) +- [Air](https://github.com/cosmtrek/air) +- [Golang Migrate - CLI](https://github.com/golang-migrate/migrate/tree/master/cmd/migrate) +- [Docker](https://www.docker.com/get-started/) + +#### Windows + +> Skip this step if you are running the project in a Unix based system or have `make` already installed. + +We use the `make` command to run different scripts in the project. Install `make` command from [here](https://gnuwin32.sourceforge.net/packages/make.htm) + +### Development Setup + +1. Once you have installed the above tools, clone the repository and navigate to the project folder. + +2. To setup the project, run the following command: + +```bash +make setup +``` + +3. If the above command runs successfully, you can start the development server by running the following command: + +```bash +make watch +``` + +To check if the server is running, navigate to `http://localhost:8080/health` in your browser. + +### Creating a production build + +To create a production build, run the following command: + +```bash +make build +``` + +This will create a executable file in the `bin` folder. + +### Migrations + +Migrations are handled using [golang-migrate](https://github.com/golang-migrate/migrate), read the [docs](https://github.com/golang-migrate/migrate/blob/master/database/postgres/TUTORIAL.md) for more information on working with migrations. + +## Database + +### Connecting to the database + +1. Visit `http://localhost:54321` in your browser and login with the following credentials: + + - Email : `default@wisee.com` + - Password : `default` + +2. If you do have server created already, create a new server by clicking on the `Add New Server` button. + + ![alt text](./public/images/readme/connect-to-db-step-1.png) + +3. Enter the server name + + ![alt text](./public/images/readme/connect-to-db-step-2.png) + +4. Enter the connection details + + - Hostname/Address : `pg_db` + - Port : `5432` + - Username : `postgres` + - Password : `postgres` + + ![alt text](./public/images/readme/connect-to-db-step-3.png) + +5. Click on the `Save` button. You should now be able to see the server and associated databases in the left sidebar. + + ![alt text](./public/images/readme/connect-to-db-step-4.png) diff --git a/database/init/db-init.sh b/database/init/db-init.sh new file mode 100755 index 0000000..6575d78 --- /dev/null +++ b/database/init/db-init.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +set -e +set -u + +function createUserAndDatabase(){ + local database=$1 + echo "Creating user and database \"${database}\"..." + psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL + CREATE USER $database; + CREATE DATABASE $database; + GRANT ALL PRIVILEGES ON DATABASE $database TO $database; +EOSQL +} + +if [ -n "$POSTGRES_MULTIPLE_DATABASES" ]; then + echo "Creating multiple databases: $POSTGRES_MULTIPLE_DATABASES" + + for db in $(echo $POSTGRES_MULTIPLE_DATABASES | tr ',' ' '); do + createUserAndDatabase $db + done + echo "Multiple databases created" +fi +``` diff --git a/database/migrations/20230912194711_init.down.sql b/database/migrations/20230912194711_init.down.sql new file mode 100644 index 0000000..c99ddcd --- /dev/null +++ b/database/migrations/20230912194711_init.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS users; diff --git a/database/migrations/20230912194711_init.up.sql b/database/migrations/20230912194711_init.up.sql new file mode 100644 index 0000000..38e02ee --- /dev/null +++ b/database/migrations/20230912194711_init.up.sql @@ -0,0 +1,15 @@ +BEGIN; + +CREATE TABLE users ( + id bigserial PRIMARY KEY, + username varchar(256) UNIQUE NOT NULL, + email varchar UNIQUE NOT NULL, + is_verified boolean DEFAULT false, + password varchar(128) NOT NULL, + created_at timestamp DEFAULT (NOW() AT TIME ZONE 'UTC'), + updated_at timestamp DEFAULT (NOW() AT TIME ZONE 'UTC'), + is_deleted boolean DEFAULT false, + is_onboarding BOOLEAN NOT NULL DEFAULT TRUE +); + +COMMIT; diff --git a/database/migrations/20230912210234_users.down.sql b/database/migrations/20230912210234_users.down.sql new file mode 100644 index 0000000..19d1eeb --- /dev/null +++ b/database/migrations/20230912210234_users.down.sql @@ -0,0 +1,2 @@ +DELETE FROM users WHERE id = 3 AND EXISTS (SELECT 1 FROM users WHERE id = 3); +DELETE FROM users WHERE id = 4 AND EXISTS (SELECT 1 FROM users WHERE id = 4); diff --git a/database/migrations/20230912210234_users.up.sql b/database/migrations/20230912210234_users.up.sql new file mode 100644 index 0000000..76dbf7c --- /dev/null +++ b/database/migrations/20230912210234_users.up.sql @@ -0,0 +1,6 @@ +BEGIN; + +INSERT INTO users (id, username, email, password) VALUES (3, 'another user', 'another@gmail.com', 'anotherpasswordhere'); +INSERT INTO users (id, username, email, password) VALUES (4, 'karla', 'karla@gmail.com', 'karlaisanewuser'); + +COMMIT; diff --git a/database/migrations/20240212042820_forms.down.sql b/database/migrations/20240212042820_forms.down.sql new file mode 100644 index 0000000..b77b4fd --- /dev/null +++ b/database/migrations/20240212042820_forms.down.sql @@ -0,0 +1,5 @@ +DROP TABLE IF EXISTS forms.responses; +DROP TABLE IF EXISTS forms.metadata; +DROP TABLE IF EXISTS forms.form; +DROP TYPE IF EXISTS forms.status_type; +DROP SCHEMA IF EXISTS forms CASCADE; diff --git a/database/migrations/20240212042820_forms.up.sql b/database/migrations/20240212042820_forms.up.sql new file mode 100644 index 0000000..7dfbba9 --- /dev/null +++ b/database/migrations/20240212042820_forms.up.sql @@ -0,0 +1,77 @@ +BEGIN; + +CREATE SCHEMA forms; + +CREATE TYPE forms.status_type AS ENUM ('DRAFT', 'PUBLISHED'); + +CREATE TABLE forms.form ( + id bigserial PRIMARY KEY, + content JSONB NOT NULL, + created_by_id INT NOT NULL, + owner_id INT NOT NULL, + "status" forms.status_type DEFAULT 'DRAFT', + updated_by_id INT, + created_at timestamp DEFAULT (NOW() AT TIME ZONE 'UTC'), + updated_at timestamp DEFAULT NULL +); + +CREATE TABLE forms.metadata ( + id bigserial PRIMARY KEY, + form_id INT NOT NULL, + is_deleted BOOLEAN NOT NULL DEFAULT FALSE, + accepting_responses BOOLEAN NOT NULL DEFAULT FALSE, + allow_guest_responses BOOLEAN NOT NULL DEFAULT TRUE, + allow_multiple_responses BOOLEAN NOT NULL DEFAULT FALSE, + send_confirmation_email_to_respondee BOOLEAN NOT NULL DEFAULT FALSE, + send_submission_email_to_owner BOOLEAN NOT NULL DEFAULT FALSE, + updated_by_id INT, + valid_till timestamp, + created_at timestamp DEFAULT (NOW() AT TIME ZONE 'UTC'), + updated_at timestamp DEFAULT NULL +); + +CREATE TABLE forms.responses ( + id bigserial PRIMARY KEY, + response_by_id INT NOT NULL, + content JSONB NOT NULL, + form_id INT NOT NULL, + created_at timestamp DEFAULT (NOW() AT TIME ZONE 'UTC'), + updated_at timestamp DEFAULT NULL +); + +ALTER TABLE forms.form +ADD CONSTRAINT fk_forms_users_created_by +FOREIGN KEY (created_by_id) +REFERENCES users(id); + +ALTER TABLE forms.form +ADD CONSTRAINT fk_forms_users_owner +FOREIGN KEY (owner_id) +REFERENCES users(id); + +ALTER TABLE forms.form +ADD CONSTRAINT fk_forms_users_updated_by +FOREIGN KEY (updated_by_id) +REFERENCES users(id); + +ALTER TABLE forms.metadata +ADD CONSTRAINT fk_form_metadata_form +FOREIGN KEY (form_id) +REFERENCES forms.form(id); + +ALTER TABLE forms.metadata +ADD CONSTRAINT fk_form_metadata_users_updated_by +FOREIGN KEY (updated_by_id) +REFERENCES users(id); + +ALTER TABLE forms.responses +ADD CONSTRAINT fk_form_responses_users +FOREIGN KEY (response_by_id) +REFERENCES users(id); + +ALTER TABLE forms.responses +ADD CONSTRAINT fk_form_responses_form +FOREIGN KEY (form_id) +REFERENCES forms.form(id); + +COMMIT; diff --git a/docker/dev-docker-compose.yaml b/docker/dev-docker-compose.yaml new file mode 100644 index 0000000..f7a78af --- /dev/null +++ b/docker/dev-docker-compose.yaml @@ -0,0 +1,28 @@ +version: "3.0" + +services: + databse: + container_name: pg_db + image: postgres:16 + restart: always + ports: + - "5432:5432" + volumes: + - ../database/init:/docker-entrypoint-initdb.d + - ../database/pgdata:/var/lib/postgresql/data + environment: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + POSTGRES_MULTIPLE_DATABASES: "wisee_core" + + pgadmin: + image: dpage/pgadmin4 + restart: always + environment: + PGADMIN_DEFAULT_EMAIL: "default@wisee.com" + PGADMIN_DEFAULT_PASSWORD: "default" + ports: + - "54321:80" + +volumes: + pgdata: diff --git a/docker/release.dockerfile b/docker/release.dockerfile new file mode 100644 index 0000000..f1fb4ac --- /dev/null +++ b/docker/release.dockerfile @@ -0,0 +1,57 @@ +# Create a stage for building the application. +ARG GO_VERSION=1.21.0 +FROM golang:${GO_VERSION} AS build +WORKDIR /src + +# Download dependencies as a separate step to take advantage of Docker's caching. +RUN --mount=type=cache,target=/go/pkg/mod/ \ + --mount=type=bind,source=go.sum,target=go.sum \ + --mount=type=bind,source=go.mod,target=go.mod \ + go mod download -x + +# This is the architecture you’re building for, which is passed in by the builder. +# Placing it here allows the previous steps to be cached across architectures. +ARG TARGETARCH + +# Build the application. +# Leverage a cache mount to /go/pkg/mod/ to speed up subsequent builds. +# Leverage a bind mount to the current directory to avoid having to copy the +# source code into the container. +RUN --mount=type=cache,target=/go/pkg/mod/ \ + --mount=type=bind,target=. \ + GOOS=linux CGO_ENABLED=0 GOARCH=amd64 go build -o /bin/server ./src + +################################################################################ +# Create a new stage for running the application that contains the minimal +FROM alpine:latest AS final + +# Install any runtime dependencies that are needed to run your application. +# Leverage a cache mount to /var/cache/apk/ to speed up subsequent builds. +RUN --mount=type=cache,target=/var/cache/apk \ + apk --update add \ + ca-certificates \ + tzdata \ + && \ + update-ca-certificates + +# Create a non-privileged user that the app will run under. +# See https://docs.docker.com/go/dockerfile-user-best-practices/ +ARG UID=10001 +RUN adduser \ + --disabled-password \ + --gecos "" \ + --home "/nonexistent" \ + --shell "/sbin/nologin" \ + --no-create-home \ + --uid "${UID}" \ + appuser +USER appuser + +# Copy the executable from the "build" stage. +COPY --from=build /bin/server /bin/ + +# Expose port 8080 to the outside world +EXPOSE 8080 + +# Command to run the executable +ENTRYPOINT [ "/bin/server" ] diff --git a/environments/dev.env b/environments/dev.env new file mode 100644 index 0000000..5efa24d --- /dev/null +++ b/environments/dev.env @@ -0,0 +1,15 @@ +ENV="dev" +JWT_SECRET="secret" +JWT_VALIDITY_IN_DAYS=1 +JWT_ISSUER="wisee-backend" + +DOMAIN="localhost" +AUTH_REDIRECT_URL="http://localhost:3000/dashboard" + +DB_URL="postgresql://postgres:postgres@localhost:5432/wisee_core?sslmode=disable" +TEST_DB_URL="postgresql://postgres:postgres@localhost:5432/wisee_core_test?sslmode=disable" +DB_MAX_OPEN_CONNECTIONS=10 + +GOOGLE_CLIENT_ID="google-client-id" +GOOGLE_CLIENT_SECRET="google-client-secret" +GOOGLE_REDIRECT_URL="http://localhost:8080/v1/auth/google/callback" diff --git a/environments/test.env b/environments/test.env new file mode 100644 index 0000000..6454b08 --- /dev/null +++ b/environments/test.env @@ -0,0 +1,15 @@ +ENV="test" +JWT_SECRET="secret" +JWT_VALIDITY_IN_DAYS=1 +JWT_ISSUER="wisee-backend" + +DOMAIN="localhost" +AUTH_REDIRECT_URL="http://localhost:3000/dashboard" + +DB_URL="postgresql://postgres:postgres@localhost:5432/wisee_core?sslmode=disable" +TEST_DB_URL="postgresql://postgres:postgres@localhost:5432/wisee_core_test?sslmode=disable" +DB_MAX_OPEN_CONNECTIONS=10 + +GOOGLE_CLIENT_ID="google-client-id" +GOOGLE_CLIENT_SECRET="google-client-secret" +GOOGLE_REDIRECT_URL="http://localhost:8080/v1/auth/google/callback" diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..bf8de80 --- /dev/null +++ b/go.mod @@ -0,0 +1,74 @@ +module github.com/Real-Dev-Squad/wisee-backend + +go 1.21.0 + +require ( + github.com/gin-contrib/cors v1.4.0 + github.com/gin-gonic/gin v1.9.1 + github.com/golang-jwt/jwt/v5 v5.0.0 + github.com/joho/godotenv v1.5.1 + github.com/lib/pq v1.10.9 + github.com/uptrace/bun v1.1.14 + github.com/uptrace/bun/dialect/pgdialect v1.1.14 + github.com/uptrace/bun/driver/pgdriver v1.1.14 + github.com/uptrace/bun/extra/bundebug v1.1.14 + golang.org/x/oauth2 v0.14.0 + google.golang.org/api v0.150.0 +) + +require ( + github.com/hashicorp/errwrap v1.1.0 // indirect + github.com/hashicorp/go-multierror v1.1.1 // indirect + go.uber.org/atomic v1.7.0 // indirect +) + +require ( + cloud.google.com/go/compute v1.23.3 // indirect + cloud.google.com/go/compute/metadata v0.2.3 // indirect + github.com/bytedance/sonic v1.10.0 // indirect + github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect + github.com/chenzhuoyu/iasm v0.9.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/fatih/color v1.15.0 // indirect + github.com/gabriel-vasile/mimetype v1.4.2 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.15.1 // indirect + github.com/goccy/go-json v0.10.2 // indirect + github.com/golang-migrate/migrate/v4 v4.17.0 + github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect + github.com/golang/protobuf v1.5.3 // indirect + github.com/google/s2a-go v0.1.7 // indirect + github.com/google/uuid v1.4.0 // indirect + github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect + github.com/googleapis/gax-go/v2 v2.12.0 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.2.5 // indirect + github.com/leodido/go-urn v1.2.4 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.0.9 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/testify v1.8.4 + github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.11 // indirect + github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect + github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect + go.opencensus.io v0.24.0 // indirect + golang.org/x/arch v0.4.0 // indirect + golang.org/x/crypto v0.17.0 // indirect + golang.org/x/net v0.18.0 // indirect + golang.org/x/sys v0.15.0 // indirect + golang.org/x/text v0.14.0 // indirect + google.golang.org/appengine v1.6.7 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20231030173426-d783a09b4405 // indirect + google.golang.org/grpc v1.59.0 // indirect + google.golang.org/protobuf v1.31.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect + mellium.im/sasl v0.3.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..9fa0285 --- /dev/null +++ b/go.sum @@ -0,0 +1,317 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +cloud.google.com/go/compute v1.23.3 h1:6sVlXXBmbd7jNX0Ipq0trII3e4n1/MsADLK6a+aiVlk= +cloud.google.com/go/compute v1.23.3/go.mod h1:VCgBUoMnIVIR0CscqQiPJLAG25E3ZRZMzcFZeQ+h8CI= +cloud.google.com/go/compute/metadata v0.2.3 h1:mg4jlk7mCAj6xXp9UJ4fjI9VUI5rubuGBW5aJ7UnBMY= +cloud.google.com/go/compute/metadata v0.2.3/go.mod h1:VAV5nSsACxMJvgaAuX6Pk2AawlZn8kiOGuCv6gTkwuA= +github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25UVaW/CKtUDjefjrs0SPonmDGUVOYP0= +github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E= +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/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= +github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM= +github.com/bytedance/sonic v1.10.0 h1:qtNZduETEIWJVIyDl01BeNxur2rW9OwTQ/yBqFRkKEk= +github.com/bytedance/sonic v1.10.0/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4= +github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= +github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= +github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= +github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0= +github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpVsBuRksnlj1mLy4AWzRNQYxauNi62uWcE3to6eA= +github.com/chenzhuoyu/iasm v0.9.0 h1:9fhXjVzq5hUy2gkhhgHl95zG2cEAhw9OSGs8toWWAwo= +github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dhui/dktest v0.4.0 h1:z05UmuXZHO/bgj/ds2bGMBu8FI4WA+Ag/m3ghL+om7M= +github.com/dhui/dktest v0.4.0/go.mod h1:v/Dbz1LgCBOi2Uki2nUqLBGa83hWBGFMu5MrgMDCc78= +github.com/docker/distribution v2.8.2+incompatible h1:T3de5rq0dB1j30rp0sA2rER+m322EBzniBPB6ZIzuh8= +github.com/docker/distribution v2.8.2+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w= +github.com/docker/docker v24.0.7+incompatible h1:Wo6l37AuwP3JaMnZa226lzVXGA3F9Ig1seQen0cKYlM= +github.com/docker/docker v24.0.7+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= +github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ= +github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= +github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4= +github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= +github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= +github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= +github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw= +github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= +github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA= +github.com/gin-contrib/cors v1.4.0 h1:oJ6gwtUl3lqV0WEIwM/LxPF1QZ5qe2lGWdY2+bz7y0g= +github.com/gin-contrib/cors v1.4.0/go.mod h1:bs9pNM0x/UsmHPBWT2xZz9ROh8xYjYkiURUfmBoMlcs= +github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.8.1/go.mod h1:ji8BvRH1azfM+SYow9zQ6SZMvR8qOMZHmsCuWR9tTTk= +github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= +github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= +github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.10.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos= +github.com/go-playground/validator/v10 v10.15.1 h1:BSe8uhN+xQ4r5guV/ywQI4gO59C2raYcGffYWZEjZzM= +github.com/go-playground/validator/v10 v10.15.1/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= +github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= +github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= +github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= +github.com/golang-jwt/jwt/v5 v5.0.0 h1:1n1XNM9hk7O9mnQoNBGolZvzebBQ7p93ULHRc28XJUE= +github.com/golang-jwt/jwt/v5 v5.0.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= +github.com/golang-migrate/migrate/v4 v4.17.0 h1:rd40H3QXU0AA4IoLllFcEAEo9dYKRHYND2gB4p7xcaU= +github.com/golang-migrate/migrate/v4 v4.17.0/go.mod h1:+Cp2mtLP4/aXDTKb9wmXYitdrNx2HGs45rbWAo6OsKM= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= +github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= +github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= +github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/s2a-go v0.1.7 h1:60BLSyTrOV4/haCDW4zb1guZItoSq8foHCXrAnjBo/o= +github.com/google/s2a-go v0.1.7/go.mod h1:50CgR4k1jNlWBu4UfS4AcfhVe1r6pdZPygJ3R8F0Qdw= +github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4= +github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/googleapis/enterprise-certificate-proxy v0.3.2 h1:Vie5ybvEvT75RniqhfFxPRy3Bf7vr3h0cechB90XaQs= +github.com/googleapis/enterprise-certificate-proxy v0.3.2/go.mod h1:VLSiSSBs/ksPL8kq3OBOQ6WRI2QnaFynd1DCjZ62+V0= +github.com/googleapis/gax-go/v2 v2.12.0 h1:A+gCJKdRfqXkr+BIRGtZLibNXf0m1f9E4HG56etFpas= +github.com/googleapis/gax-go/v2 v2.12.0/go.mod h1:y+aIqrI5eb1YGMVJfuV3185Ts/D7qKpsEkdD5+I6QGU= +github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I= +github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo= +github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg= +github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= +github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY= +github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= +github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= +github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= +github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0= +github.com/moby/term v0.5.0/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A= +github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc= +github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= +github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= +github.com/opencontainers/image-spec v1.0.2 h1:9yCKha/T5XdGtO0q9Q9a6T5NUCsTn/DrBg0D7ufOcFM= +github.com/opencontainers/image-spec v1.0.2/go.mod h1:BtxoFyWECRxE4U/7sNtV5W15zMzWCbyJoFRP3s7yZA0= +github.com/pelletier/go-toml/v2 v2.0.1/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo= +github.com/pelletier/go-toml/v2 v2.0.9 h1:uH2qQXheeefCCkuBBSLi7jCiSmj3VRh2+Goq2N7Xxu0= +github.com/pelletier/go-toml/v2 v2.0.9/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= +github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= +github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8= +github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYmW5DyG0UqvY96Bu5QYsTLvCHdrgo= +github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc/go.mod h1:bciPuU6GHm1iF1pBvUfxfsH0Wmnc2VbpgvbI9ZWuIRs= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M= +github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY= +github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= +github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +github.com/uptrace/bun v1.1.14 h1:S5vvNnjEynJ0CvnrBOD7MIRW7q/WbtvFXrdfy0lddAM= +github.com/uptrace/bun v1.1.14/go.mod h1:RHk6DrIisO62dv10pUOJCz5MphXThuOTpVNYEYv7NI8= +github.com/uptrace/bun/dialect/pgdialect v1.1.14 h1:b7+V1KDJPQSFYgkG/6YLXCl2uvwEY3kf/GSM7hTHRDY= +github.com/uptrace/bun/dialect/pgdialect v1.1.14/go.mod h1:v6YiaXmnKQ2FlhRD2c0ZfKd+QXH09pYn4H8ojaavkKk= +github.com/uptrace/bun/driver/pgdriver v1.1.14 h1:V2Etm7mLGS3mhx8ddxZcUnwZLX02Jmq9JTlo0sNVDhA= +github.com/uptrace/bun/driver/pgdriver v1.1.14/go.mod h1:D4FjWV9arDYct6sjMJhFoyU71SpllZRHXFRRP2Kd0Kw= +github.com/uptrace/bun/extra/bundebug v1.1.14 h1:9OCGfP9ZDlh41u6OLerWdhBtJAVGXHr0xtxO4xWi6t0= +github.com/uptrace/bun/extra/bundebug v1.1.14/go.mod h1:lto3guzS2v6mnQp1+akyE+ecBLOltevDDe324NXEYdw= +github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9znI5mJU= +github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc= +github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g= +github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds= +go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0= +go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo= +go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= +go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= +golang.org/x/arch v0.4.0 h1:A8WCeEWhLwPBKNbFi5Wv5UTCBx5zzubnXDlMOFAzFMc= +golang.org/x/arch v0.4.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= +golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU= +golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= +golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg= +golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/oauth2 v0.14.0 h1:P0Vrf/2538nmC0H+pEQ3MNFRRnVR7RlqyVw+bvm26z0= +golang.org/x/oauth2 v0.14.0/go.mod h1:lAtNWgaWfL4cm7j2OV8TxGi9Qb7ECORx8DktCY74OwM= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= +golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.10.0 h1:tvDr/iQoUqNdohiYm0LmmKcBk+q86lb9EprIUFhHHGg= +golang.org/x/tools v0.10.0/go.mod h1:UJwyiVBsOA2uwvK/e5OY3GTpDUJriEd+/YlqAwLPmyM= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/api v0.150.0 h1:Z9k22qD289SZ8gCJrk4DrWXkNjtfvKAUo/l1ma8eBYE= +google.golang.org/api v0.150.0/go.mod h1:ccy+MJ6nrYFgE3WgRx/AMXOxOmU8Q4hSa+jjibzhxcg= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c= +google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= +google.golang.org/genproto v0.0.0-20231016165738-49dd2c1f3d0b h1:+YaDE2r2OG8t/z5qmsh7Y+XXwCbvadxxZ0YY6mTdrVA= +google.golang.org/genproto v0.0.0-20231016165738-49dd2c1f3d0b/go.mod h1:CgAqfJo+Xmu0GwA0411Ht3OU3OntXwsGmrmjI8ioGXI= +google.golang.org/genproto/googleapis/api v0.0.0-20231016165738-49dd2c1f3d0b h1:CIC2YMXmIhYw6evmhPxBKJ4fmLbOFtXQN/GV3XOZR8k= +google.golang.org/genproto/googleapis/api v0.0.0-20231016165738-49dd2c1f3d0b/go.mod h1:IBQ646DjkDkvUIsVq/cc03FUFQ9wbZu7yE396YcL870= +google.golang.org/genproto/googleapis/rpc v0.0.0-20231030173426-d783a09b4405 h1:AB/lmRny7e2pLhFEYIbl5qkDAUt2h0ZRO4wGPhZf+ik= +google.golang.org/genproto/googleapis/rpc v0.0.0-20231030173426-d783a09b4405/go.mod h1:67X1fPuzjcrkymZzZV1vvkFeTn2Rvc6lYF9MYFGCcwE= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= +google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= +google.golang.org/grpc v1.59.0 h1:Z5Iec2pjwb+LEOqzpB2MR12/eKFhDPhuqW91O+4bwUk= +google.golang.org/grpc v1.59.0/go.mod h1:aUPDwccQo6OTjy7Hct4AfBPD1GptF4fyUjIkQ9YtF98= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= +google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +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.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +mellium.im/sasl v0.3.1 h1:wE0LW6g7U83vhvxjC1IY8DnXM+EU095yeo8XClvCdfo= +mellium.im/sasl v0.3.1/go.mod h1:xm59PUYpZHhgQ9ZqoJ5QaCqzWMi8IeS49dhp6plPCzw= +nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= +rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= diff --git a/public/images/readme/connect-to-db-step-1.png b/public/images/readme/connect-to-db-step-1.png new file mode 100644 index 0000000..fff8c19 Binary files /dev/null and b/public/images/readme/connect-to-db-step-1.png differ diff --git a/public/images/readme/connect-to-db-step-2.png b/public/images/readme/connect-to-db-step-2.png new file mode 100644 index 0000000..a6effe2 Binary files /dev/null and b/public/images/readme/connect-to-db-step-2.png differ diff --git a/public/images/readme/connect-to-db-step-3.png b/public/images/readme/connect-to-db-step-3.png new file mode 100644 index 0000000..cf1c3d2 Binary files /dev/null and b/public/images/readme/connect-to-db-step-3.png differ diff --git a/public/images/readme/connect-to-db-step-4.png b/public/images/readme/connect-to-db-step-4.png new file mode 100644 index 0000000..b640947 Binary files /dev/null and b/public/images/readme/connect-to-db-step-4.png differ diff --git a/src/config/core-config.go b/src/config/core-config.go new file mode 100644 index 0000000..8fd03bc --- /dev/null +++ b/src/config/core-config.go @@ -0,0 +1,87 @@ +package config + +import ( + "os" + "path" + "runtime" + "strconv" + + "github.com/Real-Dev-Squad/wisee-backend/src/utils/logger" + "github.com/joho/godotenv" +) + +var Env string + +var JwtSecret string +var JwtValidityInDays int +var JwtIssuer string + +var Domain string +var AuthRedirectUrl string + +var DbUrl string +var TestDbUrl string +var DbMaxOpenConnections int + +var GoogleClientId string +var GoogleClientSecret string +var GoogleRedirectUrl string + +func loadEnv() { + env := os.Getenv("ENV") + + // If the environment is production, we don't need to load the .env file + // we assume that the environment variables are already set + if env == "production" || env == "staging" { + return + } + + if env == "test" { + // for tests, chdir to the project root + _, filename, _, _ := runtime.Caller(0) + dir := path.Join(path.Dir(filename), "../..") + + if err := os.Chdir(dir); err != nil { + panic(err) + } + + if err := godotenv.Load(".env"); err != nil { + logger.Error("Error loading .env file.", err) + } + + return + } + + if err := godotenv.Load(".env"); err != nil { + logger.Fatal("Error loading .env file") + } +} + +func init() { + loadEnv() + + env := os.Getenv("ENV") + + if env == "" { + Env = "dev" + } else { + Env = env + } + + JwtSecret = os.Getenv("JWT_SECRET") + JwtValidityInDays, _ = strconv.Atoi(os.Getenv("JWT_VALIDITY_IN_DAYS")) + JwtIssuer = os.Getenv("JWT_ISSUER") + + Domain = os.Getenv("DOMAIN") + AuthRedirectUrl = os.Getenv("AUTH_REDIRECT_URL") + + DbUrl = os.Getenv("DB_URL") + TestDbUrl = os.Getenv("TEST_DB_URL") + DbMaxOpenConnections, _ = strconv.Atoi(os.Getenv("DB_MAX_OPEN_CONNECTIONS")) + + GoogleClientId = os.Getenv("GOOGLE_CLIENT_ID") + GoogleClientSecret = os.Getenv("GOOGLE_CLIENT_SECRET") + GoogleRedirectUrl = os.Getenv("GOOGLE_REDIRECT_URL") + + logger.Info("Loaded environment variables") +} diff --git a/src/dtos/http_request.go b/src/dtos/http_request.go new file mode 100644 index 0000000..6ea7e13 --- /dev/null +++ b/src/dtos/http_request.go @@ -0,0 +1,20 @@ +package dtos + +import "github.com/Real-Dev-Squad/wisee-backend/src/models" + +type CreateFormRequestDto struct { + Content models.FormContent `json:"content"` + PerformedById int64 `json:"performed_by_id"` +} + +type UpdateFormRequestDto struct { + Status string `json:"status"` + Content models.FormContent `json:"content"` + PerformedById int64 `json:"performed_by_id"` +} + +type CreateFormSubmissionRequestDto struct { + Content models.FormContent `json:"content"` + ResponseById int64 `json:"reponse_by_id"` + FormId int64 `json:"form_id"` +} diff --git a/src/dtos/http_response.go b/src/dtos/http_response.go new file mode 100644 index 0000000..7372385 --- /dev/null +++ b/src/dtos/http_response.go @@ -0,0 +1,58 @@ +package dtos + +import ( + "time" + + "github.com/Real-Dev-Squad/wisee-backend/src/models" +) + +type ErrorResponse struct { + Message string `json:"message"` + Detail interface{} `json:"detail"` +} + +type ResponseDto struct { + Message string `json:"message"` + Data interface{} `json:"data"` + Error *ErrorResponse `json:"error"` +} + +type CreateUpdateGetFormResponseDto struct { + Id int64 `json:"id"` + Content models.FormContent `json:"content"` + OwnerId int64 `json:"owner_id"` + CreatedById int64 `json:"created_by_id"` + Status string `json:"status"` + UpdatedById *int64 `json:"updated_by_id"` + CreatedAt string `json:"created_at"` + UpdatedAt string `json:"updated_at"` +} + +type GetFormMetaDataResponseDto struct { + Id int64 `json:"id"` + FormId int64 `json:"form_id"` + IsDeleted bool `json:"is_deleted"` + AccepctingResponses bool `json:"accepting_responses"` + AllowGuestResponses bool `json:"allow_guest_responses"` + AllowMultipleRepsonses bool `json:"allow_multiple_responses"` + SendConfirmationEmailToRespondee bool `json:"send_confirmation_email_to_respondee"` + SendSubmissionEmailToOwner bool `json:"send_submission_email_to_owner"` + ValidTill time.Time `json:"valid_till"` + UpdatedById *int64 `json:"updated_by_id"` + // TODO invite code + UpdatedAt time.Time `json:"updated_at"` +} + +type GetFormsResponseDto []CreateUpdateGetFormResponseDto + +type GetFormDetailResponseDto struct { + Id int64 `json:"id"` + OwnerId int64 `json:"owner_id"` + Status string `json:"status"` + CreatedById int64 `json:"created_by_id"` + UpdatedById *int64 `json:"updated_by_id"` + CreatedAt string `json:"created_at"` + UpdatedAt string `json:"updated_at"` + Content models.FormContent `json:"content"` + Meta GetFormMetaDataResponseDto `json:"meta"` +} diff --git a/src/main.go b/src/main.go new file mode 100644 index 0000000..c0b189e --- /dev/null +++ b/src/main.go @@ -0,0 +1,19 @@ +package main + +import ( + "flag" + + "github.com/Real-Dev-Squad/wisee-backend/src/config" + "github.com/Real-Dev-Squad/wisee-backend/src/routes" + "github.com/Real-Dev-Squad/wisee-backend/src/utils" +) + +func main() { + dsn := config.DbUrl + _, bunDbInstance := utils.SetupDBConnection(dsn) + + port := flag.String("port", ":8080", "server address to listen on") + flag.Parse() + + routes.Listen("0.0.0.0"+*port, bunDbInstance) +} diff --git a/src/models/forms.go b/src/models/forms.go new file mode 100644 index 0000000..1509b60 --- /dev/null +++ b/src/models/forms.go @@ -0,0 +1,79 @@ +package models + +import ( + "time" + + "github.com/uptrace/bun" +) + +type FORM_STATUS_TYPE string + +const ( + DRAFT FORM_STATUS_TYPE = "DRAFT" + PUBLISHED FORM_STATUS_TYPE = "PUBLISHED" +) + +type FormContentKeyType string + +const FORM_CONTENT_KEY FormContentKeyType = "blocks" + +type FormContent map[FormContentKeyType][]Block + +type Form struct { + bun.BaseModel `bun:"table:forms.form"` + + Id int64 `bun:"id,pk,autoincrement" json:"id"` + Content FormContent `bun:"content" json:"content"` + CreatedById int64 `bun:"created_by_id" json:"created_by_id"` + CreatedBy *User `bun:"rel:belongs-to,join:created_by_id=id" json:"created_by"` + UpdatedById *int64 `bun:"updated_by_id,default:null" json:"updated_by_id"` + UpdatedBy *User `bun:"rel:belongs-to,join:updated_by_id=id" json:"updated_by"` + OwnerId int64 `bun:"owner_id" json:"owner_id"` + Owner *User `bun:"rel:belongs-to,join:owner_id=id" json:"owner"` + Status FORM_STATUS_TYPE `bun:"status,default:'DRAFT'" json:"status"` + CreatedAt time.Time `bun:"created_at,notnull,default:current_timestamp" json:"created_at"` + UpdatedAt time.Time `bun:"updated_at,default:null" json:"updated_at"` +} + +type FormMetaData struct { + bun.BaseModel `bun:"table:forms.metadata"` + + Id int64 `bun:"id,pk,autoincrement" json:"id"` + FormId int64 `bun:"form_id" json:"form_id"` + Form *Form `bun:"rel:belongs-to,join:form_id=id" json:"form"` + IsDeleted bool `bun:"is_deleted,default:false" json:"is_deleted"` + AccepctingResponses bool `bun:"accepting_responses,default:false" json:"accepting_responses"` + AllowGuestResponses bool `bun:"allow_guest_responses,default:true" json:"allow_guest_responses"` + AllowMultipleRepsonses bool `bun:"allow_multiple_responses,default:false" json:"allow_multiple_responses"` + SendConfirmationEmailToRespondee bool `bun:"send_confirmation_email_to_respondee,default:false" json:"send_confirmation_email_to_respondee"` + SendSubmissionEmailToOwner bool `bun:"send_submission_email_to_owner,default:false" json:"send_submission_email_to_owner"` + ValidTill time.Time `bun:"valid_till" json:"valid_till"` + UpdatedById *int64 `bun:"updated_by_id,default:null" json:"updated_by_id"` + UpdatedBy *User `bun:"rel:belongs-to,join:updated_by_id=id" json:"updated_by"` + // TODO invite code + // TODO remove created by + CreatedAt time.Time `bun:"created_at,nullzero,notnull,default:current_timestamp" json:"created_at"` + UpdatedAt time.Time `bun:"updated_at,default:null" json:"updated_at"` +} + +type FormResponse struct { + bun.BaseModel `bun:"table:form.responses"` + + Id int64 `bun:"id,pk,autoincrement" json:"id" ` + ResponseByID int64 `bun:"response_by_id" json:"response_by_id"` + ResponseBy *User `bun:"rel:belongs-to,join:response_by_id=id" json:"response_by"` + Content FormContent `bun:"content" json:"content"` + FormID int64 `bun:"form_id" json:"form_id"` + Form *Form `bun:"rel:belongs-to,join:form_id=id" json:"form"` + CreatedAt time.Time `bun:"created_at,nullzero,notnull,default:current_timestamp" json:"created_at"` + UpdatedAt time.Time `bun:"updated_at,default:null" json:"updated_at"` +} + +type Block struct { + ID string `json:"id"` + Type string `json:"type"` + Content string `json:"content"` + GroupId string `json:"group_id"` + Order int `json:"order"` + Meta interface{} `json:"meta"` +} diff --git a/src/models/users.go b/src/models/users.go new file mode 100644 index 0000000..a93f057 --- /dev/null +++ b/src/models/users.go @@ -0,0 +1,20 @@ +package models + +import ( + "time" + + "github.com/uptrace/bun" +) + +type User struct { + bun.BaseModel `bun:"table:users"` + + Id int64 `bun:"id,pk,autoincrement"` + Username string `bun:"username,notnull"` + Email string `bun:"email,unique,notnull"` + Password string `bun:"password"` + IsVerified bool `bun:"is_verified,default:false"` + IsOnboarding bool `bun:"is_onboarding,default:true"` + CreatedAt time.Time `bun:"created_at,nullzero,notnull,default:current_timestamp"` + UpdatedAt time.Time `bun:"updated_at,nullzero,notnull,default:current_timestamp"` +} diff --git a/src/routes/auth.go b/src/routes/auth.go new file mode 100644 index 0000000..3964ae9 --- /dev/null +++ b/src/routes/auth.go @@ -0,0 +1,110 @@ +package routes + +import ( + "context" + + "github.com/Real-Dev-Squad/wisee-backend/src/config" + "github.com/Real-Dev-Squad/wisee-backend/src/models" + "github.com/Real-Dev-Squad/wisee-backend/src/utils" + "github.com/Real-Dev-Squad/wisee-backend/src/utils/logger" + "github.com/gin-gonic/gin" + "github.com/uptrace/bun" + "golang.org/x/oauth2" + "golang.org/x/oauth2/google" + oauth2Api "google.golang.org/api/oauth2/v2" + "google.golang.org/api/option" +) + +func getUserInfoFromCode(code string, conf *oauth2.Config, ctx *gin.Context) (*oauth2Api.Userinfo, error) { + tok, exchangeErr := conf.Exchange(context.TODO(), code) + + if exchangeErr != nil { + return nil, exchangeErr + } + + oauth2Service, serviceError := oauth2Api.NewService(ctx, option.WithTokenSource(conf.TokenSource(ctx, tok))) + + if serviceError != nil { + return nil, serviceError + } + + userInfo, getInfoError := oauth2Service.Userinfo.Get().Context(ctx).Do() + + if getInfoError != nil { + return nil, getInfoError + } + + return userInfo, nil +} + +func AuthRoutes(reg *gin.RouterGroup, db *bun.DB) { + auth := reg.Group("/auth") + googleAuth := auth.Group("/google") + + conf := &oauth2.Config{ + ClientID: config.GoogleClientId, + ClientSecret: config.GoogleClientSecret, + RedirectURL: config.GoogleRedirectUrl, + + Scopes: []string{ + "https://www.googleapis.com/auth/userinfo.email", + "https://www.googleapis.com/auth/userinfo.profile", + }, + Endpoint: google.Endpoint, + } + + googleAuth.GET("/login", func(ctx *gin.Context) { + url := conf.AuthCodeURL("state") + ctx.Redirect(302, url) + }) + + googleAuth.GET(("/callback"), func(ctx *gin.Context) { + code := ctx.Query("code") + domain := config.Domain + authRedirectUrl := config.AuthRedirectUrl + + user := new(models.User) + googleAccountInfo, getInfoError := getUserInfoFromCode(code, conf, ctx) + + if getInfoError != nil { + logger.Fatal(getInfoError) + ctx.JSON(500, gin.H{ + "message": "error", + }) + } + + count, _ := db.NewSelect().Model(user).Where("email = ?", googleAccountInfo.Email).ScanAndCount(ctx) + + // User does not exist, create a new user + if count == 0 { + newUser := &models.User{ + Username: googleAccountInfo.Name, + Email: googleAccountInfo.Email, + } + + _, err := db.NewInsert().Model(newUser).Exec(ctx) + + if err != nil { + logger.Fatal(err) + ctx.JSON(500, gin.H{ + "message": "error", + }) + } + + user = newUser + } + + token, err := utils.GenerateToken(user) + + if err != nil { + logger.Fatal(err) + ctx.JSON(500, gin.H{ + "message": "error", + }) + } + + // set cookie and redirect + ctx.SetCookie("token", token, 3600, "/", domain, true, true) + ctx.Redirect(302, authRedirectUrl) + }) +} diff --git a/src/routes/forms.go b/src/routes/forms.go new file mode 100644 index 0000000..0435560 --- /dev/null +++ b/src/routes/forms.go @@ -0,0 +1,242 @@ +package routes + +import ( + "net/http" + + "github.com/Real-Dev-Squad/wisee-backend/src/dtos" + "github.com/Real-Dev-Squad/wisee-backend/src/models" + "github.com/gin-gonic/gin" + "github.com/uptrace/bun" +) + +func FormRoutes(rg *gin.RouterGroup, db *bun.DB) { + forms := rg.Group("/forms") + + forms.POST("", func(ctx *gin.Context) { + var requestBody dtos.CreateFormRequestDto + if err := ctx.ShouldBindJSON(&requestBody); err != nil { + errObj := dtos.ResponseDto{ + Message: "invalid request", + Error: &dtos.ErrorResponse{ + Message: "invalid request body", + Detail: err.Error(), + }, + } + ctx.JSON(http.StatusBadRequest, errObj) + return + } + + var form = &models.Form{ + Content: requestBody.Content, + CreatedById: requestBody.PerformedById, + Status: models.DRAFT, + OwnerId: requestBody.PerformedById, + } + + if _, err := db.NewInsert().Model(form).Exec(ctx); err != nil { + errObj := dtos.ResponseDto{ + Message: "something went wrong", + Error: &dtos.ErrorResponse{ + Message: "error creating form", + Detail: err.Error(), + }, + } + ctx.JSON(http.StatusBadRequest, errObj) + return + } + + var FormMetaData = &models.FormMetaData{ + FormId: form.Id, + } + + if _, err := db.NewInsert().Model(FormMetaData).Exec(ctx); err != nil { + errObj := dtos.ResponseDto{ + Message: "something went wrong", + Error: &dtos.ErrorResponse{ + Message: "error creating form meta data", + Detail: err.Error(), + }, + } + ctx.JSON(http.StatusBadRequest, errObj) + return + } + + var resData = dtos.CreateUpdateGetFormResponseDto{ + Id: form.Id, + Content: form.Content, + OwnerId: form.OwnerId, + CreatedById: form.CreatedById, + Status: string(form.Status), + CreatedAt: form.CreatedAt.String(), + UpdatedAt: form.UpdatedAt.String(), + } + + resObj := dtos.ResponseDto{ + Message: "form created successfully", + Data: resData, + } + + ctx.JSON(http.StatusCreated, resObj) + }) + + forms.GET("", func(ctx *gin.Context) { + var form []models.Form + if err := db.NewSelect().Model(&form).OrderExpr("id ASC").Scan(ctx); err != nil { + errObj := dtos.ResponseDto{ + Message: "something went wrong", + Error: &dtos.ErrorResponse{ + Message: "error fetching forms", + Detail: err.Error(), + }, + } + ctx.JSON(http.StatusBadRequest, errObj) + return + } + + var resData dtos.GetFormsResponseDto + for _, f := range form { + resData = append(resData, dtos.CreateUpdateGetFormResponseDto{ + Id: f.Id, + Content: f.Content, + OwnerId: f.OwnerId, + CreatedById: f.CreatedById, + UpdatedById: f.UpdatedById, + Status: string(f.Status), + CreatedAt: f.CreatedAt.String(), + UpdatedAt: f.UpdatedAt.String(), + }) + } + + var res = dtos.ResponseDto{ + Message: "forms fetched successfully", + Data: resData, + } + + ctx.JSON(http.StatusOK, res) + }) + + forms.GET("/:id", func(ctx *gin.Context) { + var formMetaData models.FormMetaData + var form models.Form + + query := db.NewSelect().Model(&formMetaData).Relation("Form").Where("form_id = ?", ctx.Param("id")) + if err := query.Scan(ctx); err != nil { + errObj := dtos.ResponseDto{ + Message: "something went wrong", + Error: &dtos.ErrorResponse{ + Message: "error fetching form", + Detail: err.Error(), + }, + } + ctx.JSON(http.StatusBadRequest, errObj) + return + } + + form = *formMetaData.Form + + var resData = dtos.GetFormDetailResponseDto{ + Id: form.Id, + OwnerId: form.OwnerId, + Status: string(form.Status), + CreatedById: form.CreatedById, + UpdatedById: form.UpdatedById, + CreatedAt: form.CreatedAt.String(), + UpdatedAt: form.UpdatedAt.String(), + Content: form.Content, + Meta: dtos.GetFormMetaDataResponseDto{ + Id: formMetaData.Id, + FormId: formMetaData.FormId, + IsDeleted: formMetaData.IsDeleted, + AccepctingResponses: formMetaData.AccepctingResponses, + AllowGuestResponses: formMetaData.AllowGuestResponses, + AllowMultipleRepsonses: formMetaData.AllowMultipleRepsonses, + SendConfirmationEmailToRespondee: formMetaData.SendConfirmationEmailToRespondee, + SendSubmissionEmailToOwner: formMetaData.SendSubmissionEmailToOwner, + ValidTill: formMetaData.ValidTill, + UpdatedById: formMetaData.UpdatedById, + UpdatedAt: formMetaData.UpdatedAt, + }, + } + + var res = dtos.ResponseDto{ + Message: "form fetched successfully", + Data: resData, + } + + ctx.JSON(http.StatusOK, res) + }) + + forms.PATCH("/:id", func(ctx *gin.Context) { + var requestBody dtos.UpdateFormRequestDto + if err := ctx.ShouldBindJSON(&requestBody); err != nil { + errObj := dtos.ResponseDto{ + Message: "invalid request", + Error: &dtos.ErrorResponse{ + Message: "invalid request body", + Detail: err.Error(), + }, + } + ctx.JSON(http.StatusBadRequest, errObj) + return + } + + var form models.Form + if err := db.NewSelect().Model(&form).Where("id = ?", ctx.Param("id")).Scan(ctx); err != nil { + errObj := dtos.ResponseDto{ + Message: "something went wrong", + Error: &dtos.ErrorResponse{ + Message: "error fetching form", + Detail: err.Error(), + }, + } + ctx.JSON(http.StatusBadRequest, errObj) + return + } + + if requestBody.Status != string(models.DRAFT) && requestBody.Status != string(models.PUBLISHED) { + errObj := dtos.ResponseDto{ + Message: "invalid request", + Error: &dtos.ErrorResponse{ + Message: "invalid status", + }, + } + ctx.JSON(http.StatusBadRequest, errObj) + return + } + + form.Content = requestBody.Content + form.Status = models.FORM_STATUS_TYPE(requestBody.Status) + form.OwnerId = requestBody.PerformedById + form.UpdatedById = &requestBody.PerformedById + + if _, err := db.NewUpdate().Model(&form).Where("id = ?", ctx.Param("id")).Exec(ctx); err != nil { + errObj := dtos.ResponseDto{ + Message: "something went wrong", + Error: &dtos.ErrorResponse{ + Message: "error updating form", + Detail: err.Error(), + }, + } + ctx.JSON(http.StatusBadRequest, errObj) + return + } + + var resData = dtos.CreateUpdateGetFormResponseDto{ + Id: form.Id, + Content: form.Content, + OwnerId: form.OwnerId, + CreatedById: form.CreatedById, + Status: string(form.Status), + CreatedAt: form.CreatedAt.String(), + UpdatedAt: form.UpdatedAt.String(), + UpdatedById: form.UpdatedById, + } + + resObj := dtos.ResponseDto{ + Message: "form updated successfully", + Data: resData, + } + + ctx.JSON(http.StatusAccepted, resObj) + }) +} diff --git a/src/routes/health.go b/src/routes/health.go new file mode 100644 index 0000000..6e00394 --- /dev/null +++ b/src/routes/health.go @@ -0,0 +1,30 @@ +package routes + +import ( + "net/http" + + "github.com/Real-Dev-Squad/wisee-backend/src/utils/logger" + "github.com/gin-gonic/gin" + "github.com/uptrace/bun" +) + +func HealthRoutes(rg *gin.RouterGroup, db *bun.DB) { + healthCheck := rg.Group("/health") + + healthCheck.GET("", func(ctx *gin.Context) { + err := db.Ping() + + if err != nil { + ctx.JSON(http.StatusInternalServerError, gin.H{ + "message": "error", + }) + + logger.Error(err) + return + } + + ctx.JSON(http.StatusOK, gin.H{ + "message": "OK", + }) + }) +} diff --git a/src/routes/main.go b/src/routes/main.go new file mode 100644 index 0000000..4175f97 --- /dev/null +++ b/src/routes/main.go @@ -0,0 +1,28 @@ +package routes + +import ( + "github.com/gin-contrib/cors" + "github.com/gin-gonic/gin" + "github.com/uptrace/bun" +) + +func SetupV1Routes(db *bun.DB) *gin.Engine { + var router = gin.Default() + + // TODO: Configure CORS properly to allow only access from certain origins + router.Use(cors.Default()) + + v1 := router.Group("wisee/v1/") + UserRoutes(v1, db) + AuthRoutes(v1, db) + FormRoutes(v1, db) + HealthRoutes(v1, db) + + return router +} + +func Listen(listenAddress string, db *bun.DB) { + router := SetupV1Routes(db) + + router.Run(listenAddress) +} diff --git a/src/routes/users.go b/src/routes/users.go new file mode 100644 index 0000000..96853e4 --- /dev/null +++ b/src/routes/users.go @@ -0,0 +1,31 @@ +package routes + +import ( + "net/http" + + "github.com/Real-Dev-Squad/wisee-backend/src/models" + "github.com/gin-gonic/gin" + "github.com/uptrace/bun" +) + +func UserRoutes(rg *gin.RouterGroup, db *bun.DB) { + users := rg.Group("/users") + + users.GET("", func(ctx *gin.Context) { + + var users []models.User + err := db.NewSelect().Model(&users).OrderExpr("id ASC").Limit(10).Scan(ctx) + + if err != nil { + ctx.JSON(http.StatusInternalServerError, gin.H{ + "message": "error", + }) + return + } + + ctx.JSON(http.StatusOK, gin.H{ + "message": "users fetched successfully", + "data": users, + }) + }) +} diff --git a/src/utils/db.go b/src/utils/db.go new file mode 100644 index 0000000..939322b --- /dev/null +++ b/src/utils/db.go @@ -0,0 +1,27 @@ +package utils + +import ( + "database/sql" + + "github.com/Real-Dev-Squad/wisee-backend/src/config" + "github.com/uptrace/bun" + "github.com/uptrace/bun/dialect/pgdialect" + "github.com/uptrace/bun/driver/pgdriver" + "github.com/uptrace/bun/extra/bundebug" +) + +func SetupDBConnection(dsn string) (*sql.DB, *bun.DB) { + maxOpenConnections := config.DbMaxOpenConnections + + pgDB := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn))) + pgDB.SetMaxOpenConns(maxOpenConnections) + + bunDbInstance := bun.NewDB(pgDB, pgdialect.New()) + + bunDbInstance.AddQueryHook(bundebug.NewQueryHook( + bundebug.WithVerbose(true), + bundebug.FromEnv("BUNDEBUG"), + )) + + return pgDB, bunDbInstance +} diff --git a/src/utils/jwt.go b/src/utils/jwt.go new file mode 100644 index 0000000..e830396 --- /dev/null +++ b/src/utils/jwt.go @@ -0,0 +1,70 @@ +package utils + +import ( + "errors" + "time" + + "github.com/Real-Dev-Squad/wisee-backend/src/config" + "github.com/Real-Dev-Squad/wisee-backend/src/models" + "github.com/golang-jwt/jwt/v5" +) + +var jwtSecret = config.JwtSecret +var jwtValidityInDays = config.JwtValidityInDays + +/* + * GenerateToken generates a JWT token for the user + */ +func GenerateToken(user *models.User) (string, error) { + issuer := config.JwtIssuer + key := []byte(jwtSecret) + + t := jwt.NewWithClaims(jwt.SigningMethodHS512, jwt.MapClaims{ + "iss": issuer, + "email": user.Email, + "iat": jwt.NewNumericDate(time.Now()), + "exp": jwt.NewNumericDate(time.Now().AddDate(0, 0, jwtValidityInDays)), + }) + + token, error := t.SignedString(key) + + return token, error +} + +/* + * VerifyToken verifies the token and returns the email of the user + */ +func VerifyToken(tokenString string) (string, error) { + token, tokenErr := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { + if token.Method.Alg() != jwt.SigningMethodHS512.Alg() { + return nil, jwt.ErrSignatureInvalid + } + + return []byte(jwtSecret), nil + }) + + if tokenErr != nil || !token.Valid { + return "", tokenErr + } + + claims, ok := token.Claims.(jwt.MapClaims) + if !ok { + return "", errors.New("token claims are not in expected format") + } + + exp, err := claims.GetExpirationTime() + if err != nil { + return "", err + } + + if exp.Before(time.Now().UTC()) { + return "", errors.New("token has expired") + } + + email, ok := claims["email"].(string) + if !ok { + return "", errors.New("email not found in token") + } + + return email, nil +} diff --git a/src/utils/logger/logger.go b/src/utils/logger/logger.go new file mode 100644 index 0000000..9a6c9fb --- /dev/null +++ b/src/utils/logger/logger.go @@ -0,0 +1,70 @@ +// inspired by: https://github.com/championswimmer/onepixel_backend/blob/1ed767e2244832bf8e50be1eaa3d4f2edb7e1190/src/utils/applogger/app_logger.go + +package logger + +import ( + "log" + "os" + "runtime" +) + +const ( + _reset = "\033[0m" + _red = "\033[31m" + _green = "\033[32m" + _yellow = "\033[33m" + _blue = "\033[34m" + _magenta = "\033[35m" + _cyan = "\033[36m" + _white = "\033[37m" + _redbold = "\033[31;1m" + _greenbold = "\033[32;1m" + _yellowbold = "\033[33;1m" + _bluebold = "\033[34;1m" + _magentabold = "\033[35;1m" + _cyanbold = "\033[36;1m" +) + +var logFlags = log.LstdFlags | log.LUTC | log.Lmsgprefix | log.Lshortfile +var ( + traceLogger = log.New(os.Stdout, _cyanbold+"[TRACE] "+_reset, logFlags) + debugLogger = log.New(os.Stdout, _bluebold+"[DEBUG] "+_reset, logFlags) + infoLogger = log.New(os.Stdout, _greenbold+"[INFO] "+_reset, logFlags) + warnLogger = log.New(os.Stdout, _yellowbold+"[WARN] "+_reset, logFlags) + errorLogger = log.New(os.Stderr, _redbold+"[ERROR] "+_reset, logFlags) + fatalLogger = log.New(os.Stderr, _magentabold+"[FATAL] "+_reset, logFlags) + panicLogger = log.New(os.Stderr, _magentabold+"[PANIC] "+_reset, logFlags) +) + +func Trace(v ...interface{}) { + traceLogger.Println(v...) +} + +func Debug(v ...interface{}) { + debugLogger.Println(v...) +} + +func Info(v ...interface{}) { + infoLogger.Println(v...) + +} + +func Warn(v ...interface{}) { + warnLogger.Println(v...) + +} + +func Error(v ...interface{}) { + errorLogger.Println(v...) +} + +func Fatal(v ...interface{}) { + _, file, line, _ := runtime.Caller(1) + fatalLogger.Printf("%s:%d: %v\n", file, line, v) + os.Exit(1) +} + +func Panic(v ...interface{}) { + panicLogger.Println(v...) + panic(v) +} diff --git a/tests/integration/form_test.go b/tests/integration/form_test.go new file mode 100644 index 0000000..1416fd2 --- /dev/null +++ b/tests/integration/form_test.go @@ -0,0 +1,269 @@ +package integration_tests + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/Real-Dev-Squad/wisee-backend/src/dtos" + "github.com/Real-Dev-Squad/wisee-backend/src/models" + "github.com/Real-Dev-Squad/wisee-backend/src/routes" +) + +func TestFormCreation(t *testing.T) { + router := routes.SetupV1Routes(db) + // add the DTO + var requestBody = map[string]interface{}{ + "status": models.DRAFT, + "performed_by_id": user.Id, + "content": models.FormContent{"blocks": []models.Block{{ID: "1", Type: "text", Content: "Hello World", GroupId: "1", Meta: nil, Order: 1}}}, + } + + // Convert requestBody to JSON + jsonValue, _ := json.Marshal(requestBody) + + w := httptest.NewRecorder() + req, err := http.NewRequest("POST", "/wisee/v1/forms", bytes.NewBuffer(jsonValue)) + + router.ServeHTTP(w, req) + + if err != nil { + t.Fatal(err) + } + + var respBody TestResponseDto + if err := json.NewDecoder(w.Body).Decode(&respBody); err != nil { + t.Fatal(err) + } + + var resData dtos.CreateUpdateGetFormResponseDto + if err := json.Unmarshal(respBody.Data, &resData); err != nil { + t.Fatal(err) + } + + assert.Equal(t, http.StatusCreated, w.Code) + assert.Equal(t, "form created successfully", respBody.Message) + + var ctx = context.Background() + var formMetaData models.FormMetaData + if err := db.NewSelect().Model(&formMetaData).Where("form_id = ?", resData.Id).Scan(ctx); err != nil { + t.Fatal(err) + } + + assert.Equal(t, resData.Id, formMetaData.FormId) +} + +func TestFormCreationNoPerformedById(t *testing.T) { + router := routes.SetupV1Routes(db) + // add the DTO + var requestBody = map[string]interface{}{ + "content": models.FormContent{"blocks": []models.Block{{ID: "1", Type: "text", Content: "Hello World", GroupId: "1", Meta: nil, Order: 1}}}, + } + + // Convert requestBody to JSON + jsonValue, _ := json.Marshal(requestBody) + + w := httptest.NewRecorder() + req, err := http.NewRequest("POST", "/wisee/v1/forms", bytes.NewBuffer(jsonValue)) + + router.ServeHTTP(w, req) + + if err != nil { + t.Fatal(err) + } + + var respBody TestResponseDto + if err := json.NewDecoder(w.Body).Decode(&respBody); err != nil { + t.Fatal(err) + } + + var resError TestResponseDto + if err := json.Unmarshal(respBody.Data, &resError); err != nil { + t.Fatal(err) + } + + assert.Equal(t, http.StatusBadRequest, w.Code) + assert.Equal(t, "something went wrong", respBody.Message) +} + +func TestFormGetAll(t *testing.T) { + router := routes.SetupV1Routes(db) + w := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/wisee/v1/forms", nil) + + router.ServeHTTP(w, req) + + if err != nil { + t.Fatal(err) + } + + var respBody TestResponseDto + if err := json.NewDecoder(w.Body).Decode(&respBody); err != nil { + t.Fatal(err) + } + + var resData dtos.GetFormsResponseDto + if err := json.Unmarshal(respBody.Data, &resData); err != nil { + t.Fatal(err) + } + + assert.Equal(t, http.StatusOK, w.Code) + assert.Equal(t, "forms fetched successfully", respBody.Message) + assert.LessOrEqual(t, 1, len(resData)) +} + +func TestFormGetById(t *testing.T) { + router := routes.SetupV1Routes(db) + w := httptest.NewRecorder() + req, err := http.NewRequest("GET", fmt.Sprintf("/wisee/v1/forms/%v", form.Id), nil) + + router.ServeHTTP(w, req) + if err != nil { + t.Fatal(err) + } + + var respBody TestResponseDto + if err := json.NewDecoder(w.Body).Decode(&respBody); err != nil { + t.Fatal(err) + } + + var resData dtos.GetFormDetailResponseDto + if err := json.Unmarshal(respBody.Data, &resData); err != nil { + t.Fatal(err) + } + + assert.Equal(t, http.StatusOK, w.Code) + assert.Equal(t, "form fetched successfully", respBody.Message) + assert.Equal(t, form.Id, resData.Id) + assert.Equal(t, form.OwnerId, resData.OwnerId) + assert.Equal(t, string(form.Status), resData.Status) + assert.Equal(t, form.CreatedById, resData.CreatedById) + assert.Equal(t, formMetaData.Id, resData.Meta.Id) + assert.Equal(t, formMetaData.FormId, resData.Meta.FormId) +} +func TestFormGetByInvalidId(t *testing.T) { + router := routes.SetupV1Routes(db) + w := httptest.NewRecorder() + req, err := http.NewRequest("GET", fmt.Sprintf("/wisee/v1/forms/%v", 1526), nil) + + router.ServeHTTP(w, req) + if err != nil { + t.Fatal(err) + } + + var respBody TestResponseDto + if err := json.NewDecoder(w.Body).Decode(&respBody); err != nil { + t.Fatal(err) + } + + var resData dtos.GetFormDetailResponseDto + if err := json.Unmarshal(respBody.Data, &resData); err != nil { + t.Fatal(err) + } + + assert.Equal(t, http.StatusBadRequest, w.Code) + assert.Contains(t, respBody.Error.Detail, "no rows in result set") +} + +func TestFormUpdate(t *testing.T) { + assert.Nil(t, form.UpdatedById) + + router := routes.SetupV1Routes(db) + + // add the DTO + var requestBody = map[string]interface{}{ + "status": models.DRAFT, + "performed_by_id": user.Id, + "content": models.FormContent{"blocks": []models.Block{{ID: "1", Type: "text", Content: "Hello World", GroupId: "1", Meta: nil, Order: 1}}}, + } + + // Convert requestBody to JSON + jsonValue, _ := json.Marshal(requestBody) + + w := httptest.NewRecorder() + req, err := http.NewRequest("PATCH", fmt.Sprintf("/wisee/v1/forms/%v", form.Id), bytes.NewBuffer(jsonValue)) + + router.ServeHTTP(w, req) + if err != nil { + t.Fatal(err) + } + + var respBody TestResponseDto + if err := json.NewDecoder(w.Body).Decode(&respBody); err != nil { + t.Fatal(err) + } + + var resData dtos.CreateUpdateGetFormResponseDto + if err := json.Unmarshal(respBody.Data, &resData); err != nil { + t.Fatal(err) + } + + assert.Equal(t, http.StatusAccepted, w.Code) + assert.Equal(t, "form updated successfully", respBody.Message) + assert.Equal(t, user.Id, *resData.UpdatedById) +} + +func TestFormUpdateInavlidStatus(t *testing.T) { + router := routes.SetupV1Routes(db) + + // add the DTO + var requestBody = map[string]interface{}{ + "status": "random", + "performed_by_id": user.Id, + "content": models.FormContent{"blocks": []models.Block{{ID: "1", Type: "text", Content: "Hello World", GroupId: "1", Meta: nil, Order: 1}}}, + } + + // Convert requestBody to JSON + jsonValue, _ := json.Marshal(requestBody) + + w := httptest.NewRecorder() + req, err := http.NewRequest("PATCH", fmt.Sprintf("/wisee/v1/forms/%v", form.Id), bytes.NewBuffer(jsonValue)) + + router.ServeHTTP(w, req) + if err != nil { + t.Fatal(err) + } + + var respBody TestResponseDto + if err := json.NewDecoder(w.Body).Decode(&respBody); err != nil { + t.Fatal(err) + } + + assert.Equal(t, http.StatusBadRequest, w.Code) + assert.Equal(t, "invalid status", respBody.Error.Message) +} +func TestFormUpdateInavlidFormId(t *testing.T) { + router := routes.SetupV1Routes(db) + + // add the DTO + var requestBody = map[string]interface{}{ + "status": "DRAFT", + "performed_by_id": user.Id, + "content": models.FormContent{"blocks": []models.Block{{ID: "1", Type: "text", Content: "Hello World", GroupId: "1", Meta: nil, Order: 1}}}, + } + + // Convert requestBody to JSON + jsonValue, _ := json.Marshal(requestBody) + + w := httptest.NewRecorder() + req, err := http.NewRequest("PATCH", fmt.Sprintf("/wisee/v1/forms/%v", 15668), bytes.NewBuffer(jsonValue)) + + router.ServeHTTP(w, req) + if err != nil { + t.Fatal(err) + } + + var respBody TestResponseDto + if err := json.NewDecoder(w.Body).Decode(&respBody); err != nil { + t.Fatal(err) + } + + assert.Equal(t, http.StatusBadRequest, w.Code) + assert.Contains(t, respBody.Error.Detail, "no rows in result set") +} diff --git a/tests/integration/main_test.go b/tests/integration/main_test.go new file mode 100644 index 0000000..847dd1c --- /dev/null +++ b/tests/integration/main_test.go @@ -0,0 +1,53 @@ +package integration_tests + +import ( + "testing" + + _ "github.com/lib/pq" + + "github.com/Real-Dev-Squad/wisee-backend/src/config" + "github.com/Real-Dev-Squad/wisee-backend/src/utils" + "github.com/Real-Dev-Squad/wisee-backend/src/utils/logger" + "github.com/uptrace/bun" + + "github.com/golang-migrate/migrate/v4" + "github.com/golang-migrate/migrate/v4/database/postgres" + _ "github.com/golang-migrate/migrate/v4/source/file" +) + +var db *bun.DB + +func TestMain(m *testing.M) { + dsn := config.TestDbUrl + db, bunDb := utils.SetupDBConnection(dsn) + defer bunDb.Close() + + // use the db connection from the `setupDBConnection` function to run migrations + driver, pgInstanceErr := postgres.WithInstance(db, &postgres.Config{}) + + if pgInstanceErr != nil { + logger.Fatal("pg instance error: ", pgInstanceErr) + } + + migration, err := migrate.NewWithDatabaseInstance( + "file://database/migrations", // "file://" + path of the migrations folder (not using "file://" will throw an error) + "postgres", driver) + + if err != nil { + logger.Fatal("migrate: database instance error: ", err) + } + + if err := migration.Up(); err != nil { + logger.Fatal("migration error: ", err) + } + + logger.Info("Migrations complete") + + // teardown the database after the tests + defer TeardownDb(migration) + + // setup fixtures + if err := SetupFixtures(bunDb); err != nil { + logger.Fatal("Error setting up fixtures:", err) + } +} diff --git a/tests/integration/users_test.go b/tests/integration/users_test.go new file mode 100644 index 0000000..4486586 --- /dev/null +++ b/tests/integration/users_test.go @@ -0,0 +1,26 @@ +package integration_tests + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/Real-Dev-Squad/wisee-backend/src/routes" +) + +func TestGetUsers(t *testing.T) { + router := routes.SetupV1Routes(db) + + w := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/wisee/v1/users", nil) + + router.ServeHTTP(w, req) + + if err != nil { + t.Fatal(err) + } + + if w.Code != http.StatusOK { + t.Errorf("Expected status code %d but got %d", http.StatusOK, w.Code) + } +} diff --git a/tests/integration/utils_test.go b/tests/integration/utils_test.go new file mode 100644 index 0000000..6a89ad4 --- /dev/null +++ b/tests/integration/utils_test.go @@ -0,0 +1,59 @@ +package integration_tests + +import ( + "context" + "encoding/json" + + "github.com/Real-Dev-Squad/wisee-backend/src/dtos" + "github.com/Real-Dev-Squad/wisee-backend/src/models" + "github.com/Real-Dev-Squad/wisee-backend/src/utils/logger" + "github.com/golang-migrate/migrate/v4" + "github.com/uptrace/bun" +) + +var user *models.User +var form *models.Form +var formMetaData *models.FormMetaData + +type TestResponseDto struct { + Message string `json:"message"` + Data json.RawMessage `json:"data"` + Error *dtos.ErrorResponse `json:"error"` +} + +func SetupFixtures(db *bun.DB) error { + var ctx = context.Background() + logger.Info("setting up fixtures") + defer logger.Info("fixtures setup complete") + + userFixture := &models.User{Username: "test_user", Email: "test_user@admin.com", Password: "password"} + if _, err := db.NewInsert().Model(userFixture).Exec(ctx); err != nil { + return err + } + + formFixture := &models.Form{OwnerId: userFixture.Id, Status: models.DRAFT, Content: models.FormContent{"blocks": []models.Block{{ID: "1", Type: "text", Content: "Hello World", GroupId: "1", Meta: nil, Order: 1}}}, CreatedById: userFixture.Id} + if _, err := db.NewInsert().Model(formFixture).Exec(ctx); err != nil { + return err + } + + formMetaDataFixture := &models.FormMetaData{ + FormId: formFixture.Id, + } + if _, err := db.NewInsert().Model(formMetaDataFixture).Exec(ctx); err != nil { + return err + } + + user = userFixture + form = formFixture + formMetaData = formMetaDataFixture + return nil +} + +func TeardownDb(migrate *migrate.Migrate) { + logger.Info("Running migration down") + defer logger.Info("Migration down complete") + + if err := migrate.Down(); err != nil { + logger.Fatal("failed to run migration down: ", err) + } +} diff --git a/tests/unit/jwt_test.go b/tests/unit/jwt_test.go new file mode 100644 index 0000000..648c203 --- /dev/null +++ b/tests/unit/jwt_test.go @@ -0,0 +1,63 @@ +package unit + +import ( + "os" + "testing" + + "github.com/Real-Dev-Squad/wisee-backend/src/config" + "github.com/Real-Dev-Squad/wisee-backend/src/models" + "github.com/Real-Dev-Squad/wisee-backend/src/utils" + "github.com/Real-Dev-Squad/wisee-backend/src/utils/logger" +) + +func TestMain(m *testing.M) { + code := m.Run() + logger.Info(config.JwtValidityInDays) + + os.Exit(code) +} + +func TestGenerateJWT(t *testing.T) { + dummyUser := &models.User{ + Email: "test@gmail.com", + } + + token, err := utils.GenerateToken(dummyUser) + + if err != nil { + t.Fatalf("Expected nil but got %v", err) + } + + if len(token) == 0 { + t.Fatalf("Empty token of length") + } +} + +func TestVerifyJWT(t *testing.T) { + dummyUser := &models.User{ + Email: "test@gmail.com", + } + + validToken, generateTokenError := utils.GenerateToken(dummyUser) + expiredToken := "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InRlc3RAZ21haWwuY29tIiwiZXhwIjoiMjAyMy0xMC0wMVQxOTo1Njo0OS4zOTc5NzEyWiIsImlzcyI6Indpc2VlLWJhY2tlbmQifQ.h11JtaPg-ITKR8UXTyz_Q7pJU_3gYyXwIkqX7lI1UK2nVkvxQvkyN23-u3wj8fV5mNIvp-ePTOp-7odsPcGC_g" + + if generateTokenError != nil { + t.Fatalf("Error: %v", generateTokenError) + } + + _, expiredTokenError := utils.VerifyToken(expiredToken) + + if expiredTokenError == nil { + t.Fatalf("Expected error but got nil") + } + + email, validTokenError := utils.VerifyToken(validToken) + + if validTokenError != nil { + t.Fatalf("Error: %v", validTokenError) + } + + if email != dummyUser.Email { + t.Fatalf("Expected %v but got %v", dummyUser.Email, email) + } +} diff --git a/wisee_backend.postman_collection b/wisee_backend.postman_collection new file mode 100644 index 0000000..f2368c1 --- /dev/null +++ b/wisee_backend.postman_collection @@ -0,0 +1,297 @@ +{ + "info": { + "_postman_id": "4d36e291-e3e8-4360-991d-824d36792aec", + "name": "Wisee_backend", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", + "_exporter_id": "26854281" + }, + "item": [ + { + "name": "Create Form", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\r\n \"content\": {\r\n \"blocks\": [\r\n {\r\n \"id\": \"1\",\r\n \"type\": \"text\",\r\n \"content\": \"Hello World\",\r\n \"group_id\": \"1\",\r\n \"order\": 1\r\n }\r\n ]\r\n },\r\n \"performed_by_id\": 3\r\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{BASE_URL}}/v1/forms", + "host": [ + "{{BASE_URL}}" + ], + "path": [ + "v1", + "forms" + ] + } + }, + "response": [ + { + "name": "create form success", + "originalRequest": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\r\n \"content\": {\r\n \"blocks\": [\r\n {\r\n \"id\": \"1\",\r\n \"type\": \"text\",\r\n \"content\": \"Hello World\",\r\n \"group_id\": \"1\",\r\n \"order\": 1\r\n }\r\n ]\r\n },\r\n \"performed_by_id\": 3\r\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{BASE_URL}}/v1/forms", + "host": [ + "{{BASE_URL}}" + ], + "path": [ + "v1", + "forms" + ] + } + }, + "status": "Created", + "code": 201, + "_postman_previewlanguage": "json", + "header": [ + { + "key": "Content-Type", + "value": "application/json; charset=utf-8" + }, + { + "key": "Date", + "value": "Sat, 17 Feb 2024 17:48:19 GMT" + }, + { + "key": "Content-Length", + "value": "343" + } + ], + "cookie": [], + "body": "{\n \"message\": \"form created successfully\",\n \"data\": {\n \"id\": 1,\n \"content\": {\n \"blocks\": [\n {\n \"id\": \"1\",\n \"type\": \"text\",\n \"content\": \"Hello World\",\n \"group_id\": \"1\",\n \"order\": 1,\n \"meta\": null\n }\n ]\n },\n \"owner_id\": 3,\n \"created_by_id\": 3,\n \"status\": \"DRAFT\",\n \"updated_by_id\": null,\n \"created_at\": \"2024-02-17 17:48:19.565039 +0000 UTC\",\n \"updated_at\": \"0001-01-01 00:00:00 +0000 UTC\"\n },\n \"error\": null\n}" + } + ] + }, + { + "name": "Update Form", + "request": { + "method": "PATCH", + "header": [], + "body": { + "mode": "raw", + "raw": "{\r\n \"content\": {\r\n \"blocks\": [\r\n {\r\n \"id\": \"1\",\r\n \"type\": \"text\",\r\n \"content\": \"Hello World\",\r\n \"group_id\": \"1\",\r\n \"order\": 1\r\n },\r\n {\r\n \"id\": \"2\",\r\n \"type\": \"text\",\r\n \"content\": \"Hello World again\",\r\n \"group_id\": \"1\",\r\n \"order\": 3\r\n }\r\n ]\r\n },\r\n \"performed_by_id\": 3,\r\n \"status\": \"PUBLISHED\"\r\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{BASE_URL}}/v1/forms/1", + "host": [ + "{{BASE_URL}}" + ], + "path": [ + "v1", + "forms", + "1" + ] + } + }, + "response": [ + { + "name": "Update Form", + "originalRequest": { + "method": "PATCH", + "header": [], + "body": { + "mode": "raw", + "raw": "{\r\n \"content\": {\r\n \"blocks\": [\r\n {\r\n \"id\": \"1\",\r\n \"type\": \"text\",\r\n \"content\": \"Hello World\",\r\n \"group_id\": \"1\",\r\n \"order\": 1\r\n },\r\n {\r\n \"id\": \"2\",\r\n \"type\": \"text\",\r\n \"content\": \"Hello World again\",\r\n \"group_id\": \"1\",\r\n \"order\": 3\r\n }\r\n ]\r\n },\r\n \"performed_by_id\": 3,\r\n \"status\": \"PUBLISHED\"\r\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{BASE_URL}}/v1/forms/1", + "host": [ + "{{BASE_URL}}" + ], + "path": [ + "v1", + "forms", + "1" + ] + } + }, + "status": "Accepted", + "code": 202, + "_postman_previewlanguage": "json", + "header": [ + { + "key": "Content-Type", + "value": "application/json; charset=utf-8" + }, + { + "key": "Date", + "value": "Sat, 17 Feb 2024 17:51:11 GMT" + }, + { + "key": "Content-Length", + "value": "436" + } + ], + "cookie": [], + "body": "{\n \"message\": \"form updated successfully\",\n \"data\": {\n \"id\": 1,\n \"content\": {\n \"blocks\": [\n {\n \"id\": \"1\",\n \"type\": \"text\",\n \"content\": \"Hello World\",\n \"group_id\": \"1\",\n \"order\": 1,\n \"meta\": null\n },\n {\n \"id\": \"2\",\n \"type\": \"text\",\n \"content\": \"Hello World again\",\n \"group_id\": \"1\",\n \"order\": 3,\n \"meta\": null\n }\n ]\n },\n \"owner_id\": 3,\n \"created_by_id\": 3,\n \"status\": \"PUBLISHED\",\n \"updated_by_id\": 3,\n \"created_at\": \"2024-02-17 17:48:19.565039 +0000 UTC\",\n \"updated_at\": \"0001-01-01 00:00:00 +0000 UTC\"\n },\n \"error\": null\n}" + } + ] + }, + { + "name": "Get all forms", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "{{BASE_URL}}/v1/forms", + "host": [ + "{{BASE_URL}}" + ], + "path": [ + "v1", + "forms" + ] + } + }, + "response": [ + { + "name": "get all forms success", + "originalRequest": { + "method": "GET", + "header": [], + "url": { + "raw": "{{BASE_URL}}/v1/forms", + "host": [ + "{{BASE_URL}}" + ], + "path": [ + "v1", + "forms" + ] + } + }, + "status": "OK", + "code": 200, + "_postman_previewlanguage": "json", + "header": [ + { + "key": "Content-Type", + "value": "application/json; charset=utf-8" + }, + { + "key": "Date", + "value": "Sat, 17 Feb 2024 17:48:57 GMT" + }, + { + "key": "Content-Length", + "value": "346" + } + ], + "cookie": [], + "body": "{\n \"message\": \"forms fetched successfully\",\n \"data\": [\n {\n \"id\": 1,\n \"content\": {\n \"blocks\": [\n {\n \"id\": \"1\",\n \"type\": \"text\",\n \"content\": \"Hello World\",\n \"group_id\": \"1\",\n \"order\": 1,\n \"meta\": null\n }\n ]\n },\n \"owner_id\": 3,\n \"created_by_id\": 3,\n \"status\": \"DRAFT\",\n \"updated_by_id\": null,\n \"created_at\": \"2024-02-17 17:48:19.565039 +0000 UTC\",\n \"updated_at\": \"0001-01-01 00:00:00 +0000 UTC\"\n }\n ],\n \"error\": null\n}" + } + ] + }, + { + "name": "Get form by id", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "{{BASE_URL}}/v1/forms/1", + "host": [ + "{{BASE_URL}}" + ], + "path": [ + "v1", + "forms", + "1" + ] + } + }, + "response": [ + { + "name": "get form by id success", + "originalRequest": { + "method": "GET", + "header": [], + "url": { + "raw": "{{BASE_URL}}/v1/forms/1", + "host": [ + "{{BASE_URL}}" + ], + "path": [ + "v1", + "forms", + "1" + ] + } + }, + "status": "OK", + "code": 200, + "_postman_previewlanguage": "json", + "header": [ + { + "key": "Content-Type", + "value": "application/json; charset=utf-8" + }, + { + "key": "Date", + "value": "Sat, 17 Feb 2024 17:49:31 GMT" + }, + { + "key": "Content-Length", + "value": "657" + } + ], + "cookie": [], + "body": "{\n \"message\": \"form fetched successfully\",\n \"data\": {\n \"id\": 1,\n \"owner_id\": 3,\n \"status\": \"DRAFT\",\n \"created_by_id\": 3,\n \"updated_by_id\": null,\n \"created_at\": \"2024-02-17 17:48:19.565039 +0000 UTC\",\n \"updated_at\": \"0001-01-01 00:00:00 +0000 UTC\",\n \"content\": {\n \"blocks\": [\n {\n \"id\": \"1\",\n \"type\": \"text\",\n \"content\": \"Hello World\",\n \"group_id\": \"1\",\n \"order\": 1,\n \"meta\": null\n }\n ]\n },\n \"meta\": {\n \"id\": 1,\n \"form_id\": 1,\n \"is_deleted\": false,\n \"accepting_responses\": false,\n \"allow_guest_responses\": true,\n \"allow_multiple_responses\": false,\n \"send_confirmation_email_to_respondee\": false,\n \"send_submission_email_to_owner\": false,\n \"valid_till\": \"0001-01-01T00:00:00Z\",\n \"updated_by_id\": null,\n \"updated_at\": \"0001-01-01T00:00:00Z\"\n }\n },\n \"error\": null\n}" + } + ] + } + ], + "event": [ + { + "listen": "prerequest", + "script": { + "type": "text/javascript", + "exec": [ + "" + ] + } + }, + { + "listen": "test", + "script": { + "type": "text/javascript", + "exec": [ + "" + ] + } + } + ], + "variable": [ + { + "key": "BASE_URL", + "value": "http://127.0.0.1:8080", + "type": "string" + } + ] +} \ No newline at end of file