notes-server/config/config.go

56 lines
1.0 KiB
Go
Executable File

package config
import (
"bytes"
"fmt"
"io/ioutil"
"local/args"
"log"
"os"
"strings"
)
var (
Root string
Port string
Head string
Foot string
OAuthServer string
)
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", "39909")
as.Append(args.STRING, "wrap", "file with http header/footer", "./wrapper.html")
as.Append(args.STRING, "oauth", "oauth URL", "")
if err := as.Parse(); err != nil {
panic(err)
}
wrap := as.Get("wrap").GetString()
log.Printf("reading %v (%T)", wrap, wrap)
b, err := ioutil.ReadFile(wrap)
if err != nil {
panic(err)
}
bs := bytes.Split(b, []byte("{{{}}}"))
if len(bs) != 2 {
panic(len(bs))
}
Root = as.Get("root").GetString()
Port = ":" + strings.TrimPrefix(as.Get("port").GetString(), ":")
Head = string(bs[0])
Foot = string(bs[1])
OAuthServer = as.Get("oauth").GetString()
}