/
main.go
78 lines (59 loc) · 1.5 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright 2018 The go-hep Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Command xrd-srv serves data from a local filesystem over the XRootD protocol.
package main // import "go-hep.org/x/hep/xrootd/cmd/xrd-srv"
import (
"context"
"flag"
"fmt"
"log"
"net"
"os"
"os/signal"
"go-hep.org/x/hep/xrootd"
)
func init() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, `xrd-srv serves data from a local filesystem over the XRootD protocol.
Usage:
$> xrd-srv [OPTIONS] <base-dir>
Example:
$> xrd-srv /tmp
$> xrd-srv -addr=0.0.0.0:1094 /tmp
Options:
`)
flag.PrintDefaults()
}
}
func main() {
log.SetPrefix("xrd-srv: ")
log.SetFlags(0)
addr := flag.String("addr", "0.0.0.0:1094", "listen to the provided address")
flag.Parse()
if flag.NArg() != 1 {
flag.Usage()
log.Fatalf("missing base dir operand")
}
baseDir := flag.Arg(0)
listener, err := net.Listen("tcp", *addr)
if err != nil {
log.Fatalf("could not listen on %q: %v", *addr, err)
}
srv := xrootd.NewServer(xrootd.NewFSHandler(baseDir), func(err error) {
log.Printf("an error occured: %v", err)
})
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
go func() {
log.Printf("listening on %v...", listener.Addr())
if err = srv.Serve(listener); err != nil {
log.Fatalf("could not serve: %v", err)
}
}()
<-ch
err = srv.Shutdown(context.Background())
if err != nil {
log.Fatalf("could not shutdown: %v", err)
}
}