Skip to content

Commit

Permalink
🦄 init sqlite version
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokaton committed Mar 3, 2017
1 parent 6f0fb5d commit 443a1fa
Show file tree
Hide file tree
Showing 30 changed files with 239 additions and 1,396 deletions.
10 changes: 1 addition & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.junicorn</groupId>
<artifactId>tale</artifactId>
<version>1.1.0</version>
<version>1.2.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -38,20 +38,12 @@
<artifactId>sqlite-jdbc</artifactId>
<version>${sqlite.version}</version>
</dependency>

<dependency>
<groupId>com.bladejava</groupId>
<artifactId>blade-jdbc</artifactId>
<version>${blade-jdbc.version}</version>
</dependency>

<!--连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>

<!-- 模板引擎 -->
<dependency>
<groupId>com.bladejava</groupId>
Expand Down
64 changes: 5 additions & 59 deletions src/main/java/com/tale/controller/InstallController.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.tale.controller;


import com.blade.Blade;
import com.blade.ioc.annotation.Inject;
import com.blade.kit.FileKit;
import com.blade.kit.StringKit;
Expand All @@ -14,11 +13,8 @@
import com.blade.mvc.http.Request;
import com.blade.mvc.view.RestResponse;
import com.tale.controller.admin.AttachController;
import com.tale.dto.JdbcConf;
import com.tale.exception.TipException;
import com.tale.ext.Commons;
import com.tale.init.TaleConst;
import com.tale.init.TaleMySqlJdbc;
import com.tale.model.Users;
import com.tale.service.OptionsService;
import com.tale.service.SiteService;
Expand All @@ -37,8 +33,6 @@ public class InstallController extends BaseController {
@Inject
private OptionsService optionsService;

private boolean dbConn = false;

/**
* 安装页
*
Expand All @@ -50,9 +44,9 @@ public String index(Request request) {
boolean existInstall = FileKit.exist(webRoot + "install.lock");
int isInstall = TaleConst.OPTIONS.getInt("site_is_install", 0);
// 已经安装过
if(existInstall || isInstall == 1){
if (existInstall || isInstall == 1) {
// 如果设置允许重新安装
if("1".equals(TaleConst.OPTIONS.get("allow_install", "0"))){
if ("1".equals(TaleConst.OPTIONS.get("allow_install", "0"))) {
request.attribute("is_install", false);
} else {
request.attribute("is_install", true);
Expand All @@ -67,9 +61,7 @@ public String index(Request request) {
@JSON
public RestResponse doInstall(@QueryParam String site_title, @QueryParam String site_url,
@QueryParam String admin_user, @QueryParam String admin_email,
@QueryParam String admin_pwd,
@QueryParam String db_host, @QueryParam String db_name,
@QueryParam String db_user, @QueryParam String db_pass) {
@QueryParam String admin_pwd) {

try {
if (StringKit.isBlank(site_title) ||
Expand All @@ -79,13 +71,6 @@ public RestResponse doInstall(@QueryParam String site_title, @QueryParam String
return RestResponse.fail("请确认网站信息输入完整");
}

if (StringKit.isBlank(db_host) ||
StringKit.isBlank(db_name) ||
StringKit.isBlank(db_user) ||
StringKit.isBlank(db_pass)) {
return RestResponse.fail("请确认数据库信息输入完整");
}

if (admin_pwd.length() < 6 || admin_pwd.length() > 14) {
return RestResponse.fail("请输入6-14位密码");
}
Expand All @@ -94,20 +79,12 @@ public RestResponse doInstall(@QueryParam String site_title, @QueryParam String
return RestResponse.fail("邮箱格式不正确");
}

if(!dbConn){
return RestResponse.fail("请检查数据库连接");
}

TaleMySqlJdbc.injection(Blade.$().ioc());

Users users = new Users();
users.setUsername(admin_user);
users.setPassword(admin_pwd);
users.setEmail(admin_email);

JdbcConf jdbcConf = new JdbcConf(db_host, db_name, db_user, db_pass);

siteService.initSite(users, jdbcConf);
siteService.initSite(users);

if (site_url.endsWith("/")) {
site_url = site_url.substring(0, site_url.length() - 1);
Expand All @@ -122,9 +99,7 @@ public RestResponse doInstall(@QueryParam String site_title, @QueryParam String
Config config = new Config();
config.addAll(optionsService.getOptions());
TaleConst.OPTIONS = config;
TaleConst.INSTALL = true;
Commons.setSiteService(siteService);

TaleConst.INSTALL = Boolean.TRUE;
} catch (Exception e) {
String msg = "安装失败";
if (e instanceof TipException) {
Expand All @@ -137,33 +112,4 @@ public RestResponse doInstall(@QueryParam String site_title, @QueryParam String
return RestResponse.ok();
}

/**
* 测试数据库连接
*
* @return
*/
@Route(value = "conn_test", method = HttpMethod.POST)
@JSON
public RestResponse conn_test(@QueryParam String db_host, @QueryParam String db_name,
@QueryParam String db_user, @QueryParam String db_pass) {

String url = "jdbc:mysql://" + db_host + "/" + db_name + "?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull";
TaleMySqlJdbc.put("url", url);
TaleMySqlJdbc.put("username", db_user);
TaleMySqlJdbc.put("password", db_pass);
try {
TaleMySqlJdbc.testConn();
dbConn = true;
} catch (Exception e) {
String msg = "数据库连接失败, 请检查数据库配置";
if (e instanceof TipException) {
msg = e.getMessage();
} else {
LOGGER.error(msg, e);
}
return RestResponse.fail(msg);
}
return RestResponse.ok();
}

}
9 changes: 5 additions & 4 deletions src/main/java/com/tale/controller/admin/IndexController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.tale.controller.admin;

