Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F49509122
requests.rs
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
requests.rs
View Options
// -------------------------------------------------------------
// Alkane :: Server :: Requests
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Project: Nasqueron
// License: BSD-2-Clause
// -------------------------------------------------------------
use
axum
::
extract
::{
Path
,
State
};
use
axum
::
http
::
StatusCode
;
use
log
::{
debug
,
info
,
warn
};
use
limiting_factor_axum
::
api
::
guards
::
AxumRequestBody
as
RequestBody
;
use
limiting_factor_axum
::
api
::
replies
::{
ApiJsonResponse
,
ApiResponse
,
FailureResponse
};
use
limiting_factor_axum
::
api
::
replies
::
error_response
;
use
crate
::
actions
;
use
crate
::
config
::
AlkaneConfig
;
use
crate
::
deploy
::
DeployError
;
use
crate
::
runner
::
RecipeStatus
;
use
crate
::
services
::
validation
::
is_valid_site_name
;
// -------------------------------------------------------------
// Monitoring
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
pub
async
fn
status
()
->
&
'
static
str
{
"ALIVE"
}
// -------------------------------------------------------------
// Alkane requests
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
pub
async
fn
is_present
(
Path
(
site_name
):
Path
<
String
>
,
State
(
config
):
State
<
AlkaneConfig
>
,
)
->
ApiJsonResponse
<
bool
>
{
if
!
is_valid_site_name
(
&
site_name
)
{
return
invalid_site_name_response
(
&
site_name
);
}
actions
::
is_present
(
&
site_name
,
&
config
).
into_json_response
()
}
pub
async
fn
init
(
Path
(
site_name
):
Path
<
String
>
,
State
(
config
):
State
<
AlkaneConfig
>
,
context
:
RequestBody
,
)
->
ApiJsonResponse
<
RecipeStatus
>
{
if
!
is_valid_site_name
(
&
site_name
)
{
return
invalid_site_name_response
(
&
site_name
);
}
info
!
(
"Deploying {}"
,
&
site_name
);
let
context
=
context
.
into_optional_string
();
debug
!
(
"Context: {:?}"
,
&
context
);
actions
::
initialize
(
&
site_name
,
context
,
&
config
)
.
await
.
into_json_response
()
}
pub
async
fn
update
(
Path
(
site_name
):
Path
<
String
>
,
State
(
config
):
State
<
AlkaneConfig
>
,
context
:
RequestBody
,
)
->
ApiJsonResponse
<
RecipeStatus
>
{
if
!
is_valid_site_name
(
&
site_name
)
{
return
invalid_site_name_response
(
&
site_name
);
}
info
!
(
"Deploying {}"
,
&
site_name
);
let
context
=
context
.
into_optional_string
();
debug
!
(
"Context: {:?}"
,
&
context
);
actions
::
update
(
&
site_name
,
context
,
&
config
)
.
await
.
into_json_response
()
}
pub
async
fn
deploy
(
Path
(
site_name
):
Path
<
String
>
,
State
(
config
):
State
<
AlkaneConfig
>
,
context
:
RequestBody
,
)
->
ApiJsonResponse
<
RecipeStatus
>
{
if
!
is_valid_site_name
(
&
site_name
)
{
return
invalid_site_name_response
(
&
site_name
);
}
info
!
(
"Deploying {}"
,
&
site_name
);
let
context
=
context
.
into_optional_string
();
debug
!
(
"Context: {:?}"
,
&
context
);
actions
::
deploy
(
&
site_name
,
context
,
&
config
)
.
await
.
into_json_response
()
}
fn
invalid_site_name_response
<
T
>
(
site_name
:
&
str
)
->
ApiJsonResponse
<
T
>
where
T
:
ApiResponse
<
T
>
,
{
warn
!
(
"Invalid site name requested: {:?}"
,
site_name
);
error_response
(
StatusCode
::
BAD_REQUEST
,
"Invalid site name"
)
}
// -------------------------------------------------------------
// Custom error handling
//
// Deploy errors are returned as 400 + the Alkane error message
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
impl
FailureResponse
for
DeployError
{
fn
status_code
(
&
self
)
->
StatusCode
{
StatusCode
::
BAD_REQUEST
}
fn
response
(
&
self
)
->
String
{
warn
!
(
"{}"
,
self
);
// Server log
format!
(
"{}"
,
self
)
// API response
}
}
// -------------------------------------------------------------
// Tests
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#[cfg(test)]
mod
tests
{
use
super
::
*
;
#[tokio::test]
async
fn
invalid_site_name_returns_bad_request
()
{
let
config
=
AlkaneConfig
::
load
().
unwrap
();
let
response
=
is_present
(
Path
(
"../example.org"
.
to_string
()),
State
(
config
)).
await
;
assert_eq!
(
StatusCode
::
BAD_REQUEST
,
response
.
unwrap_err
().
0
);
}
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Sep 14, 16:29 (4 h, 46 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4027080
Default Alt Text
requests.rs (4 KB)
Attached To
Mode
rALK Alkane
Attached
Detach File
Event Timeline
Log In to Comment