Skip to content

Commit 263d5ff

Browse files
committed
build test
1 parent 4d93658 commit 263d5ff

File tree

11 files changed

+621
-7
lines changed

11 files changed

+621
-7
lines changed

.gitignore

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
# Virtual machine crash logs
2-
hs_err_pid*
2+
/hs_err_pid*
33

44
# Gradle build files
5-
.gradle/
5+
/.gradle/
66

77
# IDEA files
8-
.idea/
8+
/.idea/
99

1010
# Vertx
11-
.vertx
11+
/.vertx
1212

1313
# Build
14-
build/
14+
/build/
1515

1616
# Log
17-
log/
17+
/log/
1818

1919
# Config
20-
config/
20+
/config/

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Support websocket proxy (Not yet)
44
* Support multiple targets
55

6+
[![Build Status](https://travis-ci.org/code13k/zeroproxy.svg?branch=master)](https://travis-ci.org/code13k/zeroproxy)
7+
68

79
# Supported protocol
810
* http / https
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package org.code13k.zeroproxy.config;
2+
3+
import org.code13k.zeroproxy.lib.Util;
4+
import org.code13k.zeroproxy.model.config.app.PortInfo;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.yaml.snakeyaml.Yaml;
8+
9+
import java.util.LinkedHashMap;
10+
11+
public class AppConfig extends BasicConfig {
12+
// Logger
13+
private static final Logger mLogger = LoggerFactory.getLogger(AppConfig.class);
14+
15+
// Data
16+
private PortInfo mPortInfo = new PortInfo();
17+
18+
/**
19+
* Singleton
20+
*/
21+
private static class SingletonHolder {
22+
static final AppConfig INSTANCE = new AppConfig();
23+
}
24+
25+
public static AppConfig getInstance() {
26+
return AppConfig.SingletonHolder.INSTANCE;
27+
}
28+
29+
/**
30+
* Constructor
31+
*/
32+
private AppConfig() {
33+
mLogger.trace("AppConfig()");
34+
}
35+
36+
@Override
37+
protected String getDefaultConfigFilename() {
38+
return "default_app_config.yml";
39+
}
40+
41+
@Override
42+
protected String getConfigFilename() {
43+
return "app_config.yaml";
44+
}
45+
46+
@Override
47+
protected boolean loadConfig(final String content, final String filePath) {
48+
try {
49+
Yaml yaml = new Yaml();
50+
LinkedHashMap yamlObject = yaml.load(content);
51+
mLogger.trace("yamlObject class name = " + yamlObject.getClass().getName());
52+
mLogger.trace("yamlObject = " + yamlObject);
53+
54+
// PortInfo
55+
LinkedHashMap portObject = (LinkedHashMap) yamlObject.get("port");
56+
mLogger.trace("portObject class name = " + portObject.getClass().getName());
57+
mLogger.trace("portObject = " + portObject);
58+
Integer portMainHttp = (Integer) portObject.get("main_http");
59+
if (Util.isValidPortNumber(portMainHttp) == false) {
60+
mLogger.error("Invalid main_http port : " + portMainHttp);
61+
return false;
62+
}
63+
Integer portMainWs = (Integer) portObject.get("main_ws");
64+
if (Util.isValidPortNumber(portMainWs) == false) {
65+
mLogger.error("Invalid main_ws port : " + portMainWs);
66+
return false;
67+
}
68+
Integer portApiHttp = (Integer) portObject.get("api_http");
69+
if (Util.isValidPortNumber(portApiHttp) == false) {
70+
mLogger.error("Invalid api_http port : " + portApiHttp);
71+
return false;
72+
}
73+
mPortInfo.setMainHttp(portMainHttp);
74+
mPortInfo.setMainWs(portMainWs);
75+
mPortInfo.setApiHttp(portApiHttp);
76+
} catch (Exception e) {
77+
mLogger.error("Failed to load config file", e);
78+
return false;
79+
}
80+
return true;
81+
}
82+
83+
@Override
84+
public void logging() {
85+
// Begin
86+
mLogger.info("------------------------------------------------------------------------");
87+
mLogger.info("Application Configuration");
88+
mLogger.info("------------------------------------------------------------------------");
89+
90+
// Config File Path
91+
mLogger.info("Config file path = " + getConfigFilename());
92+
93+
// PortInfo
94+
mLogger.info("main_http of PortInfo = " + mPortInfo.getMainHttp());
95+
mLogger.info("main_ws of PortInfo = " + mPortInfo.getMainHttp());
96+
mLogger.info("api_http of PortInfo = " + mPortInfo.getApiHttp());
97+
98+
// End
99+
mLogger.info("------------------------------------------------------------------------");
100+
}
101+
102+
/**
103+
* Get port
104+
*/
105+
public PortInfo getPort() {
106+
return mPortInfo;
107+
}
108+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package org.code13k.zeroproxy.config;
2+
3+
import com.google.common.io.Resources;
4+
import org.apache.commons.io.FileUtils;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
import java.io.BufferedWriter;
9+
import java.io.File;
10+
import java.io.FileWriter;
11+
import java.nio.charset.Charset;
12+
import java.nio.file.Files;
13+
import java.nio.file.Paths;
14+
15+
public abstract class BasicConfig {
16+
// Logger
17+
private static final Logger mLogger = LoggerFactory.getLogger(BasicConfig.class);
18+
19+
// Const
20+
public static final String DEFAULT_CONFIG_BASE_PATH = "config/";
21+
public static final String CONFIG_BASE_PATH = "config/";
22+
23+
// Data
24+
private String mConfigAbsolutePath = null;
25+
26+
/**
27+
* Initialize
28+
*/
29+
public void init() throws Exception {
30+
// Config Absolute File Path
31+
String userDir = System.getProperty("user.dir");
32+
mConfigAbsolutePath = userDir + "/" + CONFIG_BASE_PATH + getConfigFilename();
33+
34+
// Init config
35+
if (false == initConfig()) {
36+
String errorMessage = "Failed to initialize " + mConfigAbsolutePath;
37+
mLogger.error(errorMessage);
38+
throw new Exception(errorMessage);
39+
}
40+
41+
// Load config
42+
String content = new String(Files.readAllBytes(Paths.get(mConfigAbsolutePath)));
43+
if(false == loadConfig(content, mConfigAbsolutePath)){
44+
String errorMessage = "Failed to load config " + mConfigAbsolutePath;
45+
mLogger.error(errorMessage);
46+
throw new Exception(errorMessage);
47+
}
48+
49+
// End
50+
logging();
51+
}
52+
53+
/**
54+
* Initialize config file
55+
*/
56+
private boolean initConfig() {
57+
// Init Config File
58+
File file = new File(mConfigAbsolutePath);
59+
if (file.isFile() == false) {
60+
mLogger.debug(mConfigAbsolutePath + " is not existed.");
61+
try {
62+
FileUtils.forceMkdirParent(file);
63+
String resourcePath = DEFAULT_CONFIG_BASE_PATH + getDefaultConfigFilename();
64+
String configContent = Resources.toString(Resources.getResource(resourcePath), Charset.defaultCharset());
65+
BufferedWriter out = new BufferedWriter(new FileWriter(mConfigAbsolutePath));
66+
out.write(configContent);
67+
out.close();
68+
} catch (Exception e) {
69+
mLogger.error("Failed to initialize " + mConfigAbsolutePath, e);
70+
return false;
71+
}
72+
}
73+
74+
// End
75+
return true;
76+
}
77+
78+
/**
79+
* Get configuration file name
80+
*/
81+
protected abstract String getConfigFilename();
82+
83+
/**
84+
* Get default configuration file name in resource folder
85+
*/
86+
protected abstract String getDefaultConfigFilename();
87+
88+
/**
89+
* Load configuration file
90+
*/
91+
protected abstract boolean loadConfig(final String content, final String filePath);
92+
93+
/**
94+
* Print log
95+
*/
96+
protected abstract void logging();
97+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.code13k.zeroproxy.config;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
public class LogConfig extends BasicConfig {
7+
// Logger
8+
private static final Logger mLogger = LoggerFactory.getLogger(LogConfig.class);
9+
10+
/**
11+
* Singleton
12+
*/
13+
private static class SingletonHolder {
14+
static final LogConfig INSTANCE = new LogConfig();
15+
}
16+
17+
public static LogConfig getInstance() {
18+
return LogConfig.SingletonHolder.INSTANCE;
19+
}
20+
21+
/**
22+
* Constructor
23+
*/
24+
private LogConfig() {
25+
mLogger.trace("LogConfig()");
26+
}
27+
28+
@Override
29+
protected String getDefaultConfigFilename() {
30+
return "default_logback.xml";
31+
}
32+
33+
@Override
34+
protected String getConfigFilename() {
35+
return "logback.xml";
36+
}
37+
38+
@Override
39+
protected boolean loadConfig(final String content, final String filePath) {
40+
try {
41+
// Nothing
42+
} catch (Exception e) {
43+
mLogger.error("Failed to load config file", e);
44+
}
45+
return true;
46+
}
47+
48+
@Override
49+
public void logging() {
50+
// Begin
51+
mLogger.info("------------------------------------------------------------------------");
52+
mLogger.info("Log Configuration");
53+
mLogger.info("------------------------------------------------------------------------");
54+
55+
// Config File Path
56+
mLogger.info("Config file path = " + getConfigFilename());
57+
58+
// End
59+
mLogger.info("------------------------------------------------------------------------");
60+
}
61+
}

0 commit comments

Comments
 (0)