You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.7 KiB
Kotlin
45 lines
1.7 KiB
Kotlin
package org.domaindrivenarchitecture.provs.framework.extensions.server_software.standalone_server.nginx.base
|
|
|
|
import org.domaindrivenarchitecture.provs.framework.core.Prov
|
|
import org.domaindrivenarchitecture.provs.framework.core.ProvResult
|
|
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.*
|
|
|
|
|
|
internal const val locationsAvailableDir = "/etc/nginx/locations-available/"
|
|
internal const val locationsEnabledDir = "/etc/nginx/locations-enabled/"
|
|
internal const val locationsFileExtension = ".locations"
|
|
|
|
|
|
fun Prov.createNginxLocationFolders() = requireAll {
|
|
createDirs(locationsEnabledDir, sudo = true)
|
|
createDirs(locationsAvailableDir, sudo = true)
|
|
}
|
|
|
|
|
|
fun Prov.nginxIncludeLocationFolders() = requireAll {
|
|
replaceTextInFile("/etc/nginx/nginx.conf", "listen 80;\n",
|
|
"""listen 80;
|
|
include ${locationsAvailableDir}port80*$locationsFileExtension;
|
|
include ${locationsEnabledDir}port443*$locationsFileExtension;
|
|
""")
|
|
}
|
|
|
|
|
|
fun Prov.nginxAddLocation(port: String, locationFileName: String, urlPath: String, content: String) = requireAll {
|
|
|
|
val locationConf = """location $urlPath {""" +
|
|
content +
|
|
"\n}"
|
|
|
|
if (!dirExists(locationsAvailableDir, sudo = true)) {
|
|
createNginxLocationFolders()
|
|
}
|
|
|
|
createFile("${locationsAvailableDir}port${port}_$locationFileName$locationsFileExtension", locationConf, sudo = true)
|
|
if (!fileExists("${locationsEnabledDir}port${port}_$locationFileName$locationsFileExtension", sudo = true)) {
|
|
cmd("sudo ln -s ${locationsAvailableDir}port${port}_$locationFileName$locationsFileExtension ${locationsEnabledDir}port${port}_$locationFileName$locationsFileExtension ")
|
|
} else {
|
|
ProvResult(true)
|
|
}
|
|
}
|