|
79 | 79 | import java.util.function.Consumer; |
80 | 80 | import java.util.function.Function; |
81 | 81 | import java.util.function.Predicate; |
| 82 | +import java.util.function.Supplier; |
82 | 83 | import java.util.stream.Collectors; |
83 | 84 |
|
84 | 85 | import javax.inject.Singleton; |
|
141 | 142 | import com.typesafe.config.ConfigValue; |
142 | 143 | import com.typesafe.config.ConfigValueFactory; |
143 | 144 |
|
| 145 | +import javaslang.control.Try; |
| 146 | + |
144 | 147 | /** |
145 | 148 | * <h1>Getting Started:</h1> |
146 | 149 | * <p> |
@@ -477,15 +480,8 @@ public EnvDep(final Predicate<String> predicate, final Consumer<Config> callback |
477 | 480 | // set pid as system property |
478 | 481 | String pid = System.getProperty("pid", JvmInfo.pid() + ""); |
479 | 482 | System.setProperty("pid", pid); |
480 | | - |
481 | | - // Avoid warning message from logback when multiples files are present |
482 | | - String logback = System.getProperty("logback.configurationFile", "logback.xml"); |
483 | | - System.setProperty("logback.configurationFile", logback); |
484 | 483 | } |
485 | 484 |
|
486 | | - /** The logging system. */ |
487 | | - private final Logger log = LoggerFactory.getLogger(getClass()); |
488 | | - |
489 | 485 | /** |
490 | 486 | * Keep track of routes. |
491 | 487 | */ |
@@ -3098,6 +3094,67 @@ public WebSocket.Definition ws(final String path, |
3098 | 3094 | return ws; |
3099 | 3095 | } |
3100 | 3096 |
|
| 3097 | + /** |
| 3098 | + * <h1>Bootstrap</h1> |
| 3099 | + * <p> |
| 3100 | + * The bootstrap process is defined as follows: |
| 3101 | + * </p> |
| 3102 | + * <h2>1. Configuration files (first-listed are higher priority)</h2> |
| 3103 | + * <ol> |
| 3104 | + * <li>System properties</li> |
| 3105 | + * <li>Application properties: {@code application.conf} or custom, see {@link #use(Config)}</li> |
| 3106 | + * <li>{@link Jooby.Module Modules} properties</li> |
| 3107 | + * </ol> |
| 3108 | + * |
| 3109 | + * <h2>2. Dependency Injection and {@link Jooby.Module modules}</h2> |
| 3110 | + * <ol> |
| 3111 | + * <li>An {@link Injector Guice Injector} is created.</li> |
| 3112 | + * <li>It calls to {@link Jooby.Module#configure(Env, Config, Binder)} for each module.</li> |
| 3113 | + * <li>At this point Guice is ready and all the services has been binded.</li> |
| 3114 | + * <li>A web server is started</li> |
| 3115 | + * </ol> |
| 3116 | + * |
| 3117 | + * @param app App creator. |
| 3118 | + * @param args App arguments. |
| 3119 | + * @throws Exception If something fails to start. |
| 3120 | + */ |
| 3121 | + public static void run(final Supplier<? extends Jooby> app, final String... args) |
| 3122 | + throws Exception { |
| 3123 | + Config conf = ConfigFactory.systemProperties() |
| 3124 | + .withFallback(args(args)); |
| 3125 | + System.setProperty("logback.configurationFile", logback(conf)); |
| 3126 | + app.get().start(args); |
| 3127 | + } |
| 3128 | + |
| 3129 | + /** |
| 3130 | + * <h1>Bootstrap</h1> |
| 3131 | + * <p> |
| 3132 | + * The bootstrap process is defined as follows: |
| 3133 | + * </p> |
| 3134 | + * <h2>1. Configuration files (first-listed are higher priority)</h2> |
| 3135 | + * <ol> |
| 3136 | + * <li>System properties</li> |
| 3137 | + * <li>Application properties: {@code application.conf} or custom, see {@link #use(Config)}</li> |
| 3138 | + * <li>{@link Jooby.Module Modules} properties</li> |
| 3139 | + * </ol> |
| 3140 | + * |
| 3141 | + * <h2>2. Dependency Injection and {@link Jooby.Module modules}</h2> |
| 3142 | + * <ol> |
| 3143 | + * <li>An {@link Injector Guice Injector} is created.</li> |
| 3144 | + * <li>It calls to {@link Jooby.Module#configure(Env, Config, Binder)} for each module.</li> |
| 3145 | + * <li>At this point Guice is ready and all the services has been binded.</li> |
| 3146 | + * <li>A web server is started</li> |
| 3147 | + * </ol> |
| 3148 | + * |
| 3149 | + * @param app App creator. |
| 3150 | + * @param args App arguments. |
| 3151 | + * @throws Exception If something fails to start. |
| 3152 | + */ |
| 3153 | + public static void run(final Class<? extends Jooby> app, final String... args) |
| 3154 | + throws Exception { |
| 3155 | + run(() -> Try.of(() -> app.newInstance()).get(), args); |
| 3156 | + } |
| 3157 | + |
3101 | 3158 | /** |
3102 | 3159 | * <h1>Bootstrap</h1> |
3103 | 3160 | * <p> |
@@ -3214,6 +3271,7 @@ public void start(final String[] args, final Consumer<List<Route.Definition>> ro |
3214 | 3271 |
|
3215 | 3272 | Config config = injector.getInstance(Config.class); |
3216 | 3273 |
|
| 3274 | + Logger log = LoggerFactory.getLogger(getClass()); |
3217 | 3275 | log.debug("config tree:\n{}", configTree(config.origin().description())); |
3218 | 3276 |
|
3219 | 3277 | // Start server |
@@ -3249,7 +3307,7 @@ public static void main(final String[] jsargs) throws Exception { |
3249 | 3307 | System.arraycopy(jsargs, 1, args, 0, args.length); |
3250 | 3308 | } |
3251 | 3309 | String filename = jsargs.length > 0 ? jsargs[0] : "app.js"; |
3252 | | - new JsJooby().run(new File(filename)).start(args); |
| 3310 | + run(new JsJooby().run(new File(filename)), args); |
3253 | 3311 | } |
3254 | 3312 |
|
3255 | 3313 | private String configTree(final String description) { |
@@ -3588,6 +3646,7 @@ public void stop() { |
3588 | 3646 | if (injector != null) { |
3589 | 3647 | stopManaged(injector, onStop); |
3590 | 3648 |
|
| 3649 | + Logger log = LoggerFactory.getLogger(getClass()); |
3591 | 3650 | try { |
3592 | 3651 | Server server = injector.getInstance(Server.class); |
3593 | 3652 | String serverName = server.getClass().getSimpleName().replace("Server", "").toLowerCase(); |
@@ -3721,17 +3780,18 @@ private Config modeConfig(final Config source, final String env) { |
3721 | 3780 | * @param fname A file name. |
3722 | 3781 | * @return A config for the file name. |
3723 | 3782 | */ |
3724 | | - private Config fileConfig(final String fname) { |
3725 | | - File froot = new File(fname); |
3726 | | - File fconfig = new File("conf", fname); |
3727 | | - Config config = ConfigFactory.empty(); |
| 3783 | + static Config fileConfig(final String fname) { |
| 3784 | + File dir = new File(System.getProperty("user.dir")); |
| 3785 | + File froot = new File(dir, fname); |
3728 | 3786 | if (froot.exists()) { |
3729 | | - config = config.withFallback(ConfigFactory.parseFile(froot)); |
3730 | | - } |
3731 | | - if (fconfig.exists()) { |
3732 | | - config = config.withFallback(ConfigFactory.parseFile(fconfig)); |
| 3787 | + return ConfigFactory.parseFile(froot); |
| 3788 | + } else { |
| 3789 | + File fconfig = new File(new File(dir, "conf"), fname); |
| 3790 | + if (fconfig.exists()) { |
| 3791 | + return ConfigFactory.parseFile(fconfig); |
| 3792 | + } |
3733 | 3793 | } |
3734 | | - return config; |
| 3794 | + return ConfigFactory.empty(); |
3735 | 3795 | } |
3736 | 3796 |
|
3737 | 3797 | /** |
@@ -3857,4 +3917,30 @@ private static Predicate<String> envpredicate(final String candidate) { |
3857 | 3917 | return env -> env.equalsIgnoreCase(candidate) || candidate.equals("*"); |
3858 | 3918 | } |
3859 | 3919 |
|
| 3920 | + static String logback(final Config conf) { |
| 3921 | + // Avoid warning message from logback when multiples files are present |
| 3922 | + String logback; |
| 3923 | + if (conf.hasPath("logback.configurationFile")) { |
| 3924 | + logback = conf.getString("logback.configurationFile"); |
| 3925 | + } else { |
| 3926 | + ImmutableList.Builder<File> files = ImmutableList.builder(); |
| 3927 | + File userdir = new File(System.getProperty("user.dir")); |
| 3928 | + File confdir = new File(userdir, "conf"); |
| 3929 | + if (conf.hasPath("application.env")) { |
| 3930 | + String env = conf.getString("application.env"); |
| 3931 | + files.add(new File(userdir, "logback." + env + ".xml")); |
| 3932 | + files.add(new File(confdir, "logback." + env + ".xml")); |
| 3933 | + } |
| 3934 | + files.add(new File(userdir, "logback.xml")); |
| 3935 | + files.add(new File(confdir, "logback.xml")); |
| 3936 | + logback = files.build() |
| 3937 | + .stream() |
| 3938 | + .filter(f -> f.exists()) |
| 3939 | + .map(f -> f.getAbsolutePath()) |
| 3940 | + .findFirst() |
| 3941 | + .orElse("logback.xml"); |
| 3942 | + } |
| 3943 | + return logback; |
| 3944 | + } |
| 3945 | + |
3860 | 3946 | } |
0 commit comments