|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + "github.com/gin-gonic/gin" |
| 10 | + "github.com/iloveicedgreentea/go-plex/internal/config" |
| 11 | + "github.com/iloveicedgreentea/go-plex/internal/logger" |
| 12 | +) |
| 13 | + |
| 14 | +var log = logger.GetLogger() |
| 15 | + |
| 16 | +func GenConfigPaths() (string, string) { |
| 17 | + ex, err := os.Executable() |
| 18 | + if err != nil { |
| 19 | + log.Error(err) |
| 20 | + } |
| 21 | + |
| 22 | + exPath := filepath.Dir(ex) |
| 23 | + configPath1 := "/data/config.json" // docker |
| 24 | + configPath2 := filepath.Join(exPath, "../config.json") // Fallback path (for local) |
| 25 | + |
| 26 | + log.Debugf("Config paths: %s, %s", configPath1, configPath2) |
| 27 | + |
| 28 | + return configPath1, configPath2 |
| 29 | +} |
| 30 | + |
| 31 | +// GetConfigPath returns the path to the config file |
| 32 | +func GetConfigPath() (string, error) { |
| 33 | + configPath1, configPath2 := GenConfigPaths() |
| 34 | + |
| 35 | + if _, err := os.Stat(configPath1); err == nil { |
| 36 | + return configPath1, nil |
| 37 | + } else if _, err := os.Stat(configPath2); err == nil { |
| 38 | + return configPath2, nil |
| 39 | + } |
| 40 | + return "", os.ErrNotExist |
| 41 | +} |
| 42 | + |
| 43 | +// ConfigExists checks if the config exists for the API |
| 44 | +func ConfigExists(c *gin.Context) { |
| 45 | + configPath, err := GetConfigPath() |
| 46 | + if err != nil { |
| 47 | + c.JSON(500, gin.H{"exists": false}) |
| 48 | + return |
| 49 | + } |
| 50 | + _, err = os.Stat(configPath) |
| 51 | + c.JSON(200, gin.H{"exists": err == nil}) |
| 52 | +} |
| 53 | + |
| 54 | +// Route for getting logs |
| 55 | +func GetLogs(c *gin.Context) { |
| 56 | + logFile, err := os.ReadFile("/data/application.log") |
| 57 | + if err != nil { |
| 58 | + c.JSON(500, gin.H{"error": "unable to read log file: " + err.Error()}) |
| 59 | + return |
| 60 | + } |
| 61 | + c.Data(200, "text/plain", logFile) |
| 62 | +} |
| 63 | + |
| 64 | +// GetConfig returns the config for the API |
| 65 | +func GetConfig(c *gin.Context) { |
| 66 | + path, err := GetConfigPath() |
| 67 | + // if not found, create it |
| 68 | + if err != nil { |
| 69 | + log.Debugf("Didn't get config: %v", err) |
| 70 | + err = CreateConfig(c) |
| 71 | + if err != nil { |
| 72 | + log.Debugf("Didn't create config: %v", err) |
| 73 | + c.JSON(500, gin.H{"error": "unable to create config"}) |
| 74 | + return |
| 75 | + } |
| 76 | + } |
| 77 | + data, err := os.ReadFile(path) |
| 78 | + if err != nil { |
| 79 | + log.Debugf("Didn't read config: %v", err) |
| 80 | + c.JSON(500, gin.H{"error": "unable to read config"}) |
| 81 | + return |
| 82 | + } |
| 83 | + c.Data(200, "application/json", data) |
| 84 | +} |
| 85 | + |
| 86 | +// CreateConfig creates a new config file |
| 87 | +func CreateConfig(c *gin.Context) error { |
| 88 | + log.Debug("Creating new config") |
| 89 | + configPath1, configPath2 := GenConfigPaths() |
| 90 | + |
| 91 | + // try to create config in the first path |
| 92 | + file, err := os.Create(configPath1) |
| 93 | + if err != nil { |
| 94 | + log.Debugf("Unable to create config in %s: %v", configPath1, err) |
| 95 | + // try to create config in the second path |
| 96 | + file, err = os.Create(configPath2) |
| 97 | + if err != nil { |
| 98 | + // if we can't create it in either path, return the error |
| 99 | + log.Errorf("Unable to create config in %s: %v", configPath2, err) |
| 100 | + return fmt.Errorf("unable to create config in %s or %s", configPath1, configPath2) |
| 101 | + } |
| 102 | + } |
| 103 | + defer file.Close() |
| 104 | + |
| 105 | + log.Debug("Successfully created config file") |
| 106 | + return nil |
| 107 | +} |
| 108 | + |
| 109 | +// SaveConfig saves the config for the API |
| 110 | +func SaveConfig(c *gin.Context) { |
| 111 | + var jsonData map[string]interface{} |
| 112 | + |
| 113 | + if err := c.ShouldBindJSON(&jsonData); err != nil { |
| 114 | + c.JSON(400, gin.H{"error": err.Error()}) |
| 115 | + fmt.Println(c.Request.Body) |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + path, err := GetConfigPath() |
| 120 | + if err != nil { |
| 121 | + log.Error("unable to get config") |
| 122 | + c.JSON(500, gin.H{"error": "unable to get config"}) |
| 123 | + return |
| 124 | + } |
| 125 | + |
| 126 | + // Loop through the incoming JSON map to set keys in Viper |
| 127 | + for key, value := range jsonData { |
| 128 | + switch v := value.(type) { |
| 129 | + case map[string]interface{}: |
| 130 | + for subKey, subValue := range v { |
| 131 | + config.Set(fmt.Sprintf("%s.%s", key, subKey), subValue) |
| 132 | + } |
| 133 | + default: |
| 134 | + config.Set(key, value) |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + // Use your SaveConfigFile function to save the updated configuration |
| 139 | + if err := config.SaveConfigFile(path); err != nil { |
| 140 | + log.Error("unable to save config") |
| 141 | + c.JSON(500, gin.H{"error": "Unable to save config"}) |
| 142 | + return |
| 143 | + } |
| 144 | + |
| 145 | + c.JSON(200, gin.H{"message": "Config saved successfully"}) |
| 146 | +} |
0 commit comments