From 4fc99aeb42b1dae392330a755fff02f4f0e64ffb Mon Sep 17 00:00:00 2001 From: arzumify Date: Thu, 24 Oct 2024 19:27:25 +0100 Subject: [PATCH] Add support for static files Signed-off-by: arzumify --- README.md | 5 +++++ main.go | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8de839e..61a4f4d 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,11 @@ The server can be configured using a `config.json` file. An example configuratio - `connectionString` - The connection string for the database (postgres only) - `databasePath` - The **directory** to store the databases (sqlite only) It is necessary to have a separate directory for each service, as SQLite does not support multiple schemas in a single file. +### Static (optional) +**Each entry here is in a JSON list, with the following fields:** +- `directory` - The directory to serve static files from +- `subdomain` - The subdomain the static files are hosted on (optional, will run on the root domain if not specified) +- `pattern` - The pattern to match for the static files (optional, defaults to `/*`) ### Services #### For all services - `subdomain` - The subdomain the service is hosted on (optional, will run on the root domain if not specified) diff --git a/main.go b/main.go index 2c71cb4..11b8e6c 100644 --- a/main.go +++ b/main.go @@ -44,6 +44,11 @@ type Config struct { ConnectionString string `json:"connectionString" validate:"required_if=DatabaseType postgres"` DatabasePath string `json:"databasePath" validate:"required_if=DatabaseType sqlite"` } `json:"database" validate:"required"` + Static []struct { + Subdomain string `json:"subdomain"` + Directory string `json:"directory" validate:"required,isDirectory"` + Pattern string `json:"pattern"` + } `json:"static"` Services map[string]interface{} `json:"services"` } @@ -484,11 +489,25 @@ func main() { subdomain := service.(map[string]interface{})["subdomain"].(string) if subdomains[subdomain] == nil { subdomains[subdomain] = chi.NewRouter() + slog.Info("Mapping subdomain " + subdomain) hostRouter.Map(subdomain, subdomains[subdomain]) } } } + // Iterate through the static configurations and create routers for each unique subdomain + for _, static := range config.Static { + // Check if it wants a subdomain + if static.Subdomain != "" { + // Check if the subdomain exists + if subdomains[static.Subdomain] == nil { + subdomains[static.Subdomain] = chi.NewRouter() + slog.Info("Mapping subdomain " + static.Subdomain) + hostRouter.Map(static.Subdomain, subdomains[static.Subdomain]) + } + } + } + var globalOutbox = make(chan library.InterServiceMessage) // Initialize the service discovery, health-check, and logging services @@ -601,9 +620,35 @@ func main() { slog.Info("Service " + serviceInformation.Name + " activated with ID " + serviceInformation.ServiceID.String()) } + // Mount the host router + router.Mount("/", hostRouter) + slog.Info("All subdomains mapped") + + // Initialize the static file servers + for _, static := range config.Static { + if static.Subdomain != "" { + // Serve the static directory + if static.Pattern != "" { + subdomains[static.Subdomain].Handle(static.Pattern, http.FileServerFS(os.DirFS(static.Directory))) + slog.Info("Serving static directory " + static.Directory + " on subdomain " + static.Subdomain + " with pattern " + static.Pattern) + } else { + subdomains[static.Subdomain].Handle("/*", http.FileServerFS(os.DirFS(static.Directory))) + slog.Info("Serving static directory " + static.Directory + " on subdomain " + static.Subdomain) + } + } else { + // Serve the static directory + if static.Pattern != "" { + router.Handle(static.Pattern, http.FileServerFS(os.DirFS(static.Directory))) + slog.Info("Serving static directory " + static.Directory + " with pattern " + static.Pattern) + } else { + router.Handle("/*", http.FileServerFS(os.DirFS(static.Directory))) + slog.Info("Serving static directory " + static.Directory) + } + } + } + // Start the server slog.Info("Starting server on " + config.Global.IP + ":" + config.Global.Port) - router.Mount("/", hostRouter) err = http.ListenAndServe(config.Global.IP+":"+config.Global.Port, router) if err != nil { slog.Error("Error starting server: ", err)