import com.blade.ioc.annotation.Inject;
import com.blade.kit.DateKit;
import com.blade.kit.FileKit;
import com.blade.kit.StringKit;
import com.blade.kit.Tools;
import com.blade.kit.base.Config;
Expand All @@ -19,6 +21,7 @@
import com.tale.dto.Types;
import com.tale.exception.TipException;
import com.tale.ext.Commons;
import com.tale.init.SqliteJdbc;
import com.tale.init.TaleConst;
import com.tale.model.Comments;
import com.tale.model.Contents;
Expand All @@ -32,10 +35,7 @@
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.*;

/**
* 后台控制器
Expand Down Expand Up @@ -200,6 +200,7 @@ public RestResponse backup(@QueryParam String bk_type, @QueryParam String bk_pat
if (StringKit.isBlank(bk_type)) {
return RestResponse.fail("请确认信息输入完整");
}

try {
BackResponse backResponse = siteService.backup(bk_type, bk_path, "yyyyMMddHHmm");
logService.save(LogActions.SYS_BACKUP, null, request.address(), this.getUid());
Expand Down
59 changes: 59 additions & 0 deletions src/main/java/com/tale/init/SqliteJdbc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.tale.init;

import com.blade.kit.IOKit;
import com.tale.exception.TipException;

import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
* 数据库操作
* Created by biezhi on 2017/2/23.
*/
public final class SqliteJdbc {

private SqliteJdbc() {
}

private static final String DB_NAME = "tale.db";
public static String DB_PATH = SqliteJdbc.class.getClassLoader().getResource("").getPath() + DB_NAME;

public static final String DB_SRC = "jdbc:sqlite://" + DB_PATH;

static {
try {
Class.forName("org.sqlite.JDBC");
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 测试连接并导入数据库
*/
public static void importSql() {
try {
Connection con = DriverManager.getConnection(DB_SRC);
Statement statement = con.createStatement();
ResultSet rs = statement.executeQuery("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='t_options'");
int count = rs.getInt(1);
if (count == 0) {
String cp = SqliteJdbc.class.getClassLoader().getResource("").getPath();
InputStreamReader isr = new InputStreamReader(new FileInputStream(cp + "schema.sql"), "UTF-8");
String sql = IOKit.toString(isr);
statement.executeUpdate(sql);
}
rs.close();
statement.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
throw new TipException("数据库连接失败, 请检查数据库配置");
}
}

}
12 changes: 1 addition & 11 deletions src/main/java/com/tale/init/TaleConst.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class TaleConst {
public static String AES_SALT = "0123456789abcdef";
public static String LOGIN_SESSION_KEY = "login_user";
public static Config OPTIONS = new Config();
public static boolean INSTALL = false;
public static Boolean INSTALL = false;
public static Config BCONF = null;

/**
Expand Down Expand Up @@ -53,14 +53,4 @@ public class TaleConst {
*/
public static final Set<String> BLOCK_IPS = new HashSet<>(16);

/**
* MYSQL驱动名
*/
public static final String MYSQL_DRIVER_NAME = "com.mysql.jdbc.Driver";

/**
* sqlite驱动名
*/
public static final String SQLITE_DRIVER_NAME = "org.sqlite.JDBC";

}
Loading

0 comments on commit 443a1fa

Please sign in to comment.