1
1
package zenuo .gogo .web ;
2
2
3
+ import lombok .extern .slf4j .Slf4j ;
4
+ import org .apache .commons .io .IOUtils ;
3
5
import zenuo .gogo .core .config .ApplicationConfig ;
4
6
import zenuo .gogo .core .config .GogoConfig ;
5
7
import zenuo .gogo .model .IResponse ;
6
8
7
- import java .time .LocalTime ;
9
+ import java .io .InputStream ;
10
+ import java .nio .charset .StandardCharsets ;
11
+ import java .util .Objects ;
8
12
9
13
/**
10
14
* 主页构建器
11
15
*
12
16
* @author zenuo
13
17
* 2018-07-08 20:50:25
14
18
*/
19
+ @ Slf4j
15
20
public final class IndexPageBuilder implements IIndexPageBuilder {
16
21
17
22
private final GogoConfig gogoConfig = ApplicationConfig .gogoConfig ();
18
23
19
- /**
20
- * 样式表之前的HTML字符串
21
- */
22
- private static final String HTML_BEFORE_STYLE
23
- = "<!DOCTYPE html>" +
24
- "<html lang=\" en\" >" +
25
- "<head>" +
26
- "<meta charset=\" utf-8\" />" +
27
- "<title>勾勾</title>" +
28
- "<style>" ;
24
+ private final byte [] htmlBytes ;
29
25
30
- /**
31
- * 样式表之后的HTML字符串
32
- */
33
- private String htmlAfterStyle ;
34
-
35
- /**
36
- * 夜间模式的样式表
37
- */
38
- private static final String HTML_NIGHT_MODE_STYLE = "body{text-align:center;background-color:#000;color:#B6C5D4}" +
39
- "h1{font-size:50px;font-family:\" Times New Roman\" ,Times,serif}" +
40
- "footer{font-size:15px;font-family:Roboto,arial,sans-serif}" +
41
- ".main{margin:0 auto;width:50%;padding-bottom:50px}" ;
42
-
43
- /**
44
- * 日间模式的样式表
45
- */
46
- private static final String HTML_DAY_MODE_STYLE = "body{text-align:center;background-color:#F8F4E7;color:#552800}" +
47
- "h1{font-size:50px;font-family:\" Times New Roman\" ,Times,serif}" +
48
- "footer{font-size:15px;font-family:Roboto,arial,sans-serif}" +
49
- ".main{margin:0 auto;width:50%;padding-bottom:50px}" ;
50
-
51
- public IndexPageBuilder () {
52
- htmlAfterStyle = "</style>" +
53
- "</head>" +
54
- "<body>" +
55
- "<a href=\" https://github.com/zenuo/gogo\" ><img style=\" position: absolute; top: 0; right: 0; border: 0;\" width=\" 149\" height=\" 149\" src=\" https://github.blog/wp-content/uploads/2008/12/forkme_right_orange_ff7600.png?resize=149%2C149\" class=\" attachment-full size-full\" alt=\" Fork me on GitHub\" data-recalc-dims=\" 1\" ></a>" +
56
- "<div class=\" main\" >" +
57
- "<h1>勾勾</h1>" +
58
- "<form action=\" /search\" method=\" GET\" onsubmit=\" return q.value!=''\" >" +
59
- "<input name=\" q\" autocomplete=\" off\" autofocus=\" autofocus\" type=\" text\" >" +
60
- "<button value=\" Search\" type=\" submit\" >Go</button>" +
61
- "</form>" +
62
- "</div>" +
63
- "<footer>"
64
- + gogoConfig .getSlogan ()
65
- + "</footer>" +
66
- "</body>" +
67
- "</html>" ;
26
+ {
27
+ try (final InputStream resourceAsStream = Thread .currentThread ().getContextClassLoader ().getResourceAsStream ("web/index.html" )) {
28
+ final String indexHtml = IOUtils .toString (Objects .requireNonNull (resourceAsStream ), StandardCharsets .UTF_8 );
29
+ htmlBytes = indexHtml .replace ("__SLOGAN__" , gogoConfig .getSlogan ()).getBytes (StandardCharsets .UTF_8 );
30
+ } catch (Exception e ) {
31
+ log .error ("build index error" , e );
32
+ throw new RuntimeException (e );
33
+ }
68
34
}
69
35
70
36
/**
@@ -73,22 +39,8 @@ public IndexPageBuilder() {
73
39
* @return 主页的字符串
74
40
*/
75
41
@ Override
76
- public String build (IResponse response ) {
77
- //字符串构建器,初始化内容为样式表之前的HTML
78
- final StringBuilder sb = new StringBuilder (HTML_BEFORE_STYLE );
79
- //当前时间
80
- final LocalTime now = LocalTime .now ();
81
- if (now .isBefore (gogoConfig .getDayModeStartTime ()) ||
82
- now .isAfter (gogoConfig .getDayModeEndTime ())) {
83
- //若是夜间模式,拼接夜间模式样式表
84
- sb .append (HTML_NIGHT_MODE_STYLE );
85
- } else {
86
- //若是日间模式,拼接日间模式样式表
87
- sb .append (HTML_DAY_MODE_STYLE );
88
- }
89
- //拼接样式表之后的HTML
90
- sb .append (htmlAfterStyle );
42
+ public byte [] build (IResponse response ) {
91
43
//返回字符串
92
- return sb . toString () ;
44
+ return htmlBytes ;
93
45
}
94
46
}
0 commit comments