Pharos
0.7.23
Modern Web Framework & Web App Hosting Service.
Toggle main menu visibility
Loading...
Searching...
No Matches
native_serve_config.c
Go to the documentation of this file.
1
/**
2
* @file
3
* @brief Parses native serve CLI arguments and applies runtime-conf-derived defaults to ServeConfig.
4
*/
5
6
#include "
native_serve_config.h
"
7
8
#include "
native_json_config.h
"
9
#include "
native_support.h
"
10
11
#include <stdlib.h>
12
#include <string.h>
13
14
#ifdef _WIN32
15
#define native_serve_config_strdup _strdup
16
#else
17
#define native_serve_config_strdup strdup
18
#endif
19
20
int
parse_serve_config_native
(
int
argc,
char
**argv,
ServeConfig
*config) {
21
int
index = 0;
22
memset(config, 0,
sizeof
(*config));
23
config->
host
=
native_serve_config_strdup
(
"127.0.0.1"
);
24
config->
port
= NULL;
25
while
(index < argc) {
26
const
char
*arg = argv[index];
27
if
(config->
kind
== NULL && (
streq
(arg,
"website"
) ||
streq
(arg,
"app"
) ||
streq
(arg,
"runtime"
))) {
28
config->
kind
=
native_serve_config_strdup
(arg);
29
if
(index + 1 < argc && argv[index + 1][0] !=
'-'
) {
30
config->
target_id
=
native_serve_config_strdup
(argv[index + 1]);
31
index += 2;
32
continue
;
33
}
34
index++;
35
continue
;
36
}
37
if
(
streq
(arg,
"--host"
) && index + 1 < argc) {
38
free(config->
host
);
39
config->
host
=
native_serve_config_strdup
(argv[index + 1]);
40
index += 2;
41
continue
;
42
}
43
if
(
streq
(arg,
"--port"
) && index + 1 < argc) {
44
free(config->
port
);
45
config->
port
=
native_serve_config_strdup
(argv[index + 1]);
46
index += 2;
47
continue
;
48
}
49
if
(
streq
(arg,
"--root"
) && index + 1 < argc) {
50
config->
root
=
native_serve_config_strdup
(argv[index + 1]);
51
index += 2;
52
continue
;
53
}
54
if
(
streq
(arg,
"--conf"
) && index + 1 < argc) {
55
config->
conf_path
=
native_serve_config_strdup
(argv[index + 1]);
56
index += 2;
57
continue
;
58
}
59
if
(
streq
(arg,
"--app-dir"
) && index + 1 < argc) {
60
config->
app_dir
=
native_serve_config_strdup
(argv[index + 1]);
61
index += 2;
62
continue
;
63
}
64
if
(
streq
(arg,
"--build-dir"
) && index + 1 < argc) {
65
config->
build_dir
=
native_serve_config_strdup
(argv[index + 1]);
66
index += 2;
67
continue
;
68
}
69
if
(
streq
(arg,
"--state-dir"
) && index + 1 < argc) {
70
config->
state_dir
=
native_serve_config_strdup
(argv[index + 1]);
71
index += 2;
72
continue
;
73
}
74
if
(
streq
(arg,
"--state-path"
) && index + 1 < argc) {
75
config->
state_path
=
native_serve_config_strdup
(argv[index + 1]);
76
index += 2;
77
continue
;
78
}
79
if
(
streq
(arg,
"--base-path"
) && index + 1 < argc) {
80
config->
base_path
=
native_serve_config_strdup
(argv[index + 1]);
81
index += 2;
82
continue
;
83
}
84
if
(
streq
(arg,
"--log-path"
) && index + 1 < argc) {
85
config->
log_path
=
native_serve_config_strdup
(argv[index + 1]);
86
index += 2;
87
continue
;
88
}
89
if
(
streq
(arg,
"--debug"
) && index + 1 < argc) {
90
config->
debug
=
native_serve_config_strdup
(argv[index + 1]);
91
index += 2;
92
continue
;
93
}
94
if
(
streq
(arg,
"--socket-path"
) && index + 1 < argc) {
95
config->
socket_path
=
native_serve_config_strdup
(argv[index + 1]);
96
index += 2;
97
continue
;
98
}
99
if
(
streq
(arg,
"--seed-state"
)) {
100
config->
seed_state
= 1;
101
index++;
102
continue
;
103
}
104
index++;
105
}
106
return
0;
107
}
108
109
void
free_serve_config_native
(
ServeConfig
*config) {
110
if
(config == NULL) {
111
return
;
112
}
113
free(config->
kind
);
114
free(config->
target_id
);
115
free(config->
conf_path
);
116
free(config->
app_dir
);
117
free(config->
build_dir
);
118
free(config->
state_dir
);
119
free(config->
state_path
);
120
free(config->
base_path
);
121
free(config->
log_path
);
122
free(config->
debug
);
123
free(config->
socket_path
);
124
free(config->
host
);
125
free(config->
port
);
126
free(config->
root
);
127
memset(config, 0,
sizeof
(*config));
128
}
129
130
void
apply_serve_config_defaults_from_conf_native
(
ServeConfig
*config) {
131
char
*runtime_conf_path;
132
char
*runtime_conf_dir;
133
char
*host_value;
134
char
*port_value;
135
char
*socket_value;
136
char
*resolved_socket_path = NULL;
137
char
*cwd;
138
if
(config == NULL || config->
conf_path
== NULL || config->
conf_path
[0] ==
'\0'
) {
139
return
;
140
}
141
cwd =
current_working_directory
();
142
if
(cwd == NULL) {
143
runtime_conf_path =
native_serve_config_strdup
(config->
conf_path
);
144
}
else
{
145
runtime_conf_path =
resolve_relative_to
(cwd, config->
conf_path
);
146
free(cwd);
147
}
148
if
(runtime_conf_path == NULL) {
149
return
;
150
}
151
runtime_conf_dir =
path_dirname
(runtime_conf_path);
152
if
(config->
socket_path
== NULL || config->
socket_path
[0] ==
'\0'
) {
153
socket_value =
conf_lookup_value
(runtime_conf_path,
"socket_path"
);
154
if
(socket_value != NULL && socket_value[0] !=
'\0'
) {
155
resolved_socket_path =
resolve_relative_to
(runtime_conf_dir, socket_value);
156
if
(resolved_socket_path != NULL) {
157
free(config->
socket_path
);
158
config->
socket_path
= resolved_socket_path;
159
resolved_socket_path = NULL;
160
}
161
}
162
free(socket_value);
163
}
164
if
(config->
port
== NULL || config->
port
[0] ==
'\0'
) {
165
port_value =
conf_lookup_value
(runtime_conf_path,
"port"
);
166
if
(port_value != NULL && port_value[0] !=
'\0'
) {
167
free(config->
port
);
168
config->
port
=
native_serve_config_strdup
(port_value);
169
}
170
free(port_value);
171
}
172
if
(config->
host
== NULL || config->
host
[0] ==
'\0'
||
streq
(config->
host
,
"127.0.0.1"
)) {
173
host_value =
conf_lookup_value
(runtime_conf_path,
"host"
);
174
if
(host_value != NULL && host_value[0] !=
'\0'
) {
175
free(config->
host
);
176
config->
host
=
native_serve_config_strdup
(host_value);
177
}
178
free(host_value);
179
}
180
free(resolved_socket_path);
181
free(runtime_conf_dir);
182
free(runtime_conf_path);
183
}
conf_lookup_value
char * conf_lookup_value(const char *path, const char *key)
Reads a Pharos conf file and looks up one key.
Definition
native_json_config.c:179
native_json_config.h
Declares lightweight JSON and conf value lookup helpers.
native_serve_config_strdup
#define native_serve_config_strdup
Definition
native_serve_config.c:17
parse_serve_config_native
int parse_serve_config_native(int argc, char **argv, ServeConfig *config)
Parses native serve CLI arguments into a serve configuration record.
Definition
native_serve_config.c:20
apply_serve_config_defaults_from_conf_native
void apply_serve_config_defaults_from_conf_native(ServeConfig *config)
Applies runtime-config-derived default values to an already parsed serve config.
Definition
native_serve_config.c:130
free_serve_config_native
void free_serve_config_native(ServeConfig *config)
Releases heap-owned fields in a serve configuration record.
Definition
native_serve_config.c:109
native_serve_config.h
Declares helpers for parsing and normalizing native serve configuration.
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
native_support.h
Declares shared low-level support helpers used across the Pharos native runtime.
ServeConfig
Parsed native serve configuration used to bootstrap the runtime listener.
Definition
native_runtime_types.h:16
ServeConfig::root
char * root
Definition
native_runtime_types.h:30
ServeConfig::socket_path
char * socket_path
Definition
native_runtime_types.h:27
ServeConfig::host
char * host
Definition
native_runtime_types.h:28
ServeConfig::state_path
char * state_path
Definition
native_runtime_types.h:23
ServeConfig::log_path
char * log_path
Definition
native_runtime_types.h:25
ServeConfig::build_dir
char * build_dir
Definition
native_runtime_types.h:21
ServeConfig::port
char * port
Definition
native_runtime_types.h:29
ServeConfig::app_dir
char * app_dir
Definition
native_runtime_types.h:20
ServeConfig::base_path
char * base_path
Definition
native_runtime_types.h:24
ServeConfig::conf_path
char * conf_path
Definition
native_runtime_types.h:19
ServeConfig::kind
char * kind
Definition
native_runtime_types.h:17
ServeConfig::debug
char * debug
Definition
native_runtime_types.h:26
ServeConfig::seed_state
int seed_state
Definition
native_runtime_types.h:31
ServeConfig::state_dir
char * state_dir
Definition
native_runtime_types.h:22
ServeConfig::target_id
char * target_id
Definition
native_runtime_types.h:18
src
native_runtime
c
native_serve_config.c
Generated by
1.17.0