113 lines
3.5 KiB
Go
113 lines
3.5 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/sha256"
|
|
"embed"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
const legalPageStyle = `
|
|
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
|
|
<style>
|
|
:root { color-scheme: light; }
|
|
* { box-sizing: border-box; }
|
|
body {
|
|
margin: 0;
|
|
background: #f5f6f8;
|
|
color: #24262b;
|
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
|
|
"Hiragino Sans GB", "Microsoft YaHei", sans-serif;
|
|
letter-spacing: 0;
|
|
}
|
|
main {
|
|
width: min(100%, 800px);
|
|
min-height: 100vh;
|
|
margin: 0 auto;
|
|
padding: 40px 48px 64px;
|
|
background: #fff;
|
|
}
|
|
h1 {
|
|
margin: 0 0 32px;
|
|
color: #17191d;
|
|
font-size: 28px;
|
|
font-weight: 650;
|
|
line-height: 1.35;
|
|
text-align: center;
|
|
}
|
|
.legal-content p.p1, .legal-content p.p2 {
|
|
margin: 0 0 12px;
|
|
color: inherit;
|
|
font: inherit;
|
|
font-size: 16px;
|
|
line-height: 1.85;
|
|
overflow-wrap: anywhere;
|
|
-webkit-text-stroke: 0;
|
|
}
|
|
@media (max-width: 600px) {
|
|
main { padding: 28px 20px 48px; }
|
|
h1 { margin-bottom: 24px; font-size: 22px; }
|
|
.legal-content p.p1, .legal-content p.p2 { font-size: 15px; line-height: 1.8; }
|
|
}
|
|
</style>
|
|
`
|
|
|
|
//go:embed legal/*.html
|
|
var legalDocumentFiles embed.FS
|
|
|
|
type legalDocument struct {
|
|
body []byte
|
|
etag string
|
|
}
|
|
|
|
var legalDocuments = map[string]legalDocument{
|
|
"/legal/user-terms": loadLegalDocument("legal/user-terms.html", "笔墨讯使软件服务协议"),
|
|
"/legal/privacy-policy": loadLegalDocument("legal/privacy-policy.html", "笔墨讯使隐私政策"),
|
|
}
|
|
|
|
func loadLegalDocument(name, title string) legalDocument {
|
|
body, err := legalDocumentFiles.ReadFile(name)
|
|
if err != nil {
|
|
panic("load legal document: " + err.Error())
|
|
}
|
|
body = bytes.Replace(body,
|
|
[]byte("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">"),
|
|
[]byte("<!doctype html>"), 1)
|
|
body = bytes.Replace(body, []byte("<title></title>"), []byte("<title>"+title+"</title>"), 1)
|
|
body = bytes.Replace(body, []byte("</head>"), []byte(legalPageStyle+"</head>"), 1)
|
|
body = bytes.Replace(body, []byte("<body>"),
|
|
[]byte("<body><main><h1>"+title+"</h1><div class=\"legal-content\">"), 1)
|
|
body = bytes.Replace(body, []byte("</body>"), []byte("</div></main></body>"), 1)
|
|
digest := sha256.Sum256(body)
|
|
return legalDocument{body: body, etag: fmt.Sprintf("\"%x\"", digest[:16])}
|
|
}
|
|
|
|
func serveLegalDocument(wrt http.ResponseWriter, req *http.Request) {
|
|
if req.Method != http.MethodGet && req.Method != http.MethodHead {
|
|
wrt.Header().Set("Allow", "GET, HEAD")
|
|
http.Error(wrt, "method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
document, ok := legalDocuments[req.URL.Path]
|
|
if !ok {
|
|
http.NotFound(wrt, req)
|
|
return
|
|
}
|
|
wrt.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
wrt.Header().Set("Cache-Control", "public, max-age=300, must-revalidate")
|
|
wrt.Header().Set("Content-Security-Policy", "default-src 'none'; style-src 'unsafe-inline'; img-src data:; base-uri 'none'; form-action 'none'")
|
|
wrt.Header().Set("Referrer-Policy", "no-referrer")
|
|
wrt.Header().Set("X-Content-Type-Options", "nosniff")
|
|
wrt.Header().Set("ETag", document.etag)
|
|
if req.Header.Get("If-None-Match") == document.etag {
|
|
wrt.WriteHeader(http.StatusNotModified)
|
|
return
|
|
}
|
|
if req.Method == http.MethodHead {
|
|
wrt.Header().Set("Content-Length", fmt.Sprintf("%d", len(document.body)))
|
|
return
|
|
}
|
|
_, _ = wrt.Write(document.body)
|
|
}
|