syncthing/cmd/discosrv/main.go

107 lines
2.3 KiB
Go
Raw Normal View History

2015-03-25 07:16:52 +00:00
// Copyright (C) 2014-2015 Jakob Borg and Contributors (see the CONTRIBUTORS file).
2014-06-01 20:50:14 +00:00
2013-12-23 02:35:05 +00:00
package main
import (
2015-09-13 09:44:33 +00:00
"crypto/tls"
2015-03-25 07:16:52 +00:00
"database/sql"
2014-02-20 16:40:15 +00:00
"flag"
2013-12-23 02:35:05 +00:00
"log"
2014-02-20 16:40:15 +00:00
"os"
"time"
2013-12-23 02:35:05 +00:00
"github.com/syncthing/syncthing/lib/protocol"
2015-03-25 07:16:52 +00:00
"github.com/thejerf/suture"
2013-12-23 02:35:05 +00:00
)
var (
2015-03-25 07:16:52 +00:00
lruSize = 10240
limitAvg = 5
limitBurst = 20
globalStats stats
2015-05-31 11:31:28 +00:00
statsFile string
2015-07-21 22:56:27 +00:00
backend = "ql"
dsn = getEnvDefault("DISCOSRV_DB_DSN", "memory://discosrv")
2015-09-13 09:44:33 +00:00
certFile = "cert.pem"
keyFile = "key.pem"
debug = false
2013-12-23 02:35:05 +00:00
)
func main() {
2015-03-25 07:16:52 +00:00
const (
cleanIntv = 1 * time.Hour
statsIntv = 5 * time.Minute
)
2014-02-20 16:40:15 +00:00
var listen string
2015-03-25 07:16:52 +00:00
log.SetOutput(os.Stdout)
log.SetFlags(0)
2014-02-20 16:40:15 +00:00
2015-09-13 09:44:33 +00:00
flag.StringVar(&listen, "listen", ":8443", "Listen address")
2014-06-27 20:39:03 +00:00
flag.IntVar(&lruSize, "limit-cache", lruSize, "Limiter cache entries")
flag.IntVar(&limitAvg, "limit-avg", limitAvg, "Allowed average package rate, per 10 s")
flag.IntVar(&limitBurst, "limit-burst", limitBurst, "Allowed burst size, packets")
2015-05-31 11:31:28 +00:00
flag.StringVar(&statsFile, "stats-file", statsFile, "File to write periodic operation stats to")
2015-07-21 22:56:27 +00:00
flag.StringVar(&backend, "db-backend", backend, "Database backend to use")
flag.StringVar(&dsn, "db-dsn", dsn, "Database DSN")
2015-09-13 09:44:33 +00:00
flag.StringVar(&certFile, "cert", certFile, "Certificate file")
flag.StringVar(&keyFile, "key", keyFile, "Key file")
flag.BoolVar(&debug, "debug", debug, "Debug")
2014-02-20 16:40:15 +00:00
flag.Parse()
2015-09-13 09:44:33 +00:00
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
log.Fatalln("Failed to load X509 key pair:", err)
}
2013-12-23 02:35:05 +00:00
devID := protocol.NewDeviceID(cert.Certificate[0])
log.Println("Server device ID is", devID)
2015-07-21 22:56:27 +00:00
db, err := sql.Open(backend, dsn)
2014-09-08 09:48:26 +00:00
if err != nil {
2015-07-21 22:56:27 +00:00
log.Fatalln("sql.Open:", err)
2014-09-08 09:48:26 +00:00
}
2015-07-21 22:56:27 +00:00
prep, err := setup(backend, db)
2014-09-08 09:48:26 +00:00
if err != nil {
2015-03-25 07:16:52 +00:00
log.Fatalln("Setup:", err)
2014-04-19 21:14:56 +00:00
}
2015-03-25 07:16:52 +00:00
main := suture.NewSimple("main")
2014-04-03 21:38:32 +00:00
2015-03-25 07:16:52 +00:00
main.Add(&querysrv{
2015-09-13 09:44:33 +00:00
addr: listen,
cert: cert,
2015-03-25 07:16:52 +00:00
db: db,
prep: prep,
})
2014-04-03 21:38:32 +00:00
2015-03-25 07:16:52 +00:00
main.Add(&cleansrv{
intv: cleanIntv,
db: db,
prep: prep,
})
2014-04-03 21:38:32 +00:00
2015-03-25 07:16:52 +00:00
main.Add(&statssrv{
intv: statsIntv,
2015-05-31 11:31:28 +00:00
file: statsFile,
db: db,
2015-03-25 07:16:52 +00:00
})
2013-12-23 02:35:05 +00:00
2015-03-25 07:16:52 +00:00
globalStats.Reset()
main.Serve()
2014-04-03 20:44:40 +00:00
}
2014-02-20 16:40:15 +00:00
2015-03-25 07:16:52 +00:00
func getEnvDefault(key, def string) string {
if val := os.Getenv(key); val != "" {
return val
}
2015-03-25 07:16:52 +00:00
return def
2014-04-03 20:44:40 +00:00
}
2015-03-25 07:16:52 +00:00
func next(intv time.Duration) time.Duration {
2014-04-19 21:14:56 +00:00
t0 := time.Now()
2015-03-25 07:16:52 +00:00
t1 := t0.Add(intv).Truncate(intv)
return t1.Sub(t0)
2014-09-08 09:48:26 +00:00
}