Browse Source

Implementing command line argument parsing

main
Alan Bunbury 1 year ago
parent
commit
ec310d3219
  1. 3
      TODO.md
  2. 1
      build.gradle.kts
  3. 4
      src/main/kotlin/eu/bunburya/apogee/Config.kt
  4. 27
      src/main/kotlin/eu/bunburya/apogee/Main.kt
  5. 10
      src/main/resources/config.toml

3
TODO.md

@ -6,8 +6,7 @@
- [X] CGI
- [X] More testing
- [X] Figure out why only using ~10 threads
- [ ] SCGI
- [ ] More testing
- [X] SCGI
- [ ] FastCGI
- [X] Proper handling of bad requests
- [ ] Testing

1
build.gradle.kts

@ -18,6 +18,7 @@ repositories {
dependencies {
implementation(kotlin("stdlib"))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2")
implementation("org.jetbrains.kotlinx:kotlinx-cli:0.3.2")
// https://mvnrepository.com/artifact/io.netty/netty-all
implementation("io.netty", "netty-all", "4.1.63.Final")
// https://mvnrepository.com/artifact/com.moandjiezana.toml/toml4j

4
src/main/kotlin/eu/bunburya/apogee/Config.kt

@ -69,7 +69,7 @@ data class Config (
DOCUMENT_ROOT = "/var/gemini/",
ACCESS_FILE = null,
LOG_FILE = null,
LOG_LEVEL = "FINE",
LOG_LEVEL = "WARNING",
GEMINI_EXT = "gmi",
INDEX_FILE = "index.gmi",
DIR_SORT_METHOD = "NAME",
@ -112,6 +112,6 @@ data class Config (
}
val errorLogOutputStream: OutputStream get() =
if (LOG_FILE != null) FileOutputStream(File(LOG_FILE)) else System.out
if (LOG_FILE != null) FileOutputStream(File(LOG_FILE)) else System.err
}

27
src/main/kotlin/eu/bunburya/apogee/Main.kt

@ -1,16 +1,35 @@
package eu.bunburya.apogee
import kotlinx.coroutines.IO_PARALLELISM_PROPERTY_NAME
import kotlinx.cli.ArgParser
import kotlinx.cli.ArgType
import kotlinx.cli.required
import java.io.File
import java.io.FileNotFoundException
import java.lang.IllegalStateException
import java.lang.RuntimeException
/**
* Main function to run the server; currently, this initialises a test setup.
*/
fun main(args: Array<String>) {
val config = Config.fromTomlFile(File(args[0]))
//System.setProperty(IO_PARALLELISM_PROPERTY_NAME, "1024")
//println("MAX THREADS: ${System.getProperty(IO_PARALLELISM_PROPERTY_NAME)}")
val parser = ArgParser("apogee")
val configFile by parser.option(ArgType.String, fullName = "config", shortName = "c",
description = "Path to configuration file").required()
parser.parse(args)
val config = try {
Config.fromTomlFile(File(configFile))
} catch (e: Exception) {
when (e.cause) {
is FileNotFoundException -> System.err.println("Config file not found.")
is IllegalStateException -> System.err.println("Could not parse config file.")
else -> {
System.err.println("Encountered uncaught exception when initialising application.")
e.printStackTrace(System.err)
}
}
return
}
configureLogging(config)
val server = GeminiServer(config)
server.run()

10
src/main/resources/config.toml

@ -29,19 +29,19 @@ CGI_PATHS = [
#"/scgi-path-1" = "/var/run/apogee/scgi1.sock"
#"/scgi-path-2" = "/var/run/apogee/scgi2.sock"
## MIME type overrides (not implemented yet)
## MIME type overrides
#"atom.xml$" = "application/atom+xml"
#"rss.xml$" = "application/rss+xml"
## Redirects (not implemented yet)
## Redirects
#
#[TEMP_REDIRECTS]
#"/old/path/file.ext" = "/new/path/file.ext"
#"^/old/path/file.ext$" = "/new/path/file.ext"
#[PERM_REDIRECTS]
#"/old/path/file.ext" = "/new/path/file.ext"
#"^/old/path/file.ext$" = "/new/path/file.ext"
#
## Certificate zones (not fully implemented yet)
## Certificate zones
#
#[CLIENT_CERT_ZONES]
#"^/secure-zone-1/" = [

Loading…
Cancel
Save