summaryrefslogtreecommitdiff
path: root/java/Main.java
blob: 76cb839ee6d3a91d912108a272be266555affc0d (plain)
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
package org.lambdanantes.lcgoji;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.fluent.Response;
import org.apache.http.entity.StringEntity;

import static spark.Spark.*;

@Slf4j
public class Main {

    public static final String TEAM_NAME = "LCGOJI";
    public static final int SELF_PORT = 8888;
    public static final String SELF_URL = "http://127.0.0.1:" + SELF_PORT;
    public static final String TESTER_URL = "http://127.0.0.1:8080";

    public static void main(String[] args) throws Exception {
        port(SELF_PORT);

        before((request, response) -> log.info("Requête entrante : " + request.requestMethod() + " " + request.pathInfo() + ", query params : " + request.queryString()));

        // API pour l'évaluation de λ-term
        // Le body est une S-expression sous sa forme textuelle
        post("/eval", (request, response) -> {
            String body = request.body();
            log.info("Demande d'évaluation de l'expression : " + body);

            // TODO Parser, contruire l'AST, l'évaluer
            String result = body; // Renvoie la s-expression à l'identique pour le moment

            log.info("Réponse envoyée : " + body);

            return result.getBytes();
        });

        init();

        // Enregistrement de notre API auprès du tester d'API
        String jsonBody = "{\"url\":\"" + SELF_URL + "/eval\", \"name\": \"" + TEAM_NAME + "\"}";
        Response response = Request.Post(TESTER_URL + "/register")
                .addHeader("Content-type", "application/json")
                .body(new StringEntity(jsonBody))
                .execute();

        log.info("Résultat de l'enregistrement : "+response.returnContent().toString());
    }

}