package eu.nerro.tus.server;

import java.util.Collections;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;

import eu.nerro.tus.server.exception.TusExceptionHandler;
import eu.nerro.tus.server.service.Service;
import eu.nerro.tus.server.service.ShutdownHookService;
import eu.nerro.tus.server.service.http.HttpUploadService;

import static org.slf4j.LoggerFactory.getLogger;

public class CliMain {

  private static final Logger LOG = getLogger(CliMain.class);

  public static void main(String[] args) {
    LOG.info("TUS Java server starting");
    final long startTime = System.nanoTime();

    // setup exception handler to handle all uncaught (runtime) exceptions
    Thread.setDefaultUncaughtExceptionHandler(new TusExceptionHandler());

    final Service httpUploadService = new HttpUploadService();
    ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
    scheduler.execute(httpUploadService);

    Runtime.getRuntime().addShutdownHook(new Thread(new ShutdownHookService(Collections.singletonList(httpUploadService))));

    final long endTime = System.nanoTime();
    LOG.info("TUS Java server started in {} ms", TimeUnit.MILLISECONDS.convert(endTime - startTime, TimeUnit.NANOSECONDS));
  }
}

@@ -207,24 +207,134 @@
 4j.Logger;%0A%0A
+import eu.nerro.tus.server.configuration.ConfigVar;%0Aimport eu.nerro.tus.server.configuration.ConfigVarHelper;%0A
 import eu.ne
@@ -375,24 +375,24 @@
 ionHandler;%0A
-
 import eu.ne
@@ -537,16 +537,141 @@
 Service;
+%0Aimport eu.nerro.tus.server.store.Datastore;%0Aimport eu.nerro.tus.server.store.Store;%0Aimport eu.nerro.tus.server.store.Stores;
 %0A%0Aimport
@@ -1079,24 +1079,365 @@
 andler());%0A%0A
+    // initialize upload store mechanism%0A    final Store store;%0A    final Datastore datastore = getConfiguredDatastore();%0A    switch (datastore) %7B%0A      case FILESTORE:%0A        store = Stores.newFileStore();%0A        break;%0A%0A      default:%0A        throw new IllegalArgumentException(%22No valid datastore defined: '%22 + datastore + %22'%22);%0A    %7D%0A%0A
     final Se
@@ -1774,24 +1774,24 @@
 nanoTime();%0A
-
     LOG.info
@@ -1905,11 +1905,968 @@
 S));%0A  %7D
+%0A%0A  /**%0A   * Reads the config %7B@link ConfigVar#DATASTORE%7D from environment variables.%0A   * %3Cp%3E%0A   * %3Ci%3ENote:%3C/i%3E if the variable is not defined or could not be parsed,%0A   * then %7B@link Datastore#FILESTORE%7D will be returned as default value.%0A   */%0A  private static Datastore getConfiguredDatastore() %7B%0A    final String datastoreConfigVar = ConfigVarHelper.getEnvValue(ConfigVar.DATASTORE);%0A    if (datastoreConfigVar == null %7C%7C datastoreConfigVar.isEmpty()) %7B%0A      LOG.warn(%22Configuration variable '%7B%7D' is not defined, use '%7B%7D' as default value%22, ConfigVar.DATASTORE, Datastore.FILESTORE);%0A      return Datastore.FILESTORE;%0A    %7D%0A%0A    Datastore datastore = Datastore.fromValue(datastoreConfigVar);%0A%0A    if (Datastore.UNKNOWN.equals(datastore)) %7B%0A      LOG.warn(%22Environment variable '%7B%7D=%7B%7D' could not be parsed, use '%7B%7D'%22, ConfigVar.DATASTORE, datastoreConfigVar, Datastore.FILESTORE);%0A      datastore = Datastore.FILESTORE;%0A    %7D%0A%0A    return datastore;%0A  %7D
 %0A%7D%0A

Initialize upload store mechanism when starting tus-java-server