Pharos
0.7.23
Modern Web Framework & Web App Hosting Service.
Toggle main menu visibility
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
6
#include "
native_runtime_load.h
"
7
8
#include "
native_support.h
"
9
#include "
native_json_config.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
20
static
void
free_loaded_app_runtime_fields
(
AppRuntime
*app) {
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
37
int
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"
);
57
app->
app_root
=
pharos_runtime_load_strdup
(
"."
);
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) {
68
app->
base_path
=
pharos_runtime_load_strdup
(
""
);
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) {
131
app->
error_report_path
=
pharos_runtime_load_strdup
(
""
);
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) {
137
free_loaded_app_runtime_fields
(app);
138
return
66;
139
}
140
if
(!
streq
(app->
host_profile
,
"static-pages"
) && !
streq
(app->
host_profile
,
"dynamic-app"
) && app->
compiled_entrypoint
== NULL) {
141
free_loaded_app_runtime_fields
(app);
142
return
66;
143
}
144
return
0;
145
}
json_find_string
char * json_find_string(const char *text, const char *key)
Finds the first JSON string value associated with a key.
Definition
native_json_config.c:58
json_find_string_in_section
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.
Definition
native_json_config.c:62
native_json_config.h
Declares lightweight JSON and conf value lookup helpers.
pharos_runtime_load_strdup
#define pharos_runtime_load_strdup
Definition
native_runtime_load.c:17
free_loaded_app_runtime_fields
static void free_loaded_app_runtime_fields(AppRuntime *app)
Definition
native_runtime_load.c:20
load_app_runtime_from_artifact_native
int load_app_runtime_from_artifact_native(const char *artifact_dir, AppRuntime *app)
Loads runtime metadata for one app from an emitted artifact directory.
Definition
native_runtime_load.c:37
native_runtime_load.h
Declares helpers for loading native runtime metadata from manifests.
path_join
char * path_join(const char *left, const char *right)
Joins two path segments using the native path separator.
Definition
native_support.c:115
current_working_directory
char * current_working_directory(void)
Returns the current working directory.
Definition
native_support.c:310
streq
int streq(const char *a, const char *b)
Tests whether two strings are exactly equal.
Definition
native_support.c:111
path_dirname
char * path_dirname(const char *path)
Returns the parent directory component of a path.
Definition
native_support.c:145
resolve_relative_to
char * resolve_relative_to(const char *base_dir, const char *value)
Resolves a path value relative to a base directory when needed.
Definition
native_support.c:294
read_file_text
char * read_file_text(const char *path)
Reads an entire file into a newly allocated text buffer.
Definition
native_support.c:318
path_exists
int path_exists(const char *path)
Tests whether a filesystem path currently exists.
Definition
native_support.c:168
native_support.h
Declares shared low-level support helpers used across the Pharos native runtime.
AppRuntime
Loaded runtime metadata for one Pharos app artifact.
Definition
native_runtime_types.h:37
AppRuntime::build_dir
char * build_dir
Definition
native_runtime_types.h:40
AppRuntime::error_report_path
char * error_report_path
Definition
native_runtime_types.h:48
AppRuntime::debug
char * debug
Definition
native_runtime_types.h:49
AppRuntime::artifact_dir
char * artifact_dir
Definition
native_runtime_types.h:41
AppRuntime::base_path
char * base_path
Definition
native_runtime_types.h:45
AppRuntime::app_id
char * app_id
Definition
native_runtime_types.h:38
AppRuntime::compiled_entrypoint
char * compiled_entrypoint
Definition
native_runtime_types.h:43
AppRuntime::app_root
char * app_root
Definition
native_runtime_types.h:39
AppRuntime::host_profile
char * host_profile
Definition
native_runtime_types.h:42
AppRuntime::state_path
char * state_path
Definition
native_runtime_types.h:44
AppRuntime::log_format
char * log_format
Definition
native_runtime_types.h:47
AppRuntime::log_path
char * log_path
Definition
native_runtime_types.h:46
AppRuntime::runtime_conf_path
char * runtime_conf_path
Definition
native_runtime_types.h:50
src
native_runtime
c
native_runtime_load.c
Generated by
1.17.0