Search in sources :

Example 1 with Config

use of com.blade.kit.base.Config in project blade by biezhi.

the class PropKit method loadInputStream.

private static Config loadInputStream(InputStream is, String location) {
    if (is == null) {
        throw new IllegalStateException("InputStream not found: " + location);
    }
    try {
        Properties config = new Properties();
        config.load(is);
        return new Config(config);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    } finally {
        if (null != is) {
            try {
                is.close();
            } catch (Exception e) {
            }
        }
    }
}
Also used : Config(com.blade.kit.base.Config) IOException(java.io.IOException) Properties(java.util.Properties) IOException(java.io.IOException)

Example 2 with Config

use of com.blade.kit.base.Config in project blade by biezhi.

the class EmbedJettyServer method startup.

@Override
public void startup(int port, String contextPath, String webRoot) throws EmbedServerException {
    this.port = port;
    Config config = Blade.$().config();
    int minThreads = config.getInt("server.jetty.min-threads", 8);
    int maxThreads = config.getInt("server.jetty.max-threads", 200);
    String poolName = config.get("server.jetty.pool-name", "blade-pool");
    // Setup Threadpool
    QueuedThreadPool threadPool = new QueuedThreadPool();
    threadPool.setMinThreads(minThreads);
    threadPool.setMaxThreads(maxThreads);
    threadPool.setName(poolName);
    this.server = new org.eclipse.jetty.server.Server(threadPool);
    this.webAppContext = new WebAppContext();
    this.webAppContext.setContextPath(contextPath);
    this.webAppContext.setResourceBase("");
    int securePort = config.getInt("server.jetty.http.secure-port", 9443);
    int outputBufferSize = config.getInt("server.jetty.http.output-buffersize", 32 * 1024);
    int requestHeaderSize = config.getInt("server.jetty.http.request-headersize", 8 * 1024);
    int responseHeaderSize = config.getInt("server.jetty.http.response-headersize", 8 * 1024);
    // HTTP Configuration
    HttpConfiguration http_config = new HttpConfiguration();
    http_config.setSecurePort(securePort);
    http_config.setOutputBufferSize(outputBufferSize);
    http_config.setRequestHeaderSize(requestHeaderSize);
    http_config.setResponseHeaderSize(responseHeaderSize);
    long idleTimeout = config.getLong("server.jetty.http.idle-timeout", 30000L);
    String host = config.get("server.host", "0.0.0.0");
    ServerConnector serverConnector = new ServerConnector(server, new HttpConnectionFactory(http_config));
    serverConnector.setHost(host);
    serverConnector.setPort(this.port);
    serverConnector.setIdleTimeout(idleTimeout);
    server.setConnectors(new Connector[] { serverConnector });
    boolean isAsync = config.getBoolean("server.async", false);
    Class<? extends Servlet> servlet = isAsync ? AsyncDispatcherServlet.class : DispatcherServlet.class;
    ServletHolder servletHolder = new ServletHolder(servlet);
    servletHolder.setAsyncSupported(isAsync);
    servletHolder.setInitOrder(1);
    webAppContext.addEventListener(new BladeInitListener());
    Set<String> statics = Blade.$().bConfig().getStatics();
    defaultHolder = new ServletHolder(DefaultServlet.class);
    defaultHolder.setInitOrder(0);
    if (StringKit.isNotBlank(classPath)) {
        LOGGER.info("add classpath : {}", classPath);
        defaultHolder.setInitParameter("resourceBase", classPath);
    }
    statics.forEach(s -> {
        if (s.indexOf(".") != -1) {
            webAppContext.addServlet(defaultHolder, s);
        } else {
            s = s.endsWith("/") ? s + '*' : s + "/*";
            webAppContext.addServlet(defaultHolder, s);
        }
    });
    webAppContext.addServlet(defaultHolder, "/favicon.ico");
    webAppContext.addServlet(servletHolder, "/");
    try {
        this.loadServlets(webAppContext);
        this.loadFilters(webAppContext);
        HandlerList handlerList = new HandlerList();
        handlerList.setHandlers(new Handler[] { webAppContext, new DefaultHandler() });
        server.setHandler(handlerList);
        server.setStopAtShutdown(true);
        server.start();
        LOGGER.info("Blade Server Listen on {}:{}", host, this.port);
        server.join();
    } catch (Exception e) {
        throw new EmbedServerException(e);
    }
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) org.eclipse.jetty.server(org.eclipse.jetty.server) EmbedServerException(com.blade.exception.EmbedServerException) Config(com.blade.kit.base.Config) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) BladeInitListener(com.blade.mvc.context.BladeInitListener) EmbedServerException(com.blade.exception.EmbedServerException) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) DefaultServlet(org.eclipse.jetty.servlet.DefaultServlet)

Example 3 with Config

use of com.blade.kit.base.Config in project tale by otale.

the class IndexController method saveSetting.

/**
     * 保存系统设置
     */
