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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//! Routes in the backend.

use actix_utils::future::ready;
use actix_web::{middleware::NormalizePath, web};
use actix_web_httpauth::middleware::HttpAuthentication;

use crate::{
    config::auth::middleware::validator,
    controller::{
        areas, base_layer_image, blossoms, config, drawings, guided_tours, layers, map,
        map_collaborators, plant_layer, plantings, plants, seed, sse, timeline, users,
    },
};

/// Defines all routes of the backend and which functions they map to.
pub fn config(cfg: &mut web::ServiceConfig) {
    let auth = HttpAuthentication::bearer(|req, credentials| ready(validator(req, &credentials)));

    let routes = web::scope("/api")
        .service(
            web::scope("/seeds")
                .service(seed::find)
                .service(seed::create)
                .service(seed::delete_by_id)
                .service(seed::find_by_id)
                .service(seed::edit_by_id)
                .service(seed::archive),
        )
        .service(
            web::scope("/plants")
                .service(plants::find)
                .service(plants::find_by_id),
        )
        .service(
            web::scope("/maps")
                .service(map::find)
                .service(map::find_by_id)
                .service(map::create)
                .service(map::update)
                .service(map::update_geometry)
                .service(map::delete_by_id)
                .service(
                    web::scope("/{map_id}")
                        .service(
                            web::scope("/layers")
                                .service(layers::find)
                                .service(layers::find_by_id)
                                .service(layers::create)
                                .service(layers::update)
                                .service(layers::delete)
                                .service(
                                    web::scope("/base/images")
                                        .service(base_layer_image::create)
                                        .service(base_layer_image::update)
                                        .service(base_layer_image::delete),
                                )
                                .service(
                                    web::scope("/base/{layer_id}/images")
                                        .service(base_layer_image::find),
                                )
                                .service(
                                    web::scope("/plants")
                                        .service(plant_layer::heatmap)
                                        .service(plant_layer::find_relations)
                                        .service(
                                            web::scope("/plantings")
                                                .service(plantings::find)
                                                .service(plantings::create)
                                                .service(plantings::update)
                                                .service(plantings::delete),
                                        ),
                                ),
                        )
                        .service(
                            web::scope("/drawings")
                                .service(drawings::find)
                                .service(drawings::create)
                                .service(drawings::update)
                                .service(drawings::delete),
                        )
                        .service(
                            web::scope("/collaborators")
                                .service(map_collaborators::create)
                                .service(map_collaborators::find)
                                .service(map_collaborators::delete),
                        )
                        .service(
                            web::scope("/areas")
                                .service(areas::create)
                                .service(areas::find)
                                .service(areas::update)
                                .service(areas::delete),
                        )
                        .service(timeline::get_timeline),
                ),
        )
        .service(
            web::scope("/tours")
                .service(guided_tours::setup)
                .service(guided_tours::find_by_user)
                .service(guided_tours::update),
        )
        .service(
            web::scope("/users")
                .service(users::create)
                .service(users::find),
        )
        .service(web::scope("/blossoms").service(blossoms::gain))
        .service(web::scope("/timeline").service(timeline::get_timeline))
        .service(web::scope("/updates/maps").service(sse::connect_to_map))
        .wrap(NormalizePath::trim())
        .wrap(auth);

    let config_route = web::scope("/api/config").service(config::get);

    cfg.service(config_route).service(routes);
}