-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
381 changed files
with
9,370 additions
and
7,095 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,34 @@ | ||
package nebulosa.api | ||
|
||
import com.github.rvesse.airline.SingleCommand | ||
import com.sun.jna.Platform | ||
import nebulosa.time.SystemClock | ||
import org.springframework.boot.runApplication | ||
import java.nio.file.Path | ||
import java.time.LocalDate | ||
import java.time.format.DateTimeFormatter | ||
import java.util.* | ||
import javax.swing.filechooser.FileSystemView | ||
import kotlin.io.path.Path | ||
import kotlin.io.path.createDirectories | ||
import kotlin.io.path.deleteIfExists | ||
import kotlin.io.path.exists | ||
import kotlin.io.path.listDirectoryEntries | ||
|
||
const val APP_DIR_KEY = "app.dir" | ||
|
||
fun initAppDirectory(): Path { | ||
val appPath = when { | ||
Platform.isLinux() -> Path.of(System.getProperty("user.home"), ".nebulosa") | ||
Platform.isWindows() -> Path.of(FileSystemView.getFileSystemView().defaultDirectory.path, "Nebulosa") | ||
Platform.isLinux() -> Path(System.getProperty("user.home"), ".nebulosa") | ||
Platform.isWindows() -> Path(FileSystemView.getFileSystemView().defaultDirectory.path, "Nebulosa") | ||
else -> throw IllegalStateException("unsupported operating system") | ||
} | ||
|
||
appPath.createDirectories() | ||
System.setProperty("app.dir", "$appPath") | ||
return appPath | ||
} | ||
|
||
private fun Path.clearLogIfPastDays(days: Long = 7L) { | ||
if (exists()) { | ||
val pastDays = LocalDate.now(SystemClock).minusDays(days) | ||
|
||
for (entry in listDirectoryEntries("nebulosa-*.log")) { | ||
val logDate = entry.fileName.toString() | ||
.replace("nebulosa-", "") | ||
.replace(".log", "") | ||
.let { runCatching { LocalDate.parse(it, DateTimeFormatter.ISO_LOCAL_DATE) }.getOrNull() } | ||
?: continue | ||
|
||
if (pastDays.isAfter(logDate)) { | ||
entry.deleteIfExists() | ||
} | ||
} | ||
} | ||
.createDirectories() | ||
.also { System.setProperty(APP_DIR_KEY, "$it") } | ||
} | ||
|
||
fun main(args: Array<String>) { | ||
with(initAppDirectory()) { | ||
Path.of("$this", "logs").createDirectories().clearLogIfPastDays() | ||
Path.of("$this", "data").createDirectories().also { System.setProperty("DATA_PATH", "$it") } | ||
} | ||
initAppDirectory() | ||
|
||
// Sets default locale to en_US. | ||
Locale.setDefault(Locale.ENGLISH) | ||
|
||
// Run the Spring Boot application. | ||
runApplication<Nebulosa>(*args) | ||
val parser = SingleCommand.singleCommand(Nebulosa::class.java) | ||
val nebulosa = parser.parse(*args) | ||
nebulosa.run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,82 @@ | ||
package nebulosa.api | ||
|
||
import ch.qos.logback.classic.Level | ||
import com.fasterxml.jackson.databind.DeserializationFeature | ||
import com.fasterxml.jackson.databind.SerializationFeature | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule | ||
import com.fasterxml.jackson.module.kotlin.jsonMapper | ||
import com.github.rvesse.airline.annotations.Command | ||
import com.github.rvesse.airline.annotations.Option | ||
import io.javalin.Javalin | ||
import io.javalin.json.JavalinJackson | ||
import nebulosa.api.atlas.Location | ||
import nebulosa.api.beans.modules.DeviceModule | ||
import nebulosa.api.inject.* | ||
import nebulosa.json.PathModule | ||
import nebulosa.log.loggerFor | ||
import org.springframework.boot.autoconfigure.SpringBootApplication | ||
import org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent | ||
import org.springframework.context.event.EventListener | ||
import org.koin.core.context.startKoin | ||
import org.slf4j.LoggerFactory | ||
import java.util.concurrent.ConcurrentHashMap | ||
|
||
@SpringBootApplication | ||
class Nebulosa { | ||
@Command(name = "nebulosa") | ||
class Nebulosa : Runnable, AutoCloseable { | ||
|
||
@EventListener | ||
private fun onServletWebServerInitializedEvent(event: ServletWebServerInitializedEvent) { | ||
LOG.info("server is started at port: ${event.webServer.port}") | ||
@Option(name = ["-h", "--host"]) | ||
private var host = "0.0.0.0" | ||
|
||
@Option(name = ["-p", "--port"]) | ||
private var port = 0 | ||
|
||
@Option(name = ["-d", "--debug"]) | ||
private var debug = false | ||
|
||
private lateinit var app: Javalin | ||
|
||
override fun run() { | ||
if (debug) { | ||
with(LoggerFactory.getLogger("nebulosa") as ch.qos.logback.classic.Logger) { | ||
level = Level.DEBUG | ||
} | ||
} | ||
|
||
// Run the server. | ||
app = Javalin.create { config -> | ||
config.showJavalinBanner = false | ||
// JACKSON | ||
config.jsonMapper(JavalinJackson(OBJECT_MAPPER)) | ||
// CORS | ||
config.bundledPlugins.enableCors { cors -> | ||
cors.addRule { | ||
it.anyHost() | ||
it.exposeHeader("X-Image-Info") | ||
} | ||
} | ||
}.start(host, port) | ||
|
||
koinApp.modules(appModule(app)) | ||
koinApp.modules(objectMapperModule(OBJECT_MAPPER)) | ||
koinApp.modules(servicesModule()) | ||
koinApp.modules(controllersModule()) | ||
startKoin(koinApp) | ||
|
||
LOG.info("server is started at port: {}", app.port()) | ||
} | ||
|
||
override fun close() { | ||
app.stop() | ||
} | ||
|
||
companion object { | ||
|
||
@JvmStatic private val LOG = loggerFor<Nebulosa>() | ||
|
||
@JvmStatic private val OBJECT_MAPPER = jsonMapper { | ||
addModule(JavaTimeModule()) | ||
addModule(PathModule()) | ||
addModule(DeviceModule()) | ||
disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) | ||
enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY) | ||
disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) | ||
} | ||
} | ||
} |
Oops, something went wrong.