syncthing/cmd/discosrv/main.go

113 lines
2.4 KiB
Go
Raw Normal View History

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