|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "io/fs" |
| 5 | + "io/ioutil" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + |
| 10 | + "github.com/mirkobrombin/goup/internal/config" |
| 11 | +) |
| 12 | + |
| 13 | +func getLogsHandler(w http.ResponseWriter, r *http.Request) { |
| 14 | + logFile := os.Getenv("GOUP_LOG_FILE") |
| 15 | + if logFile == "" { |
| 16 | + http.Error(w, "Log file not set", http.StatusNotFound) |
| 17 | + return |
| 18 | + } |
| 19 | + data, err := ioutil.ReadFile(logFile) |
| 20 | + if err != nil { |
| 21 | + http.Error(w, "Unable to read log file", http.StatusInternalServerError) |
| 22 | + return |
| 23 | + } |
| 24 | + w.Header().Set("Content-Type", "text/plain") |
| 25 | + w.Write(data) |
| 26 | +} |
| 27 | + |
| 28 | +func getMetricsHandler(w http.ResponseWriter, r *http.Request) { |
| 29 | + metrics := map[string]interface{}{ |
| 30 | + "requests_total": 1234, |
| 31 | + "latency_avg_ms": 45.6, |
| 32 | + "cpu_usage": 23.4, |
| 33 | + "ram_usage_mb": 512, |
| 34 | + "active_sites": len(config.SiteConfigs), |
| 35 | + "active_plugins": len(config.GlobalConf.EnabledPlugins), |
| 36 | + } |
| 37 | + jsonResponse(w, metrics) |
| 38 | +} |
| 39 | + |
| 40 | +func getStatusHandler(w http.ResponseWriter, r *http.Request) { |
| 41 | + status := map[string]interface{}{ |
| 42 | + "uptime": "72h", |
| 43 | + "sites": len(config.SiteConfigs), |
| 44 | + "plugins": config.GlobalConf.EnabledPlugins, |
| 45 | + "apiAlive": true, |
| 46 | + } |
| 47 | + jsonResponse(w, status) |
| 48 | +} |
| 49 | + |
| 50 | +func getLogWeightHandler(w http.ResponseWriter, r *http.Request) { |
| 51 | + logDir := config.GetLogDir() |
| 52 | + var totalSize int64 = 0 |
| 53 | + err := filepath.Walk(logDir, func(_ string, info fs.FileInfo, err error) error { |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + if !info.IsDir() { |
| 58 | + totalSize += info.Size() |
| 59 | + } |
| 60 | + return nil |
| 61 | + }) |
| 62 | + if err != nil { |
| 63 | + http.Error(w, "Error calculating log weight", http.StatusInternalServerError) |
| 64 | + return |
| 65 | + } |
| 66 | + jsonResponse(w, map[string]interface{}{ |
| 67 | + "log_weight_bytes": totalSize, |
| 68 | + }) |
| 69 | +} |
| 70 | + |
| 71 | +func getPluginUsageHandler(w http.ResponseWriter, r *http.Request) { |
| 72 | + usage := make(map[string]int) |
| 73 | + for _, site := range config.SiteConfigs { |
| 74 | + for pluginName := range site.PluginConfigs { |
| 75 | + usage[pluginName]++ |
| 76 | + } |
| 77 | + } |
| 78 | + jsonResponse(w, usage) |
| 79 | +} |
0 commit comments