1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*!
Handlers for HTTP requests to `/api/v0`.

This documents all endpoints. Select a module below for a namespace.

*/

// tracing::instrument seems to have issues with this
#![allow(clippy::suspicious_else_formatting)]

pub mod auth;
pub mod cms;
pub mod course_material;
pub mod exercise_services;
pub mod files;
pub mod healthz;
pub mod helpers;
pub mod langs;
pub mod main_frontend;
pub mod other_domain_redirects;
pub mod study_registry;
pub mod tmc_server;

use crate::domain::error::{ControllerError, ControllerErrorType};
use actix_web::{
    web::{self, ServiceConfig},
    HttpRequest, HttpResponse, ResponseError,
};
use headless_lms_utils::prelude::*;
use serde::{Deserialize, Serialize};
#[cfg(feature = "ts_rs")]
use ts_rs::TS;

/// Result of a image upload. Tells where the uploaded image can be retrieved from.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[cfg_attr(feature = "ts_rs", derive(TS))]
pub struct UploadResult {
    pub url: String,
}

/// Add controllers from all the submodules.
pub fn configure_controllers(cfg: &mut ServiceConfig) {
    cfg.service(web::scope("/course-material").configure(course_material::_add_routes))
        .service(web::scope("/cms").configure(cms::_add_routes))
        .service(web::scope("/files").configure(files::_add_routes))
        .service(web::scope("/main-frontend").configure(main_frontend::_add_routes))
        .service(web::scope("/auth").configure(auth::_add_routes))
        .service(web::scope("/study-registry").configure(study_registry::_add_routes))
        .service(web::scope("/exercise-services").configure(exercise_services::_add_routes))
        .service(
            web::scope("/other-domain-redirects").configure(other_domain_redirects::_add_routes),
        )
        .service(web::scope("/healthz").configure(healthz::_add_routes))
        .service(web::scope("/langs").configure(langs::_add_routes))
        .service(web::scope("/tmc-server").configure(tmc_server::_add_routes))
        .default_service(web::to(not_found));
}

async fn not_found(req: HttpRequest) -> HttpResponse {
    ControllerError::new(
        ControllerErrorType::NotFound,
        format!("No handler found for route '{}'", req.path()),
        None,
    )
    .error_response()
}