Pharos 0.7.23
Modern Web Framework & Web App Hosting Service.
Loading...
Searching...
No Matches
native_runtime_load.c
Go to the documentation of this file.
1/**
2 * @file
3 * @brief Loads AppRuntime values from the finalized artifact-local app manifest and validates required runtime fields.
4 */
5
7
8#include "native_support.h"
10
11#include <stdlib.h>
12#include <string.h>
13
14#ifdef _WIN32
15#define pharos_runtime_load_strdup _strdup
16#else
17#define pharos_runtime_load_strdup strdup
18#endif
19
21 free(app->app_id);
22 free(app->app_root);
23 free(app->build_dir);
24 free(app->artifact_dir);
25 free(app->host_profile);
26 free(app->compiled_entrypoint);
27 free(app->state_path);
28 free(app->base_path);
29 free(app->log_path);
30 free(app->log_format);
31 free(app->error_report_path);
32 free(app->debug);
33 free(app->runtime_conf_path);
34 memset(app, 0, sizeof(*app));
35}
36
37int load_app_runtime_from_artifact_native(const char *artifact_dir, AppRuntime *app) {
38 char *manifest_path = path_join(artifact_dir, "pharos.app.json");
39 char *manifest_text;
40 char *cwd = current_working_directory();
41 char *app_manifest_path = NULL;
42 char *app_manifest_text = NULL;
43 const char *last_sep;
44 memset(app, 0, sizeof(*app));
45 if (manifest_path == NULL) {
46 free(cwd);
47 return 69;
48 }
49 manifest_text = read_file_text(manifest_path);
50 free(manifest_path);
51 if (manifest_text == NULL) {
52 free(cwd);
53 return 66;
54 }
55 app->artifact_dir = pharos_runtime_load_strdup(artifact_dir);
56 app->app_id = json_find_string(manifest_text, "app_id");
58 app->build_dir = path_dirname(artifact_dir);
59 app->host_profile = json_find_string_in_section(manifest_text, "runtime", "host_profile");
60 app->compiled_entrypoint = json_find_string_in_section(manifest_text, "runtime", "compiled_entrypoint");
61 app->state_path = json_find_string_in_section(manifest_text, "runtime", "state_path");
62 app->base_path = json_find_string_in_section(manifest_text, "runtime", "base_path");
63 app->log_path = json_find_string_in_section(manifest_text, "runtime", "log_path");
64 app->log_format = json_find_string_in_section(manifest_text, "runtime", "log_format");
65 app->error_report_path = json_find_string_in_section(manifest_text, "runtime", "error_report_path");
66 app->debug = json_find_string_in_section(manifest_text, "runtime", "debug");
67 if (app->base_path == NULL) {
69 }
70 if (app->app_root != NULL && app->app_root[0] != '\0' && app->app_root[0] != '/' && !(strlen(app->app_root) > 1 && app->app_root[1] == ':')) {
71 char *resolved = resolve_relative_to(artifact_dir, app->app_root);
72 free(app->app_root);
73 app->app_root = resolved;
74 }
75 if (app->app_id == NULL && app->app_root != NULL) {
76 app_manifest_path = path_join(app->app_root, "pharos.app.json");
77 if (app_manifest_path != NULL) {
78 app_manifest_text = read_file_text(app_manifest_path);
79 if (app_manifest_text != NULL) {
80 app->app_id = json_find_string(app_manifest_text, "app_id");
81 }
82 }
83 }
84 if (app->app_id == NULL) {
85 last_sep = strrchr(artifact_dir, '/');
86#ifdef _WIN32
87 {
88 const char *alt_sep = strrchr(artifact_dir, '\\');
89 if (alt_sep != NULL && (last_sep == NULL || alt_sep > last_sep)) {
90 last_sep = alt_sep;
91 }
92 }
93#endif
94 app->app_id = pharos_runtime_load_strdup(last_sep != NULL ? last_sep + 1 : artifact_dir);
95 }
96 if (app->compiled_entrypoint != NULL && app->compiled_entrypoint[0] != '\0' && app->compiled_entrypoint[0] != '/' && !(strlen(app->compiled_entrypoint) > 1 && app->compiled_entrypoint[1] == ':')) {
97 char *entrypoint_base = app->app_root != NULL ? app->app_root : cwd;
98 char *resolved = resolve_relative_to(entrypoint_base, app->compiled_entrypoint);
99 if (resolved != NULL && path_exists(resolved)) {
100 free(app->compiled_entrypoint);
101 app->compiled_entrypoint = resolved;
102 } else {
103 free(resolved);
104 resolved = resolve_relative_to(cwd, app->compiled_entrypoint);
105 if (resolved != NULL && path_exists(resolved)) {
106 free(app->compiled_entrypoint);
107 app->compiled_entrypoint = resolved;
108 } else {
109 free(resolved);
110 resolved = resolve_relative_to(artifact_dir, app->compiled_entrypoint);
111 if (resolved != NULL && path_exists(resolved)) {
112 free(app->compiled_entrypoint);
113 app->compiled_entrypoint = resolved;
114 } else {
115 free(resolved);
116 }
117 }
118 }
119 }
120 free(cwd);
121 free(app_manifest_path);
122 free(app_manifest_text);
123 free(manifest_text);
124 if (app->host_profile == NULL) {
125 app->host_profile = pharos_runtime_load_strdup("dynamic-app");
126 }
127 if (app->log_format == NULL) {
128 app->log_format = pharos_runtime_load_strdup("service");
129 }
130 if (app->error_report_path == NULL) {
132 }
133 if (app->debug == NULL) {
134 app->debug = pharos_runtime_load_strdup("false");
135 }
136 if (app->app_id == NULL || app->build_dir == NULL) {
138 return 66;
139 }
140 if (!streq(app->host_profile, "static-pages") && !streq(app->host_profile, "dynamic-app") && app->compiled_entrypoint == NULL) {
142 return 66;
143 }
144 return 0;
145}
char * json_find_string(const char *text, const char *key)
Finds the first JSON string value associated with a key.
char * json_find_string_in_section(const char *text, const char *section, const char *key)
Finds a JSON string value for a key inside a named section object.
Declares lightweight JSON and conf value lookup helpers.
#define pharos_runtime_load_strdup
static void free_loaded_app_runtime_fields(AppRuntime *app)
int load_app_runtime_from_artifact_native(const char *artifact_dir, AppRuntime *app)
Loads runtime metadata for one app from an emitted artifact directory.
Declares helpers for loading native runtime metadata from manifests.
char * path_join(const char *left, const char *right)
Joins two path segments using the native path separator.
char * current_working_directory(void)
Returns the current working directory.
int streq(const char *a, const char *b)
Tests whether two strings are exactly equal.
char * path_dirname(const char *path)
Returns the parent directory component of a path.
char * resolve_relative_to(const char *base_dir, const char *value)
Resolves a path value relative to a base directory when needed.
char * read_file_text(const char *path)
Reads an entire file into a newly allocated text buffer.
int path_exists(const char *path)
Tests whether a filesystem path currently exists.
Declares shared low-level support helpers used across the Pharos native runtime.
Loaded runtime metadata for one Pharos app artifact.