main.vala 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using Astralis;
  2. using Inversion;
  3. using Statum;
  4. using UsmWeb;
  5. int main(string[] args) {
  6. int port = 8080;
  7. string? repo_dir = null;
  8. for (int i = 1; i < args.length; i++) {
  9. var arg = args[i];
  10. if (arg == "--repo" && i + 1 < args.length) {
  11. repo_dir = args[++i];
  12. } else if (arg.has_prefix("--repo=")) {
  13. repo_dir = arg.substring("--repo=".length);
  14. } else if (arg.has_prefix("--port=")) {
  15. port = int.parse(arg.substring("--port=".length));
  16. } else {
  17. port = int.parse(arg);
  18. }
  19. }
  20. repo_dir = repo_dir ?? Environment.get_variable("USM_WEB_REPO_DIR");
  21. try {
  22. var application = new WebApplication(port);
  23. application.use_compression();
  24. application.add_module<StatumModule>();
  25. var web_config = application.container.create_transient_scope().resolve<WebConfig>();
  26. // Precedence: --repo argument > USM_WEB_REPO_DIR > the "usm-web"
  27. // section's "repo" key (UsmWebConfig.load) > /repo
  28. var config = UsmWebConfig.load(web_config, repo_dir);
  29. application.add_singleton<UsmWebConfig>(() => config);
  30. application.add_singleton<RepositoryService>();
  31. var statum = application.configure_with<StatumConfigurator>();
  32. // spry:pages-begin
  33. statum.add_page<HomePage, HomeEntrypoint>();
  34. statum.add_page<RepoBrowsePage, RepoBrowseEntrypoint>();
  35. statum.add_page<PackageDetailPage, PackageDetailEntrypoint>();
  36. // spry:pages-end
  37. // spry:actions-begin
  38. statum.action<SearchPackagesAction>();
  39. // spry:actions-end
  40. // spry:resources-begin
  41. statum.add_resource<MainCssResource>();
  42. // spry:resources-end
  43. // USM-serving endpoints. The exact /repo/{name}/PACKAGES.usml and
  44. // /repo/{name}/repo.usmr routes must precede /repo/{name}/{file}
  45. // (which also matches them), and the /{file} catch-all — serving
  46. // only /install-usm.sh — must come last.
  47. application.add_endpoint<PackagesListingEndpoint>(new EndpointRoute("/repo/{name}/PACKAGES.usml"));
  48. application.add_endpoint<RepositoryDefinitionEndpoint>(new EndpointRoute("/repo/{name}/repo.usmr"));
  49. application.add_endpoint<PackageDownloadEndpoint>(new EndpointRoute("/repo/{name}/{file}"));
  50. if (config.installer == InstallerSource.PATH) {
  51. application.add_endpoint<InstallerScriptEndpoint>(new EndpointRoute("/{file}"));
  52. }
  53. application.run();
  54. return 0;
  55. } catch (Error e) {
  56. printerr("[usm-web] Error: %s\n", e.message);
  57. return 1;
  58. }
  59. }