|
| 1 | +package com.craftpilot.userservice.controller; |
| 2 | + |
| 3 | +import com.craftpilot.userservice.service.ModelDataLoader; |
| 4 | +import lombok.RequiredArgsConstructor; |
| 5 | +import lombok.extern.slf4j.Slf4j; |
| 6 | +import org.springframework.http.HttpStatus; |
| 7 | +import org.springframework.http.ResponseEntity; |
| 8 | +import org.springframework.web.bind.annotation.*; |
| 9 | +import reactor.core.publisher.Mono; |
| 10 | + |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +@RestController |
| 15 | +@RequestMapping("/api/admin/models") |
| 16 | +@RequiredArgsConstructor |
| 17 | +@Slf4j |
| 18 | +public class ModelLoaderController { |
| 19 | + |
| 20 | + private final ModelDataLoader modelDataLoader; |
| 21 | + |
| 22 | + @PostMapping("/reload") |
| 23 | + public Mono<ResponseEntity<Map<String, Object>>> reloadModels() { |
| 24 | + log.info("Model verilerini yeniden yükleme API çağrısı alındı"); |
| 25 | + |
| 26 | + return modelDataLoader.reloadModelData() |
| 27 | + .map(count -> { |
| 28 | + Map<String, Object> response = new HashMap<>(); |
| 29 | + response.put("success", true); |
| 30 | + response.put("message", "AI model verileri başarıyla yüklendi"); |
| 31 | + response.put("loadedModels", count); |
| 32 | + return ResponseEntity.ok(response); |
| 33 | + }) |
| 34 | + .onErrorResume(e -> { |
| 35 | + log.error("Model yeniden yüklenirken hata: {}", e.getMessage(), e); |
| 36 | + Map<String, Object> response = new HashMap<>(); |
| 37 | + response.put("success", false); |
| 38 | + response.put("message", "Model yükleme hatası: " + e.getMessage()); |
| 39 | + return Mono.just(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response)); |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + @GetMapping("/status") |
| 44 | + public ResponseEntity<Map<String, Object>> getModelLoaderStatus() { |
| 45 | + log.info("Model yükleme durumu API çağrısı alındı"); |
| 46 | + return ResponseEntity.ok(modelDataLoader.getModelLoadingStatus()); |
| 47 | + } |
| 48 | + |
| 49 | + @PostMapping("/load-from-file") |
| 50 | + public Mono<ResponseEntity<Map<String, Object>>> loadFromCustomFile(@RequestBody Map<String, String> request) { |
| 51 | + String filePath = request.get("filePath"); |
| 52 | + if (filePath == null || filePath.isEmpty()) { |
| 53 | + Map<String, Object> response = new HashMap<>(); |
| 54 | + response.put("success", false); |
| 55 | + response.put("message", "Dosya yolu belirtilmedi"); |
| 56 | + return Mono.just(ResponseEntity.badRequest().body(response)); |
| 57 | + } |
| 58 | + |
| 59 | + log.info("Özel dosyadan model yükleme başlatılıyor: {}", filePath); |
| 60 | + |
| 61 | + // Dosya yolu güvenlik kontrolü |
| 62 | + if (!filePath.startsWith("classpath:") && !filePath.startsWith("file:") && !filePath.startsWith("http")) { |
| 63 | + filePath = "file:" + filePath; |
| 64 | + } |
| 65 | + |
| 66 | + final String validatedFilePath = filePath; |
| 67 | + |
| 68 | + return modelDataLoader.loadModelsFromFile(validatedFilePath) |
| 69 | + .map(count -> { |
| 70 | + Map<String, Object> response = new HashMap<>(); |
| 71 | + response.put("success", true); |
| 72 | + response.put("message", "Özel dosyadan AI model verileri başarıyla yüklendi"); |
| 73 | + response.put("loadedModels", count); |
| 74 | + response.put("filePath", validatedFilePath); |
| 75 | + response.put("timestamp", System.currentTimeMillis()); |
| 76 | + return ResponseEntity.ok(response); |
| 77 | + }) |
| 78 | + .onErrorResume(e -> { |
| 79 | + log.error("Özel dosyadan model yüklenirken hata: {}", e.getMessage(), e); |
| 80 | + Map<String, Object> response = new HashMap<>(); |
| 81 | + response.put("success", false); |
| 82 | + response.put("message", "Özel dosyadan model yükleme hatası: " + e.getMessage()); |
| 83 | + response.put("filePath", validatedFilePath); |
| 84 | + response.put("errorType", e.getClass().getSimpleName()); |
| 85 | + response.put("timestamp", System.currentTimeMillis()); |
| 86 | + return Mono.just(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response)); |
| 87 | + }); |
| 88 | + } |
| 89 | +} |
0 commit comments