lib9p

Go 9P library.
Log | Files | Refs | LICENSE

main.go (1101B)


      1 // Iofs exports the file system on the disk.
      2 // By default it listens to 127.0.0.1:5640.
      3 // You can change the address and port number with -a and -p flags.
      4 package main
      5 
      6 import (
      7 	"context"
      8 	"flag"
      9 	"fmt"
     10 	"log"
     11 	"net"
     12 	"os"
     13 
     14 	"git.mtkn.jp/lib9p"
     15 	"git.mtkn.jp/lib9p/iofs"
     16 )
     17 
     18 var dFlag = flag.Bool("D", false, "Prints chatty message to the stderr.")
     19 var aFlag = flag.String("a", "127.0.0.1", "Address the server listens to.")
     20 var pFlag = flag.Int("p", 5640, "Port number the server listens to.")
     21 
     22 func main() {
     23 	flag.Parse()
     24 
     25 	if flag.NArg() != 1 {
     26 		fmt.Fprintf(os.Stderr, "usage: %s [-D] [-a <addr>] [-p <port>] <root>\n", os.Args[0])
     27 		os.Exit(1)
     28 	}
     29 
     30 	listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", *aFlag, *pFlag))
     31 	if err != nil {
     32 		log.Fatalf("listen tcp: %v", err)
     33 	}
     34 	dir := os.DirFS(flag.Arg(0))
     35 	fsys := iofs.NewFS(dir)
     36 	s := lib9p.NewServer(fsys)
     37 	if *dFlag {
     38 		s.Chatty()
     39 	}
     40 	for {
     41 		conn, err := listener.Accept()
     42 		if err != nil {
     43 			log.Printf("accept connection: %v", err)
     44 			continue
     45 		}
     46 		go func() {
     47 			defer conn.Close()
     48 			s.Serve(context.TODO(), conn, conn)
     49 		}()
     50 	}
     51 }