Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(server): Add ApiUrl + ServerUrl env + allow usage of https #8579

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

AMoreaux
Copy link
Contributor

@AMoreaux AMoreaux commented Nov 19, 2024

  • Use SERVER_URL to define the NestJS instance URL
  • Add API_URL to set the public URL. Loadbalancer in production. In non-secure environments, it will be the same as the SERVER_URL
  • Allows setting an SSL certificate and using https.

Replaced individual environment-based server URL retrievals with a centralized ServerUrl utility. This change simplifies URL management and ensures a consistent approach across different services. Added validation for SSL configurations when using HTTPS.
Removed standalone ServerUrl and integrated with combined ServerUrl/ApiUrl module. Refactored codebase to use ApiUrl for public network accessibility and adjusted corresponding imports.
Renamed utils file for better readability and consistency. Updated all references to the new file name and added unit tests for ServerUrl and ApiUrl functionalities.
Copy link

github-actions bot commented Nov 19, 2024

Warnings
⚠️ Changes were made to the environment variables, but not to the documentation - Please review your changes and check if a change needs to be documented!

Generated by 🚫 dangerJS against 79a8da2

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

This PR adds HTTPS support and introduces separate URL management for server and API endpoints, with SSL certificate configuration and improved URL handling across the application.

  • Added SSL_KEY_PATH and SSL_CERT_PATH environment variables in /packages/twenty-server/.env.example for HTTPS support
  • Introduced ServerUrl and ApiUrl utilities in /packages/twenty-server/src/engine/utils/server-and-api-urls.ts for centralized URL management
  • Added validation in main.ts to ensure SSL certificates exist when using HTTPS protocol
  • Refactored services to use ApiUrl.get() instead of direct environment variable access for consistent URL handling
  • Added IPv6 support and protocol detection in URL handling with proper hostname normalization

9 file(s) reviewed, 15 comment(s)
Edit PR Review Bot Settings | Greptile

packages/twenty-server/.env.example Outdated Show resolved Hide resolved
packages/twenty-server/.env.example Outdated Show resolved Hide resolved
packages/twenty-server/src/main.ts Show resolved Hide resolved
packages/twenty-server/src/main.ts Outdated Show resolved Hide resolved
packages/twenty-server/src/main.ts Outdated Show resolved Hide resolved
packages/twenty-server/src/main.ts Show resolved Hide resolved
Move the reset of ServerUrl and ApiUrl to beforeEach in tests to ensure clean state before each test runs. Update example SSL_KEY_PATH and SSL_CERT_PATH to use relative paths for more portability.
Import the ApiUrl module and set the local URL in the setup function. This ensures the tests have the correct API endpoint configured.
Reordered import statements in `workspace-invitation.service.spec.ts` for improved code organization. This change ensures that dependencies are imported in a more logical sequence.
Included API_URL in self-hosting documentation for public endpoint configuration. This helps developers set up the correct URLs for API interactions in their self-hosted environments.
@@ -75,3 +75,7 @@ ACCESS_TOKEN_SECRET=replace_me_with_a_random_string_access
# PG_SSL_ALLOW_SELF_SIGNED=true
# SESSION_STORE_SECRET=replace_me_with_a_random_string_session
# ENTERPRISE_KEY=replace_me_with_a_valid_enterprise_key
###### --------------> !!! FOR CHARLES AND FELIX !!! we can create a gist in twenty if you want <---------------------------
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can store this script in the repo (twenty-server/scripts), you can move instructions there as well (or the dev documentation but I think having a readme in a dedicated scripts/ssl-generation/ folder is fine too 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AMoreaux let's remove this comment before merging 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad it's done

packages/twenty-server/src/main.ts Outdated Show resolved Hide resolved
Removed protocol check logic from main.ts and enforced URL validation to require protocol directly in environment variables definition. This ensures consistent and secure URL formats throughout the application.
Add a new Bash script for generating self-signed SSL certificates, including a README with instructions. Supports customizable domain, root certificate name, and validity period, and integrates root certificate into macOS keychain.
@AMoreaux AMoreaux requested a review from Weiko November 20, 2024 14:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants