notes-server/config/config.go

189 lines
10 KiB
Go
Executable File

package config
import (
"bytes"
"fmt"
"io/ioutil"
"local/args"
"log"
"os"
"path/filepath"
"strings"
"time"
)
var (
Root string
Port string
Head string
Foot string
OAuthServer string
VersionInterval time.Duration
ReadOnly bool
MaxSizeMB int
)
func init() {
Refresh()
}
func Refresh() {
if strings.Contains(fmt.Sprint(os.Args), " -test") {
return
}
as := args.NewArgSet()
as.Append(args.STRING, "root", "root dir path", "./public")
as.Append(args.STRING, "port", "port to listen on", "49909")
as.Append(args.STRING, "wrap", "file with http header/footer", "")
as.Append(args.STRING, "oauth", "oauth URL", "")
as.Append(args.DURATION, "version", "duration to mark versions", "0s")
as.Append(args.BOOL, "ro", "read-only mode", false)
as.Append(args.INT, "max-v-size", "max size in mb for versioning", 2)
if err := as.Parse(); err != nil {
panic(err)
}
wrap := as.GetString("wrap")
var b []byte
if len(wrap) > 0 {
log.Printf("reading %v (%T)", wrap, wrap)
var err error
b, err = ioutil.ReadFile(wrap)
if err != nil {
panic(err)
}
} else {
b = []byte(defaultWrapper)
}
bs := bytes.Split(b, []byte("{{{}}}"))
if len(bs) != 2 {
panic(len(bs))
}
Root = strings.TrimSuffix(as.GetString("root"), "/")
Root, _ = filepath.Abs(Root)
Port = ":" + strings.TrimPrefix(as.GetString("port"), ":")
Head = string(bs[0])
Foot = string(bs[1])
OAuthServer = as.GetString("oauth")
VersionInterval = as.GetDuration("version")
ReadOnly = as.GetBool("ro")
MaxSizeMB = as.GetInt("max-v-size")
}
const defaultWrapper = `
<html>
<header>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- https://cdn.jsdelivr.net/gh/kognise/water.css@latest/dist/dark.min.css -->
<style>
body{font-family:system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen,Ubuntu,Cantarell,Fira Sans,Droid Sans,Helvetica Neue,sans-serif;line-height:1.4;max-width:800px;margin:20px auto;padding:0 10px;word-wrap:break-word;color:#dbdbdb;background:#421531;text-rendering:optimizeLegibility}button,input,textarea{transition:background-color .1s linear,border-color .1s linear,color .1s linear,box-shadow .1s linear,transform .1s ease}h1{font-size:2.2em;margin-top:0}h1,h2,h3,h4,h5,h6{margin-bottom:12px}h1,h2,h3,h4,h5,h6,strong{color:#fff}b,h1,h2,h3,h4,h5,h6,strong,th{font-weight:600}q:after,q:before{content:none}blockquote,q{border-left:4px solid #adb4a0;margin:1.5em 0;padding:.5em 1em;font-style:italic}blockquote>footer{font-style:normal;border:0}address,blockquote cite{font-style:normal}a[href^=mailto\:]:before{content:"📧 "}a[href^=tel\:]:before{content:"📞 "}a[href^=sms\:]:before{content:"💬 "}mark{background-color:#8fae83;border-radius:2px;padding:0 2px;color:#000}button,input[type=button],input[type=checkbox],input[type=radio],input[type=range],input[type=submit],select{cursor:pointer}input:not([type=checkbox]):not([type=radio]),select{display:block}button,input,select,textarea{color:#fff;background-color:#300330;font-family:inherit;font-size:inherit;margin-right:6px;margin-bottom:6px;padding:10px;border:none;border-radius:6px;outline:none}button,input,select,textarea{-webkit-appearance:none}textarea{margin-right:0;width:100%;box-sizing:border-box;resize:vertical}select{background:#300330 url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' height='63' width='117' fill='%23efefef'%3E%3Cpath d='M115 2c-1-2-4-2-5 0L59 53 7 2a4 4 0 0 0-5 5l54 54 2 2 3-2 54-54c2-1 2-4 0-5z'/%3E%3C/svg%3E") calc(100% - 12px) 50%/12px no-repeat;padding-right:35px}select::-ms-expand{display:none}select[multiple]{padding-right:10px;background-image:none;overflow-y:auto}button,input[type=button],input[type=submit]{padding-right:30px;padding-left:30px}button:hover,input[type=button]:hover,input[type=submit]:hover{background:#55354c}button:focus,input:focus,select:focus,textarea:focus{box-shadow:0 0 0 2px #adb4a0}input[type=checkbox],input[type=radio]{position:relative;width:14px;height:14px;display:inline-block;vertical-align:middle;margin:0 2px 0 0}input[type=radio]{border-radius:50%}input[type=checkbox]:checked,input[type=radio]:checked{background:#55354c}input[type=checkbox]:checked:before,input[type=radio]:checked:before{content:"•";display:block;position:absolute;left:50%;top:50%;transform:translateX(-50%) translateY(-50%)}input[type=checkbox]:checked:before{content:"✔";transform:translateY(-50%) translateY(.5px) translateX(-6px)}button:active,input[type=button]:active,input[type=checkbox]:active,input[type=radio]:active,input[type=range]:active,input[type=submit]:active{transform:translateY(2px)}button:disabled,input:disabled,select:disabled,textarea:disabled{cursor:not-allowed;opacity:.5}::-webkit-input-placeholder{color:#a9a9a9}:-ms-input-placeholder{color:#a9a9a9}::-ms-input-placeholder{color:#a9a9a9}::placeholder{color:#a9a9a9}fieldset{border:1px solid #adb4a0;border-radius:6px;margin:0 0 6px;padding:10px}legend{font-size:.9em;font-weight:600}input[type=range]{margin:10px 0;padding:10px 0;background:transparent}input[type=range]:focus{outline:none}input[type=range]::-webkit-slider-runnable-track{width:100%;height:9.5px;transition:.2s;background:#300330;border-radius:3px}input[type=range]::-webkit-slider-thumb{box-shadow:0 1px 1px #000,0 0 1px #0d0d0d;height:20px;width:20px;border-radius:50%;background:#dbdbdb;-webkit-appearance:none;margin-top:-7px}input[type=range]:focus::-webkit-slider-runnable-track{background:#300330}input[type=range]::-moz-range-track{width:100%;height:9.5px;transition:.2s;background:#300330;border-radius:3px}input[type=range]::-moz-range-thumb{box-shadow:1px 1px 1px #000,0 0 1px #0d0d0d;height:20px;width:20px;border-radius:50%;background:#dbdbdb}input[type=range]::-ms-track{width:100%;height:9.5px;background:transparent;border-color:transparent;border-width:16px 0;color:transparent}input[type=range]::-ms-fill-lower,input[type=range]::-ms-fill-upper{background:#300330;border:.2px solid #010101;border-radius:3px;box-shadow:1px 1px 1px #000,0 0 1px #0d0d0d}input[type=range]::-ms-thumb{box-shadow:1px 1px 1px #000,0 0 1px #0d0d0d;border:1px solid #000;height:20px;width:20px;border-radius:50%;background:#dbdbdb}input[type=range]:focus::-ms-fill-lower,input[type=range]:focus::-ms-fill-upper{background:#300330}a{text-decoration:none;color:#7fc090}a:hover{text-decoration:underline}code,samp,time{background:#300330;color:#dcff7f;padding:2.5px 5px;border-radius:6px;font-size:1em}pre>code{padding:10px;display:block;overflow-x:auto}var{color:#8a9e84;font-style:normal;font-family:monospace}kbd{background:#300330;border:1px solid #dbdbdb;border-radius:2px;color:#dbdbdb;padding:2px 4px}img{max-width:100%;height:auto}hr{border:none;border-top:1px solid #dbdbdb}table{border-collapse:collapse;margin-bottom:10px;width:100%}td,th{padding:6px;text-align:left}thead{border-bottom:1px solid #dbdbdb}tfoot{border-top:1px solid #dbdbdb}tbody tr:nth-child(2n){background-color:#3e0a2b}::-webkit-scrollbar{height:10px;width:10px}::-webkit-scrollbar-track{background:#300330;border-radius:6px}::-webkit-scrollbar-thumb{background:#55354c;border-radius:6px}::-webkit-scrollbar-thumb:hover{background:#6e4562}::-moz-selection{background-color:#200220}::selection{background-color:#200220}details{display:flex;flex-direction:column;align-items:flex-start;background-color:#3e0a2b;padding:10px 10px 0;margin:1em 0;border-radius:6px;overflow:hidden}details[open]{padding:10px}details>:last-child{margin-bottom:0}details[open] summary{margin-bottom:10px}summary{display:list-item;background-color:#300330;padding:10px;margin:-10px -10px 0}details>:not(summary){margin-top:0}summary::-webkit-details-marker{color:#dbdbdb}footer{border-top:1px solid #300330;padding-top:10px;font-size:.8em;color:#787878}
/*# sourceMappingURL=dark-legacy.standalone.min.css.map */
button, input, select, textarea {
background-color: #300330;
}
</style>
<style>
thead, tr:nth-child(2n) {
background-color:#3e0a2b;
background-color:#300330 !important;
}
nav {
display: block;
background: #300330;
padding: .5pt;
border-radius: 6px;
}
nav li li li li {
display: none;
}
img {
max-height: 400px;
}
body {
font-size: 125%;
background: #3b242b;
margin-top: 0;
margin-bottom: 0;
height: calc(100vh - 1em);
overflow: auto;
display: flex;
flex-direction: column;
}
body > .form {
flex-grow: 2;
}
body > .form > form {
height: 100%;
display: flex;
flex-direction: column;
}
body > .form > form > textarea {
flex-grow: 2;
}
rendered > h2:nth-child(2) {
margin: 0;
position: fixed;
padding: .25em;
width: 100%;
background: #3b242b;
top: 0;
}
h1, h2, h3, h4, h5, h6, h7, h8 {
position: relative;
}
.comment {
display: none;
margin: 0;
padding: 0;
flex-direction: column;
justify-content: center;
position: absolute;
right: 100%;
padding-right: 1em;
width: 10em;
top: 0;
text-align: right;
font-size: 12pt;
}
body > h2:first-of-type {
margin: 0;
margin-bottom: .5em;
padding: .3em 0 .3em 0;
background-color: #3b242b;
z-index: 999;
position: -webkit-sticky;
position: sticky;
top: 0;
}
h1:hover > .comment,
h2:hover > .comment,
h3:hover > .comment,
h4:hover > .comment,
h5:hover > .comment,
h6:hover > .comment,
h7:hover > .comment,
h8:hover > .comment,
.comment:focus,
.comment:focus-within {
display: inline-flex;
}
.attachments > details > form {
display: flex;
flex-direction: row;
width: 100%;
}
.attachments > details > form > input:first-of-type {
flex-grow: 2;
}
</style>
</header>
<body>
{{{}}}
</body>
<footer>
</footer>
</html>
`