/
nginx.go
153 lines (137 loc) · 3.97 KB
/
nginx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
Copyright 2021 Hiroshi.tao
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package lib
import (
"bytes"
"errors"
"text/template"
"tcw.im/rtfd/pkg/util"
"tcw.im/gtc"
)
// 渲染nginx配置文件所需参数,前四项是必填项,后面选填,最后Now、SSL、SSLCFG会自动填充
type nginxOptions struct {
Name string // 文档项目名
Lang string // 文档默认语言
Domain string // 默认域名或自定义域名
DocsDir string // 文档项目所在的父目录
Single bool // 单一版本
SSLCrt string // ssl公钥
SSLKey string // ssl私钥
Now string // 当前时间
SSL bool // 是否开启ssl
SSLCFG string // ssl配置内容
}
func (opt *nginxOptions) render() (conf string, err error) {
if opt.Name == "" || opt.Lang == "" || opt.Domain == "" || opt.DocsDir == "" {
err = errors.New("found empty field")
return
}
if opt.Now == "" {
opt.Now = util.GetNow()
}
if gtc.IsFile(opt.SSLCrt) && gtc.IsFile(opt.SSLKey) {
ssltpl, e := template.New("ssl").Parse(nginxSSLTPL())
if e != nil {
err = e
return
}
var sslcfg bytes.Buffer
e = ssltpl.Execute(&sslcfg, struct{ SSLCrt, SSLKey string }{opt.SSLCrt, opt.SSLKey})
if e != nil {
err = e
return
}
opt.SSL = true
opt.SSLCFG = sslcfg.String()
} else {
opt.SSL = false
}
var tpl string
if opt.Single {
tpl = nginxSingleTPL()
} else {
tpl = nginxMultiTPL()
}
tmpl, err := template.New("nginx").Parse(tpl)
if err != nil {
return
}
var result bytes.Buffer
err = tmpl.Execute(&result, opt)
if err != nil {
return
}
return result.String(), nil
}
func nginxMultiTPL() string {
// 通用模板,需要参数:
return `#: Automatic generated by rtfd at {{ .Now }}
server {
listen 80;
{{- if .SSL }}
listen 443 ssl http2;
{{- end }}
server_name {{ .Domain }};
charset utf-8;
root {{ .DocsDir }}/{{ .Name }}/;
index index.html master.html;
set $home /{{ .Lang }}/latest;
error_page 403 =404 /404.html;
{{- if .SSL -}}
{{ .SSLCFG }}
{{- end }}
location / {
if (-e $document_root$home$document_uri) {
return 302 $home$document_uri$is_args$args;
}
}
}
`
}
func nginxSingleTPL() string {
// 单一版本的模板,参数要求同通用模板
return `#: Automatic generated by rtfd at {{ .Now }}
server {
listen 80;
{{- if .SSL }}
listen 443 ssl http2;
{{- end }}
server_name {{ .Domain }};
charset utf-8;
root {{ .DocsDir }}/{{ .Name }}/{{ .Lang }}/latest/;
index index.html master.html;
{{- if .SSL -}}
{{ .SSLCFG }}
{{- end }}
}
`
}
func nginxSSLTPL() string {
// SSL模板,需要传递证书、私钥三个参数
return `
if ($scheme = http) {
return 301 https://$server_name$request_uri;
}
ssl_certificate {{ .SSLCrt }};
ssl_certificate_key {{ .SSLKey }};
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 114.114.114.114 valid=300s;
resolver_timeout 5s;
ssl_session_tickets on;
ssl_session_timeout 10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
ssl_ciphers TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+ECDSA+AES128:EECDH+aRSA+AES128:RSA+AES128:EECDH+ECDSA+AES256:EECDH+aRSA+AES256:RSA+AES256:EECDH+ECDSA+3DES:EECDH+aRSA+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
add_header Strict-Transport-Security "max-age=31536000;preload";`
}