Mise en place docker + mise en place des settings (config ollama / 1min.ai)
This commit is contained in:
@@ -1,28 +1,37 @@
|
||||
package com.loremind.infrastructure.web.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.cors.CorsConfiguration;
|
||||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
||||
import org.springframework.web.filter.CorsFilter;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Configuration CORS pour autoriser les requêtes depuis le Frontend Angular.
|
||||
* Adaptateur d'infrastructure qui configure la politique CORS.
|
||||
* Configuration CORS. Origines configurables via la propriete
|
||||
* `app.cors.allowed-origins` (liste separee par virgules) ou l'env var
|
||||
* APP_CORS_ALLOWED_ORIGINS. Defaut : Angular dev server + port Docker par defaut.
|
||||
*/
|
||||
@Configuration
|
||||
public class CorsConfig {
|
||||
|
||||
@Value("${app.cors.allowed-origins:http://localhost:4200,http://localhost:8081}")
|
||||
private String allowedOrigins;
|
||||
|
||||
@Bean
|
||||
public CorsFilter corsFilter() {
|
||||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
||||
CorsConfiguration config = new CorsConfiguration();
|
||||
|
||||
// Autoriser les requêtes depuis localhost:4200 (Angular dev server)
|
||||
config.addAllowedOrigin("http://localhost:4200");
|
||||
|
||||
Arrays.stream(allowedOrigins.split(","))
|
||||
.map(String::trim)
|
||||
.filter(s -> !s.isEmpty())
|
||||
.forEach(config::addAllowedOrigin);
|
||||
config.addAllowedHeader("*");
|
||||
config.addAllowedMethod("*");
|
||||
|
||||
|
||||
source.registerCorsConfiguration("/**", config);
|
||||
return new CorsFilter(source);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.loremind.infrastructure.web.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Proxy fin entre le frontend Angular et les endpoints /settings du Brain Python.
|
||||
*
|
||||
* Ce controller n'a aucune logique metier propre : il transfere les requetes
|
||||
* telles-quelles. Raison d'etre : eviter d'exposer le Brain (port 8000) au
|
||||
* navigateur et centraliser CORS sur Spring.
|
||||
*
|
||||
* Les payloads sont passes en Map<String,Object> pour rester tolerant aux
|
||||
* evolutions du schema cote Brain (ajout de champs sans recompiler le Core).
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/settings")
|
||||
public class SettingsController {
|
||||
|
||||
private final RestTemplate restTemplate;
|
||||
private final String brainBaseUrl;
|
||||
|
||||
public SettingsController(RestTemplate restTemplate,
|
||||
@Value("${brain.base-url}") String brainBaseUrl) {
|
||||
this.restTemplate = restTemplate;
|
||||
this.brainBaseUrl = brainBaseUrl;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public ResponseEntity<Map<String, Object>> getSettings() {
|
||||
return forward(HttpMethod.GET, "/settings", null);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public ResponseEntity<Map<String, Object>> updateSettings(@RequestBody Map<String, Object> patch) {
|
||||
return forward(HttpMethod.PUT, "/settings", patch);
|
||||
}
|
||||
|
||||
@GetMapping("/models/ollama")
|
||||
public ResponseEntity<Map<String, Object>> listOllamaModels() {
|
||||
return forward(HttpMethod.GET, "/models/ollama", null);
|
||||
}
|
||||
|
||||
@GetMapping("/models/onemin")
|
||||
public ResponseEntity<Map<String, Object>> listOneMinModels() {
|
||||
return forward(HttpMethod.GET, "/models/onemin", null);
|
||||
}
|
||||
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
private ResponseEntity<Map<String, Object>> forward(HttpMethod method, String path, Object body) {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
HttpEntity<Object> entity = new HttpEntity<>(body, headers);
|
||||
ResponseEntity<Map> response = restTemplate.exchange(
|
||||
brainBaseUrl + path, method, entity, Map.class);
|
||||
return ResponseEntity.status(response.getStatusCode()).body((Map<String, Object>) response.getBody());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user