mirror of https://github.com/micromata/borgbackup-butler.git

Kai Reinhard
17.56.2021 812f12a69468d5b20fef7afc6cd82c130481e9ad
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package de.micromata.borgbutler.server.rest
 
import de.micromata.borgbutler.cache.ButlerCache
import de.micromata.borgbutler.config.ConfigurationHandler
import de.micromata.borgbutler.json.JsonUtils
import de.micromata.borgbutler.server.BorgInstallation
import de.micromata.borgbutler.server.ServerConfiguration
import de.micromata.borgbutler.server.user.UserData
import de.micromata.borgbutler.server.user.UserManager
import mu.KotlinLogging
import org.apache.commons.lang3.StringUtils
import org.springframework.web.bind.annotation.*
 
private val log = KotlinLogging.logger {}
 
@RestController
@RequestMapping("/rest/configuration")
class ConfigurationRest {
 
    /**
     * @param prettyPrinter If true then the json output will be in pretty format.
     * @see JsonUtils.toJson
     */
    @GetMapping("config")
    fun getConfig(@RequestParam("prettyPrinter", required = false) prettyPrinter: Boolean?): String {
        val configurationInfo = ConfigurationInfo()
        configurationInfo.serverConfiguration = ServerConfiguration.get()
        configurationInfo.borgVersion = BorgInstallation.getInstance().getBorgVersion()
        return JsonUtils.toJson(configurationInfo, prettyPrinter)
    }
 
    @PostMapping("config")
    fun setConfig(@RequestBody configurationInfo: ConfigurationInfo) {
        val configurationHandler = ConfigurationHandler.getInstance()
        BorgInstallation.getInstance()
            .configure(configurationInfo.serverConfiguration, configurationInfo.borgVersion?.borgBinary)
        val configuration: ServerConfiguration = ServerConfiguration.get()
        configurationInfo.serverConfiguration?.let {
            configuration.copyFrom(it)
        }
        configurationHandler.save()
    }
 
    /**
     * @param prettyPrinter If true then the json output will be in pretty format.
     * @see JsonUtils.toJson
     */
    @GetMapping("user")
    fun getUser(@RequestParam("prettyPrinter", required = false) prettyPrinter: Boolean?): String {
        val user: UserData = RestUtils.getUser()
        return JsonUtils.toJson(user, prettyPrinter)
    }
 
    @PostMapping("user")
    fun setUser(@RequestBody user: UserData) {
        if (user.getLocale() != null && StringUtils.isBlank(user.getLocale().getLanguage())) {
            // Don't set locale with "" as language.
            user.setLocale(null)
        }
        if (StringUtils.isBlank(user.getDateFormat())) {
            // Don't set dateFormat as "".
            user.setDateFormat(null)
        }
        UserManager.instance().saveUser(user)
    }
 
    /**
     * Resets the settings to default values (deletes all settings).
     */
    @GetMapping("clearAllCaches")
    fun clearAllCaches(): String {
        log.info("Clear all caches called...")
        ButlerCache.getInstance().clearAllCaches()
        return "OK"
    }
}