@@ -98,6 +98,11 @@ pub struct Opts {
9898 /// Introduces random delays in the server, to simulate a slow connection. Useful for testing.
9999 #[ clap( long, env = "ATOMIC_SLOW_MODE" ) ]
100100 pub slow_mode : bool ,
101+
102+ /// The base domain for multi-tenant hosting.
103+ /// If set, the server will allow serving subdomains of this domain (e.g. *.atomicserver.eu).
104+ #[ clap( long, env = "ATOMIC_BASE_DOMAIN" ) ]
105+ pub base_domain : Option < String > ,
101106}
102107
103108#[ derive( clap:: ValueEnum , Clone , Debug ) ]
@@ -196,6 +201,35 @@ pub struct Config {
196201 pub search_index_path : PathBuf ,
197202 /// If true, the initialization scripts will be ran (create first Drive, Agent, indexing, etc)
198203 pub initialize : bool ,
204+ /// The base domain for multi-tenant hosting.
205+ pub base_domain : Option < String > ,
206+ }
207+
208+ impl Config {
209+ /// Returns the server URL for a given request.
210+ /// If multi-tenancy is enabled and the host matches a subdomain of the base domain, it returns the host URL.
211+ pub fn get_server_url_for_request ( & self , req : & actix_web:: HttpRequest ) -> String {
212+ if let Some ( base) = & self . base_domain {
213+ if let Some ( host) = req. head ( ) . headers . get ( "Host" ) {
214+ if let Ok ( host_str) = host. to_str ( ) {
215+ // Remove port if present
216+ let domain = host_str. split ( ':' ) . next ( ) . unwrap_or ( host_str) ;
217+ if domain. ends_with ( base) {
218+ let schema =
219+ if let Some ( proto) = req. head ( ) . headers . get ( "X-Forwarded-Proto" ) {
220+ proto. to_str ( ) . unwrap_or ( "http" )
221+ } else if self . opts . https {
222+ "https"
223+ } else {
224+ "http"
225+ } ;
226+ return format ! ( "{}://{}" , schema, host_str) ;
227+ }
228+ }
229+ }
230+ }
231+ self . server_url . clone ( )
232+ }
199233}
200234
201235/// Parse .env and CLI options
@@ -289,6 +323,8 @@ pub fn build_config(opts: Opts) -> AtomicServerResult<Config> {
289323 format ! ( "{}://{}:{}" , schema, opts. domain, opts. port)
290324 } ;
291325
326+ let base_domain = opts. base_domain . clone ( ) ;
327+
292328 Ok ( Config {
293329 initialize,
294330 opts,
@@ -302,5 +338,6 @@ pub fn build_config(opts: Opts) -> AtomicServerResult<Config> {
302338 store_path,
303339 search_index_path,
304340 uploads_path,
341+ base_domain,
305342 } )
306343}
0 commit comments