0

What tools are out there for managing configuration files (per environment) for Kotlin/Javalin applications? Alternatives to Konf (https://github.com/uchuhimo/konf)?

Bizgu Daniela
  • 61
  • 1
  • 7
  • I usually build a "ConfigTool" class that loads configuration files via a single load method. Generally I make the class final so that I can statically access its load method from other classes. Then I just make more of these ConfigTool classes for whatever kind of configuration data I need to pull from files. How many configuration files do you really need to work with that you need an entire extra tool/dependency for it? – TheFunk Apr 01 '20 at 13:48
  • Interesting, ok. Thanks for sharing that. Usually around 3-5 files, dev/local, int, prod, acceptance tests int, acceptance tests prod. As well as environment variables on the machine set through CFT or other. – Bizgu Daniela Apr 03 '20 at 02:55

1 Answers1

0

Here's an example "ConfigTool". You could expand this to create multiple objects and parse multiple files. I can't take full credit for it, I originally adapted it from someone else:

package app.utils;

import app.pojos.DBProps;
import app.pojos.WebAppVars;

import java.io.FileInputStream;
import java.util.Properties;

public final class ConfigTool {


  //Create a connection parameter array for storing... connection parameters
  private static final DBProps sysParams = new DBProps();

  public static void load() {
    //Read configuration properties for where the DB is and how to connect, close program if catch exception
    Properties cfg_props = new Properties();
    try (FileInputStream configfile = new FileInputStream(WebAppVars.configLocation)) {
        cfg_props.load(configfile);
        sysParams.setDbAddr(cfg_props.getProperty("database.dbAddr"));
        sysParams.setDbUser(cfg_props.getProperty("database.dbUser"));
        sysParams.setDbPass(cfg_props.getProperty("database.dbPass"));
        sysParams.setDbType(cfg_props.getProperty("database.dbType"));
        sysParams.setDbName(cfg_props.getProperty("database.dbName"));
        sysParams.setDbAuth(cfg_props.getProperty("database.dbAuth"));

        //TODO Add more if statements to config tool to support larger array of SQL Servers
        if(sysParams.getDbType().equalsIgnoreCase("MSSQL")){
            sysParams.setDbConnStr("jdbc:sqlserver://" + sysParams.getDbAddr() + ";databaseName=" + sysParams.getDbName());
            sysParams.setDbDriver("com.microsoft.jdbc.sqlserver.SQLServerDriver");
        }
    }
    catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
  }

  public static DBProps getSysParams(){
    return sysParams;
  }
}
TheFunk
  • 981
  • 11
  • 39