diff --git a/backend/Dockerfile.server b/backend/Dockerfile.server index 4d6794be..2d8194e3 100644 --- a/backend/Dockerfile.server +++ b/backend/Dockerfile.server @@ -18,4 +18,4 @@ COPY --from=builder /app/bin/sac /sac EXPOSE 8080 -ENTRYPOINT [ "/sac" ] \ No newline at end of file +ENTRYPOINT [ "/sac" ] diff --git a/backend/config/config.go b/backend/config/config.go index 010b5a55..152c8cbb 100644 --- a/backend/config/config.go +++ b/backend/config/config.go @@ -8,8 +8,10 @@ import ( ) func GetConfiguration(path string) (*Settings, error) { - if err := godotenv.Load(path); err != nil { - return nil, fmt.Errorf("failed to load environment variables: %s", err.Error()) + if path != "" { + if err := godotenv.Load(path); err != nil { + return nil, fmt.Errorf("failed to load environment variables: %s", err.Error()) + } } intSettings, err := env.ParseAs[intermediateSettings]() diff --git a/backend/main.go b/backend/main.go index 65a6e778..be739406 100644 --- a/backend/main.go +++ b/backend/main.go @@ -9,7 +9,6 @@ import ( "net" "os" "os/signal" - "path/filepath" "syscall" "github.com/GenerateNU/sac/backend/background" @@ -18,13 +17,11 @@ import ( "github.com/GenerateNU/sac/backend/constants" "github.com/GenerateNU/sac/backend/database" "github.com/GenerateNU/sac/backend/database/store" - - // TODO: disable for prod with build tag - _ "github.com/GenerateNU/sac/backend/docs" "github.com/GenerateNU/sac/backend/integrations" "github.com/GenerateNU/sac/backend/integrations/email" "github.com/GenerateNU/sac/backend/integrations/file" "github.com/GenerateNU/sac/backend/integrations/oauth/soth/sothic" + "github.com/GenerateNU/sac/backend/search" "github.com/GenerateNU/sac/backend/server" "github.com/GenerateNU/sac/backend/telemetry" "github.com/GenerateNU/sac/backend/utilities" @@ -85,7 +82,7 @@ func main() { func parseFlags() (onlyMigrate, seedSearch *bool, configPath *string) { onlyMigrate = flag.Bool("only-migrate", false, "Specify if you want to only perform the database migration") seedSearch = flag.Bool("seed-search", true, "Specify if you want to seed the opensearch nodes.") - configPath = flag.String("config", filepath.Join("..", "config", ".env.dev"), "Specify the path to the config file (.env)") + configPath = flag.String("config", "", "Specify the path to the config file (.env)") flag.Parse() return } @@ -101,14 +98,13 @@ func checkServerRunning(host string, port uint16) error { } func seedSearchData(db *gorm.DB) { - slog.Info("to appease linter", "seedSearch", true, "db", db) - // if err := search.SeedClubs(db); err != nil { - // return - // } - - // if err := search.SeedEvents(db); err != nil { - // return - // } + if err := search.SeedClubs(db); err != nil { + return + } + + if err := search.SeedEvents(db); err != nil { + return + } } func startBackgroundJobs(ctx context.Context, db *gorm.DB) { diff --git a/backend/redis_entrypoint.sh b/backend/redis_entrypoint.sh index ffa4913b..37ba71ef 100644 --- a/backend/redis_entrypoint.sh +++ b/backend/redis_entrypoint.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash # set up redis configuration directory mkdir -p /usr/local/etc/redis @@ -12,7 +12,7 @@ if [ -n ${REDIS_USERNAME} ] && [ -n ${REDIS_PASSWORD} ]; then fi # disable default user -if [ $(echo ${REDIS_DISABLE_DEFAULT_USER}) == "true" ]; then +if [ "$REDIS_DISABLE_DEFAULT_USER" == "true" ]; then echo "user default off nopass nocommands" >> /usr/local/etc/redis/custom_aclfile.acl fi diff --git a/backend/search/base/controller.go b/backend/search/base/controller.go index bd8a6b47..02c63700 100644 --- a/backend/search/base/controller.go +++ b/backend/search/base/controller.go @@ -1,7 +1,9 @@ package base import ( + "log" "net/http" + "os" search_types "github.com/GenerateNU/sac/backend/search/types" "github.com/GenerateNU/sac/backend/utilities" @@ -30,6 +32,8 @@ func NewSearchController(searchService SearchServiceInterface) *SearchController // @Failure 500 {object} error // @Router /search/clubs [get] func (s *SearchController) SearchClubs(c *fiber.Ctx) error { + log.SetOutput(os.Stdout) + var searchQuery search_types.ClubSearchRequest if err := c.BodyParser(&searchQuery); err != nil { diff --git a/backend/search/base/transactions.go b/backend/search/base/transactions.go index a56f64fa..f9730157 100644 --- a/backend/search/base/transactions.go +++ b/backend/search/base/transactions.go @@ -45,7 +45,7 @@ func doSearchGetRequest[T, V any](url string, requestBody T) (*V, error) { func Search[T types.Searchable](db *gorm.DB, query types.SearchRequest) (*types.SearchResult[T], error) { result, err := doSearchGetRequest[types.SearchEndpointRequest, types.SearchEndpointResponse](fmt.Sprintf("/%s/_search", query.Index()), query.ToSearchEndpointRequest()) if err != nil { - return nil, nil + return nil, err } ids := make([]string, len(result.Hits.Hits)) @@ -57,7 +57,7 @@ func Search[T types.Searchable](db *gorm.DB, query types.SearchRequest) (*types. var results []T if err = query.Preload(db).Where("id IN ?", ids).Find(&results).Error; err != nil { - return nil, nil + return nil, err } return &types.SearchResult[T]{ diff --git a/config/.env.template b/config/.env.template index 542b2a0d..29867efd 100644 --- a/config/.env.template +++ b/config/.env.template @@ -1,6 +1,7 @@ SAC_APPLICATION_PORT="8080" SAC_APPLICATION_HOST="127.0.0.1" SAC_APPLICATION_BASE_URL="http://127.0.0.1" +SAC_APPLICATION_PUBLIC_URL="http://127.0.0.1" SAC_DB_USERNAME="postgres" SAC_DB_PASSWORD="password" @@ -36,11 +37,6 @@ SAC_AWS_REGION="SAC_AWS_REGION" SAC_SUDO_PASSWORD="Password#!1" -SAC_AWS_BUCKET_NAME="SAC_AWS_BUCKET_NAME" -SAC_AWS_ID="SAC_AWS_ID" -SAC_AWS_SECRET="SAC_AWS_SECRET" -SAC_AWS_REGION="SAC_AWS_REGION" - SAC_RESEND_API_KEY="SAC_RESEND_API_KEY" SAC_CALENDAR_MAX_TERMINATION_DATE="12-31-2024" diff --git a/deployment/Caddyfile b/deployment/Caddyfile new file mode 100644 index 00000000..062cdc7f --- /dev/null +++ b/deployment/Caddyfile @@ -0,0 +1,3 @@ +studentactivitycalendar.xyz { + reverse_proxy sac_webserver:8080 +} \ No newline at end of file diff --git a/deployment/compose.yml b/deployment/compose.yml new file mode 100644 index 00000000..fda9ec29 --- /dev/null +++ b/deployment/compose.yml @@ -0,0 +1,74 @@ +services: + # WEBSERVER + caddy: + image: caddy:latest + restart: unless-stopped + cap_add: + - NET_ADMIN + ports: + - 80:80 + - 443:443 + - 443:443/udp + - 8443:8443 + - 8443:8443/udp + volumes: + - $PWD/Caddyfile:/etc/caddy/Caddyfile + - caddy_data:/data + - caddy_config:/config + webserver: + container_name: sac_webserver + build: + context: ../backend + dockerfile: ../backend/Dockerfile.server + env_file: + - .env.prod + + # REDIS + redis-db-cache: + build: + context: ../backend + dockerfile: ../backend/Dockerfile.redis + container_name: redis_db_cache + ports: + - "6379" + environment: + - REDIS_USERNAME=redis_db_cache + - REDIS_PASSWORD=redis_db_cache!#1 + - REDIS_DISABLE_DEFAULT_USER="true" + volumes: + - redis-db-cache-data:/data + redis-session: + build: + context: ../backend + dockerfile: ../backend/Dockerfile.redis + container_name: redis_session + ports: + - "6379" + environment: + - REDIS_USERNAME=${SAC_REDIS_SESSION_USERNAME} + - REDIS_PASSWORD=${SAC_REDIS_SESSION_PASSWORD} + - REDIS_DISABLE_DEFAULT_USER="true" + volumes: + - redis-session-data:/data + redis-limiter: + build: + context: ../backend + dockerfile: ../backend/Dockerfile.redis + container_name: redis_limiter + expose: + - "6379" + environment: + - REDIS_USERNAME=${SAC_REDIS_LIMITER_USERNAME} + - REDIS_PASSWORD=${SAC_REDIS_LIMITER_PASSWORD} + - REDIS_DISABLE_DEFAULT_USER="true" + volumes: + - redis-limiter-data:/data + +volumes: + redis-session-data: + redis-limiter-data: + redis-db-cache-data: + opensearch-data1: + caddy_data: + external: true + caddy_config: diff --git a/deployment/init-db.sh b/deployment/init-db.sh new file mode 100644 index 00000000..486b8605 --- /dev/null +++ b/deployment/init-db.sh @@ -0,0 +1,2 @@ +go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest +~/go/bin/migrate -path ../backend/migrations/ -database postgres://${SAC_DB_USERNAME}:${SAC_DB_PASSWORD}@${SAC_DB_HOST}:${SAC_DB_PORT}/${SAC_DB_NAME}?sslmode=require -verbose up \ No newline at end of file diff --git a/deployment/setup.sh b/deployment/setup.sh new file mode 100644 index 00000000..7ea1eaf6 --- /dev/null +++ b/deployment/setup.sh @@ -0,0 +1,18 @@ +sudo yum update -y +sudo yum install -y docker git golang +sudo systemctl enable --now docker +sudo usermod -a -G docker ec2-user + +# Install the docker compose plugin for all users +sudo mkdir -p /usr/local/lib/docker/cli-plugins + +sudo curl -sL https://github.com/docker/compose/releases/latest/download/docker-compose-linux-"$(uname -m)" \ + -o /usr/local/lib/docker/cli-plugins/docker-compose + +# Set ownership to root and make executable +test -f /usr/local/lib/docker/cli-plugins/docker-compose \ + && sudo chown root:root /usr/local/lib/docker/cli-plugins/docker-compose +test -f /usr/local/lib/docker/cli-plugins/docker-compose \ + && sudo chmod +x /usr/local/lib/docker/cli-plugins/docker-compose + +git clone -b prod-business https://github.com/GenerateNU/sac