diff --git a/README.md b/README.md index 2756d24a..49252ec8 100644 --- a/README.md +++ b/README.md @@ -26,24 +26,29 @@ If you would like to contribute, please have a look at our [contribution guideli - Clone the repository - `cp backend/.env.example backend/.env && cp frontend/.env.example frontend/.env` -- Run `./build up` -- To migrate the database to a fresh state, run `./build migrate-fresh` (you can do this while docker is running with all the services) -- If you do not need hot reloading for the client, you can set `APP_ENV=produdction` in the `.env` files, and you will not need to run the frontend server (`yarn run dev`), - simply go to `localhost:8080` in your browser and the Go server will serve the frontend as well (it builds the js in docker). - -- If you are working on the backend, you may wish to comment out the `server` service (1st) in the `docker-compose.yml` file, -and run `./build up`, then you can simply run `./build` to start the backend server, and when making changes you will not have -to wait for docker to rebuild the frontend, the middleware, and start the other 4 containers each time you wish to test out -a change. If you are developing the middleware, simply comment it out and run it separately with `go run provider-middleware/.` - -**If you need hot reloading for the client:** -- Change directory to the frontend directory -- Run `yarn install` -- Run `yarn run dev` -- Open `http://localhost:5173` in your browser - -- Login with `SuperAdmin` and password: `ChangeMe!` -- You will be prompted immediately to set a new password, and then you will be redirected to the dashboard. + +**For frontend development:** +- Run `./build dev -f` + +This will build everything but the client, which you can then run separately with `yarn run dev` in the frontend directory. + +**For backend development:** + +- Run `./build dev` + +This will build only the Auth and run Postgres. You are responsible for building and running the server and middleware. + +**For Production:** + +- Run `./build prod` to build the entire project in docker. You can then go to `localhost` in your browser. + + +Login with `SuperAdmin` and password: `ChangeMe!` + +You will be prompted immediately to set a new password, and then you will be redirected to the dashboard. + +### To migrate the database to a fresh state, run `./build migrate-fresh` (you can do this while docker is running with all the services) + ## Style/Linting diff --git a/backend/go.mod b/backend/go.mod index 976358a7..ef6ee523 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -1,4 +1,4 @@ -module Go-Prototype +module UnlockEdv2 go 1.22.2 diff --git a/backend/main.go b/backend/main.go index db4a1b3f..74cfd0f9 100644 --- a/backend/main.go +++ b/backend/main.go @@ -1,7 +1,7 @@ package main import ( - server "Go-Prototype/src/handlers" + server "UnlockEdv2/src/handlers" "fmt" "net/http" "os" diff --git a/backend/migrations/main.go b/backend/migrations/main.go index aa7c4f38..a32ac407 100644 --- a/backend/migrations/main.go +++ b/backend/migrations/main.go @@ -1,8 +1,8 @@ package main import ( - "Go-Prototype/src/database" - "Go-Prototype/src/models" + "UnlockEdv2/src/database" + "UnlockEdv2/src/models" "fmt" "log" "os" diff --git a/backend/src/database/DB.go b/backend/src/database/DB.go index 6a70b7b8..a67a2ab9 100644 --- a/backend/src/database/DB.go +++ b/backend/src/database/DB.go @@ -1,7 +1,7 @@ package database import ( - "Go-Prototype/src/models" + "UnlockEdv2/src/models" "encoding/json" "fmt" "log" diff --git a/backend/src/database/activity.go b/backend/src/database/activity.go index e4d37f1f..d712b2a0 100644 --- a/backend/src/database/activity.go +++ b/backend/src/database/activity.go @@ -1,7 +1,7 @@ package database import ( - "Go-Prototype/src/models" + "UnlockEdv2/src/models" "fmt" "time" diff --git a/backend/src/database/leftmenulinks.go b/backend/src/database/leftmenulinks.go index b5642d99..5a0491b9 100644 --- a/backend/src/database/leftmenulinks.go +++ b/backend/src/database/leftmenulinks.go @@ -1,7 +1,7 @@ package database import ( - "Go-Prototype/src/models" + "UnlockEdv2/src/models" "log" ) diff --git a/backend/src/database/milestones.go b/backend/src/database/milestones.go index 908fdaa9..864e02fd 100644 --- a/backend/src/database/milestones.go +++ b/backend/src/database/milestones.go @@ -1,7 +1,7 @@ package database import ( - "Go-Prototype/src/models" + "UnlockEdv2/src/models" ) func IsValidOrderBy(orderBy string) bool { diff --git a/backend/src/database/oidc_client.go b/backend/src/database/oidc_client.go index 86bc6bc2..d3952df9 100644 --- a/backend/src/database/oidc_client.go +++ b/backend/src/database/oidc_client.go @@ -1,6 +1,6 @@ package database -import "Go-Prototype/src/models" +import "UnlockEdv2/src/models" func (db *DB) GetClientForProvider(provID uint) (*models.OidcClient, error) { client := models.OidcClient{} diff --git a/backend/src/database/outcomes.go b/backend/src/database/outcomes.go index 600989fb..95756a4b 100644 --- a/backend/src/database/outcomes.go +++ b/backend/src/database/outcomes.go @@ -1,7 +1,7 @@ package database import ( - "Go-Prototype/src/models" + "UnlockEdv2/src/models" ) func (db *DB) GetOutcomesForUser(id uint, page, perPage int) (int64, []models.Outcome, error) { diff --git a/backend/src/database/programs.go b/backend/src/database/programs.go index 28814cb9..facd61dd 100644 --- a/backend/src/database/programs.go +++ b/backend/src/database/programs.go @@ -1,7 +1,7 @@ package database import ( - "Go-Prototype/src/models" + "UnlockEdv2/src/models" ) func (db *DB) GetProgramByID(id int) (*models.Program, error) { diff --git a/backend/src/database/provider_platforms.go b/backend/src/database/provider_platforms.go index 860dd971..0cf20d5b 100644 --- a/backend/src/database/provider_platforms.go +++ b/backend/src/database/provider_platforms.go @@ -1,7 +1,7 @@ package database import ( - "Go-Prototype/src/models" + "UnlockEdv2/src/models" "fmt" "log" ) diff --git a/backend/src/database/provider_user_mappings.go b/backend/src/database/provider_user_mappings.go index 9c6d5eff..3d8c83aa 100644 --- a/backend/src/database/provider_user_mappings.go +++ b/backend/src/database/provider_user_mappings.go @@ -1,7 +1,7 @@ package database import ( - "Go-Prototype/src/models" + "UnlockEdv2/src/models" "errors" ) diff --git a/backend/src/database/user_activity.go b/backend/src/database/user_activity.go index 1d79552b..1da5cf62 100644 --- a/backend/src/database/user_activity.go +++ b/backend/src/database/user_activity.go @@ -1,7 +1,7 @@ package database import ( - "Go-Prototype/src/models" + "UnlockEdv2/src/models" ) type UserAcitivityJoin struct { diff --git a/backend/src/database/users.go b/backend/src/database/users.go index 127fa97b..53e995cf 100644 --- a/backend/src/database/users.go +++ b/backend/src/database/users.go @@ -1,7 +1,7 @@ package database import ( - "Go-Prototype/src/models" + "UnlockEdv2/src/models" "errors" log "github.com/sirupsen/logrus" diff --git a/backend/src/handlers/actions.go b/backend/src/handlers/actions.go index 70166054..0e133724 100644 --- a/backend/src/handlers/actions.go +++ b/backend/src/handlers/actions.go @@ -1,8 +1,8 @@ package handlers import ( - "Go-Prototype/src" - "Go-Prototype/src/models" + "UnlockEdv2/src" + "UnlockEdv2/src/models" "net/http" "strconv" diff --git a/backend/src/handlers/activity_handler.go b/backend/src/handlers/activity_handler.go index 73dff09b..860c23b5 100644 --- a/backend/src/handlers/activity_handler.go +++ b/backend/src/handlers/activity_handler.go @@ -1,7 +1,7 @@ package handlers import ( - "Go-Prototype/src/models" + "UnlockEdv2/src/models" "encoding/json" "net/http" "strconv" diff --git a/backend/src/handlers/left_menu_handler.go b/backend/src/handlers/left_menu_handler.go index 771034b6..8b42fd23 100644 --- a/backend/src/handlers/left_menu_handler.go +++ b/backend/src/handlers/left_menu_handler.go @@ -1,7 +1,7 @@ package handlers import ( - "Go-Prototype/src/models" + "UnlockEdv2/src/models" "encoding/json" "net/http" diff --git a/backend/src/handlers/milestones_handler.go b/backend/src/handlers/milestones_handler.go index d606b6a7..c34f62b5 100644 --- a/backend/src/handlers/milestones_handler.go +++ b/backend/src/handlers/milestones_handler.go @@ -1,7 +1,7 @@ package handlers import ( - "Go-Prototype/src/models" + "UnlockEdv2/src/models" "encoding/json" "net/http" "strconv" diff --git a/backend/src/handlers/oidc.go b/backend/src/handlers/oidc.go index c835be0f..70e038a6 100644 --- a/backend/src/handlers/oidc.go +++ b/backend/src/handlers/oidc.go @@ -1,7 +1,7 @@ package handlers import ( - "Go-Prototype/src/models" + "UnlockEdv2/src/models" "encoding/json" "net/http" "os" diff --git a/backend/src/handlers/outcomes_handler.go b/backend/src/handlers/outcomes_handler.go index fa247852..f630e2cc 100644 --- a/backend/src/handlers/outcomes_handler.go +++ b/backend/src/handlers/outcomes_handler.go @@ -1,7 +1,7 @@ package handlers import ( - "Go-Prototype/src/models" + "UnlockEdv2/src/models" "encoding/json" "net/http" "strconv" diff --git a/backend/src/handlers/programs_handler.go b/backend/src/handlers/programs_handler.go index c46420db..afc131b5 100644 --- a/backend/src/handlers/programs_handler.go +++ b/backend/src/handlers/programs_handler.go @@ -1,7 +1,7 @@ package handlers import ( - "Go-Prototype/src/models" + "UnlockEdv2/src/models" "encoding/json" "net/http" "strconv" diff --git a/backend/src/handlers/provider_platform_handler.go b/backend/src/handlers/provider_platform_handler.go index d21360bf..5c5057a6 100644 --- a/backend/src/handlers/provider_platform_handler.go +++ b/backend/src/handlers/provider_platform_handler.go @@ -1,7 +1,7 @@ package handlers import ( - "Go-Prototype/src/models" + "UnlockEdv2/src/models" "encoding/json" "net/http" "strconv" diff --git a/backend/src/handlers/provider_user_mapping_handler.go b/backend/src/handlers/provider_user_mapping_handler.go index 57f01c7d..d4f04d46 100644 --- a/backend/src/handlers/provider_user_mapping_handler.go +++ b/backend/src/handlers/provider_user_mapping_handler.go @@ -1,7 +1,7 @@ package handlers import ( - "Go-Prototype/src/models" + "UnlockEdv2/src/models" "encoding/json" "net/http" "strconv" diff --git a/backend/src/handlers/server.go b/backend/src/handlers/server.go index c5599421..2a05f25b 100644 --- a/backend/src/handlers/server.go +++ b/backend/src/handlers/server.go @@ -1,7 +1,7 @@ package handlers import ( - database "Go-Prototype/src/database" + database "UnlockEdv2/src/database" "context" "encoding/json" "math" diff --git a/backend/src/handlers/user_activity_handler.go b/backend/src/handlers/user_activity_handler.go index 7a480e74..9cf03ae5 100644 --- a/backend/src/handlers/user_activity_handler.go +++ b/backend/src/handlers/user_activity_handler.go @@ -1,8 +1,8 @@ package handlers import ( - "Go-Prototype/src/database" - "Go-Prototype/src/models" + "UnlockEdv2/src/database" + "UnlockEdv2/src/models" "net/http" "strconv" "strings" diff --git a/backend/src/handlers/user_handler.go b/backend/src/handlers/user_handler.go index 2deb5caf..11f340f2 100644 --- a/backend/src/handlers/user_handler.go +++ b/backend/src/handlers/user_handler.go @@ -1,7 +1,7 @@ package handlers import ( - "Go-Prototype/src/models" + "UnlockEdv2/src/models" "encoding/json" "fmt" "net/http" diff --git a/backend/src/service.go b/backend/src/service.go index a61b9a53..fc9f82c8 100644 --- a/backend/src/service.go +++ b/backend/src/service.go @@ -1,7 +1,7 @@ package src import ( - "Go-Prototype/src/models" + "UnlockEdv2/src/models" "bytes" "encoding/json" "errors" diff --git a/backend/tests/main_test.go b/backend/tests/main_test.go index 63495542..5950d870 100644 --- a/backend/tests/main_test.go +++ b/backend/tests/main_test.go @@ -1,7 +1,7 @@ package tests import ( - "Go-Prototype/src/handlers" + "UnlockEdv2/src/handlers" "os" "testing" ) diff --git a/backend/tests/provider_platform_handler_test.go b/backend/tests/provider_platform_handler_test.go index a6e1b6c1..65b40aca 100644 --- a/backend/tests/provider_platform_handler_test.go +++ b/backend/tests/provider_platform_handler_test.go @@ -1,7 +1,7 @@ package tests import ( - "Go-Prototype/src/models" + "UnlockEdv2/src/models" "bytes" "encoding/json" "net/http" diff --git a/backend/tests/user_handler_test.go b/backend/tests/user_handler_test.go index fef891b9..c1b0cc7a 100644 --- a/backend/tests/user_handler_test.go +++ b/backend/tests/user_handler_test.go @@ -1,8 +1,8 @@ package tests import ( - "Go-Prototype/src/handlers" - "Go-Prototype/src/models" + "UnlockEdv2/src/handlers" + "UnlockEdv2/src/models" "bytes" "encoding/json" "fmt" diff --git a/build b/build index 533b0e9a..9335d41b 100755 --- a/build +++ b/build @@ -1,23 +1,22 @@ #!/bin/bash -help="Usage: ./build up | migrate-fresh | server" +help="Usage: ./build prod | dev [-f] | migrate-fresh" -if [[ "$1" == "-h" ]]; then - echo "$help" +if [[ "$1" == "prod" ]]; then + docker compose -f docker-compose.yml -f config/docker-compose.prod.yml up --build exit 0 fi -if [[ "$1" == "up" ]]; then - docker-compose up -d --build - echo "Building backend..." +if [[ "$1" == "dev" ]]; then + if [[ "$2" == "-f" ]]; then + docker compose -f docker-compose.yml -f config/docker-compose.fe-dev.yml up --build + else + docker compose up + fi fi if [[ "$1" == "migrate-fresh" ]]; then go run backend/migrations/main.go fi -if [[ "$1" == "server" ]]; then - go run backend/main.go -fi - exit 0 diff --git a/config/docker-compose.fe-dev.yml b/config/docker-compose.fe-dev.yml new file mode 100644 index 00000000..35a48af5 --- /dev/null +++ b/config/docker-compose.fe-dev.yml @@ -0,0 +1,43 @@ +services: + server: + build: + context: . + dockerfile: ./backend/Dockerfile + environment: + - APP_ENV=production + - DB_HOST=postgres + - DB_PORT=5432 + - DB_USER=unlocked + - DB_PASSWORD=dev + - DB_NAME=unlocked + - PROVIDER_SERVICE_URL=http://provider-service:8081 + - HYDRA_ADMIN_URL=http://hydra:4445 + - HYDRA_PUBLIC_URL=http://hydra:4444 + ports: + - "8080:8080" + depends_on: + postgres: + condition: service_healthy + env_file: + - "./backend/.env" + command: > + ./backend + volumes: + - ./logs:/logs/ + networks: + - intranet + + # Provider middleware + provider-service: + build: + context: ./provider-middleware + dockerfile: Dockerfile + env_file: "./backend/.env" + command: ./provider-service + networks: + - intranet + volumes: + - ./logs:/logs + depends_on: + postgres: + condition: service_healthy diff --git a/config/docker-compose.prod.yml b/config/docker-compose.prod.yml new file mode 100644 index 00000000..30e49c5b --- /dev/null +++ b/config/docker-compose.prod.yml @@ -0,0 +1,60 @@ +services: + # Main backend server + server: + build: + context: . + dockerfile: ./backend/Dockerfile + environment: + - APP_ENV=production + - DB_HOST=postgres + - DB_PORT=5432 + - DB_USER=unlocked + - DB_PASSWORD=dev + - DB_NAME=unlocked + - PROVIDER_SERVICE_URL=http://provider-service:8081 + - HYDRA_ADMIN_URL=http://hydra:4445 + - HYDRA_PUBLIC_URL=http://hydra:4444 + depends_on: + postgres: + condition: service_healthy + env_file: + - "./backend/.env" + command: > + ./backend + volumes: + - ./logs:/logs/ + networks: + - intranet + + # Provider middleware + provider-service: + build: + context: ./provider-middleware + dockerfile: Dockerfile + env_file: "./backend/.env" + command: ./provider-service + networks: + - intranet + volumes: + - ./logs:/logs + depends_on: + postgres: + condition: service_healthy + + # Frontend served in nginx container + frontend: + build: + context: frontend + dockerfile: Dockerfile + ports: + - 80:80 + environment: + - APP_ENV=production + env_file: frontend/.env + networks: + - intranet + + volumes: + - ./config/nginx.conf:/etc/nginx/conf.d/default.conf + - ./logs:/var/log/nginx/ + depends_on: [server] diff --git a/frontend/nginx.conf b/config/nginx.conf similarity index 53% rename from frontend/nginx.conf rename to config/nginx.conf index 67b0a8c1..737e251e 100644 --- a/frontend/nginx.conf +++ b/config/nginx.conf @@ -1,16 +1,18 @@ server { listen 80; server_name localhost; + location / { root /usr/share/nginx/html; index index.html index.htm; try_files $uri $uri/ /index.html; } + location /api { - proxy_pass http://server:8080; - proxy_http_version 1.1; - proxy_set_header Upgrade $http_upgrade; - proxy_set_header Host $host; - proxy_cache_bypass $http_upgrade; + proxy_pass http://server:8080; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Host $host; + proxy_cache_bypass $http_upgrade; } } diff --git a/docker-compose.yml b/docker-compose.yml index a9236e18..68ac0978 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,44 +1,4 @@ services: - server: - build: - context: . - dockerfile: ./backend/Dockerfile - environment: - - APP_ENV=production - - DB_HOST=postgres - - DB_PORT=5432 - - DB_USER=unlocked - - DB_PASSWORD=dev - - DB_NAME=unlocked - - PROVIDER_SERVICE_URL=http://provider-service:8081 - - HYDRA_ADMIN_URL=http://hydra:4445 - - HYDRA_PUBLIC_URL=http://hydra:4444 - depends_on: - postgres: - condition: service_healthy - env_file: - - "./backend/.env" - command: > - ./backend - volumes: - - ./logs:/logs/ - networks: - - intranet - - provider-service: - build: - context: ./provider-middleware - dockerfile: Dockerfile - env_file: "./backend/.env" - command: ./provider-service - networks: - - intranet - volumes: - - ./logs:/logs - depends_on: - postgres: - condition: service_healthy - postgres: image: postgres:16 environment: @@ -97,22 +57,6 @@ services: restart: on-failure networks: - intranet - - frontend: - build: - context: frontend - dockerfile: Dockerfile - ports: - - 80:80 - environment: - - APP_ENV=production - env_file: frontend/.env - networks: - - intranet - volumes: - - ./logs:/var/log/nginx/ - depends_on: [server] - networks: intranet: diff --git a/docker-dev.yml b/docker-dev.yml deleted file mode 100644 index 18bf0151..00000000 --- a/docker-dev.yml +++ /dev/null @@ -1,67 +0,0 @@ -services: - postgres: - image: postgres:16 - environment: - POSTGRES_DB: unlocked - POSTGRES_USER: unlocked - POSTGRES_PASSWORD: dev - ports: - - "5432:5432" - volumes: - - postgres_data:/var/lib/postgresql/data - - ./logs:/var/log/postgresql - healthcheck: - test: ["CMD-SHELL", "pg_isready -U unlocked"] - interval: 10s - timeout: 5s - retries: 5 - networks: - - intranet - - hydra: - image: oryd/hydra:v2.2.0 - ports: - - "4444:4444" # Public port - - "4445:4445" # Admin port - - "5555:5555" # Port for hydra token user - command: serve -c /etc/config/hydra/hydra.yml all --dev - volumes: - - type: volume - source: hydra-sqlite - target: /var/lib/sqlite - read_only: false - - type: bind - source: ./hydra - target: /etc/config/hydra - environment: - - DSN=sqlite:///var/lib/sqlite/db.sqlite?_fk=true - restart: unless-stopped - env_file: ./backend/.env - depends_on: - - hydra-migrate - networks: - - intranet - hydra-migrate: - image: oryd/hydra:v2.2.0 - environment: - - DSN=sqlite:///var/lib/sqlite/db.sqlite?_fk=true - command: migrate -c /etc/config/hydra/hydra.yml sql -e --yes - volumes: - - type: volume - source: hydra-sqlite - target: /var/lib/sqlite - read_only: false - - type: bind - source: ./hydra - target: /etc/config/hydra - restart: on-failure - networks: - - intranet - -networks: - intranet: - -volumes: - postgres_data: - hydra-sqlite: - logs: diff --git a/frontend/Dockerfile b/frontend/Dockerfile index 47a98dcb..e2821193 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -7,6 +7,5 @@ RUN yarn run build FROM nginx:1.21.3-alpine COPY --from=builder /app/dist /usr/share/nginx/html -COPY --from=builder /app/nginx.conf /etc/nginx/conf.d/default.conf EXPOSE 80 ENTRYPOINT ["nginx", "-g", "daemon off;"] diff --git a/frontend/vite.config.ts b/frontend/vite.config.ts index cca7e0db..22c1406b 100644 --- a/frontend/vite.config.ts +++ b/frontend/vite.config.ts @@ -10,4 +10,12 @@ export default defineConfig({ "@": path.resolve(__dirname, "src"), }, }, + server: { + proxy: { + "/api": { + target: "http://localhost:8080", + changeOrigin: true, + }, + }, + }, });