Add support for static files
Signed-off-by: arzumify <jliwin98@danwin1210.de>
This commit is contained in:
parent
6f0bdb66db
commit
4fc99aeb42
|
@ -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)
|
- `connectionString` - The connection string for the database (postgres only)
|
||||||
- `databasePath` - The **directory** to store the databases (sqlite 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.
|
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
|
### Services
|
||||||
#### For all services
|
#### For all services
|
||||||
- `subdomain` - The subdomain the service is hosted on (optional, will run on the root domain if not specified)
|
- `subdomain` - The subdomain the service is hosted on (optional, will run on the root domain if not specified)
|
||||||
|
|
47
main.go
47
main.go
|
@ -44,6 +44,11 @@ type Config struct {
|
||||||
ConnectionString string `json:"connectionString" validate:"required_if=DatabaseType postgres"`
|
ConnectionString string `json:"connectionString" validate:"required_if=DatabaseType postgres"`
|
||||||
DatabasePath string `json:"databasePath" validate:"required_if=DatabaseType sqlite"`
|
DatabasePath string `json:"databasePath" validate:"required_if=DatabaseType sqlite"`
|
||||||
} `json:"database" validate:"required"`
|
} `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"`
|
Services map[string]interface{} `json:"services"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -484,11 +489,25 @@ func main() {
|
||||||
subdomain := service.(map[string]interface{})["subdomain"].(string)
|
subdomain := service.(map[string]interface{})["subdomain"].(string)
|
||||||
if subdomains[subdomain] == nil {
|
if subdomains[subdomain] == nil {
|
||||||
subdomains[subdomain] = chi.NewRouter()
|
subdomains[subdomain] = chi.NewRouter()
|
||||||
|
slog.Info("Mapping subdomain " + subdomain)
|
||||||
hostRouter.Map(subdomain, subdomains[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)
|
var globalOutbox = make(chan library.InterServiceMessage)
|
||||||
|
|
||||||
// Initialize the service discovery, health-check, and logging services
|
// 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())
|
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
|
// Start the server
|
||||||
slog.Info("Starting server on " + config.Global.IP + ":" + config.Global.Port)
|
slog.Info("Starting server on " + config.Global.IP + ":" + config.Global.Port)
|
||||||
router.Mount("/", hostRouter)
|
|
||||||
err = http.ListenAndServe(config.Global.IP+":"+config.Global.Port, router)
|
err = http.ListenAndServe(config.Global.IP+":"+config.Global.Port, router)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Error starting server: ", err)
|
slog.Error("Error starting server: ", err)
|
||||||
|
|
Loading…
Reference in New Issue