go-hep.org/x/hep@v0.38.1/xrootd/cmd/xrd-srv/main.go (about)

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