@Route(value = "setting", method = HttpMethod.POST)
@JSON
public RestResponse saveSetting(@QueryParam String site_theme, Request request) {
    try {
        Map<String, String> querys = request.querys();
        optionsService.saveOptions(querys);
        Config config = new Config();
        config.addAll(optionsService.getOptions());
        TaleConst.OPTIONS = config;
        logService.save(LogActions.SYS_SETTING, JSONKit.toJSONString(querys), request.address(), this.getUid());
        return RestResponse.ok();
    } catch (Exception e) {
        String msg = "保存设置失败";
        if (e instanceof TipException) {
            msg = e.getMessage();
        } else {
            LOGGER.error(msg, e);
        }
        return RestResponse.fail(msg);
    }
}
Also used : Config(com.blade.kit.base.Config) TipException(com.tale.exception.TipException) TipException(com.tale.exception.TipException) JSON(com.blade.mvc.annotation.JSON) Route(com.blade.mvc.annotation.Route)

Example 4 with Config

use of com.blade.kit.base.Config in project tale by otale.

the class InstallController method doInstall.

@Route(value = "/", method = HttpMethod.POST)
@JSON
public RestResponse doInstall(@QueryParam String site_title, @QueryParam String site_url, @QueryParam String admin_user, @QueryParam String admin_email, @QueryParam String admin_pwd) {
    if (FileKit.exist(AttachController.CLASSPATH + "install.lock") && TaleConst.OPTIONS.getInt("allow_install", 0) != 1) {
        return RestResponse.fail("请勿重复安装");
    }
    try {
        if (StringKit.isBlank(site_title) || StringKit.isBlank(site_url) || StringKit.isBlank(admin_user) || StringKit.isBlank(admin_pwd)) {
            return RestResponse.fail("请确认网站信息输入完整");
        }
        if (admin_pwd.length() < 6 || admin_pwd.length() > 14) {
            return RestResponse.fail("请输入6-14位密码");
        }
        if (StringKit.isNotBlank(admin_email) && !TaleUtils.isEmail(admin_email)) {
            return RestResponse.fail("邮箱格式不正确");
        }
        Users users = new Users();
        users.setUsername(admin_user);
        users.setPassword(admin_pwd);
        users.setEmail(admin_email);
        siteService.initSite(users);
        if (site_url.endsWith("/")) {
            site_url = site_url.substring(0, site_url.length() - 1);
        }
        if (!site_url.startsWith("http")) {
            site_url = "http://".concat(site_url);
        }
        optionsService.saveOption("site_title", site_title);
        optionsService.saveOption("site_url", site_url);
        Config config = new Config();
        config.addAll(optionsService.getOptions());
        TaleConst.OPTIONS = config;
    } catch (Exception e) {
        String msg = "安装失败";
        if (e instanceof TipException) {
            msg = e.getMessage();
        } else {
            LOGGER.error(msg, e);
        }
        return RestResponse.fail(msg);
    }
    return RestResponse.ok();
}
Also used : Config(com.blade.kit.base.Config) Users(com.tale.model.Users) TipException(com.tale.exception.TipException) TipException(com.tale.exception.TipException) JSON(com.blade.mvc.annotation.JSON) Route(com.blade.mvc.annotation.Route)

Example 5 with Config

use of com.blade.kit.base.Config in project tale by otale.

the class ThemeController method saveSetting.

/**
     * 保存主题配置项
     * @param request
     * @return
     */
@Route(value = "setting", method = HttpMethod.POST)
@JSON
public RestResponse saveSetting(Request request) {
    try {
        Map<String, String> querys = request.querys();
        optionsService.saveOptions(querys);
        Config config = new Config();
        config.addAll(optionsService.getOptions());
        TaleConst.OPTIONS = config;
        logService.save(LogActions.THEME_SETTING, JSONKit.toJSONString(querys), request.address(), this.getUid());
        return RestResponse.ok();
    } catch (Exception e) {
        String msg = "主题设置失败";
        if (e instanceof TipException) {
            msg = e.getMessage();
        } else {
            LOGGER.error(msg, e);
        }
        return RestResponse.fail(msg);
    }
}
Also used : Config(com.blade.kit.base.Config) TipException(com.tale.exception.TipException) TipException(com.tale.exception.TipException) JSON(com.blade.mvc.annotation.JSON) Route(com.blade.mvc.annotation.Route)

Aggregations

Config (com.blade.kit.base.Config)5 JSON (com.blade.mvc.annotation.JSON)3 Route (com.blade.mvc.annotation.Route)3 TipException (com.tale.exception.TipException)3 EmbedServerException (com.blade.exception.EmbedServerException)1 BladeInitListener (com.blade.mvc.context.BladeInitListener)1 Users (com.tale.model.Users)1 IOException (java.io.IOException)1 Properties (java.util.Properties)1 org.eclipse.jetty.server (org.eclipse.jetty.server)1 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)1 HandlerList (org.eclipse.jetty.server.handler.HandlerList)1 DefaultServlet (org.eclipse.jetty.servlet.DefaultServlet)1 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)1 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)1 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)1