Example #1
0
func StartServer(args []string) {
	var err error
	const port_usage = "Port on which to serve HTTP API"

	flags := flag.NewFlagSet("start", flag.ExitOnError)
	flags.IntVar(&port, "port", 8080, port_usage)
	flags.IntVar(&port, "p", 8080, port_usage+" (shorthand)")
	flags.StringVar(&rpcSock, "sock", sock_default, sock_usage)
	flags.StringVar(&rpcSock, "s", sock_default, sock_usage+" (shorthand)")
	flags.Parse(args)

	adb, err = GetAndInitDB()
	if err != nil {
		panic(err)
	}

	rpcTornDown := make(chan int)
	go StartRPC(rpcSock, rpcTornDown, adb)

	http.HandleFunc("/", rootHandler)
	http.HandleFunc("/events", eventHandler)
	http.HandleFunc("/events/", eventHandler)

	//TODO add HTTPS, something like http://golang.org/pkg/net/http/#ListenAndServeTLS
	l, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
	if err != nil {
		panic(err)
	}
	defer l.Close()
	go http.Serve(l, nil)
	//TODO handle errors from http.Serve?

	asink.WaitOnExit()
	<-rpcTornDown
}
Example #2
0
func StartRPC(sock string, tornDown chan int) {
	defer func() { tornDown <- 0 }() //the main thread waits for this to ensure the socket is closed

	clientadmin := new(ClientAdmin)
	rpc.Register(clientadmin)

	rpc.HandleHTTP()
	l, err := net.Listen("unix", sock)
	if err != nil {
		panic(err)
	}
	defer l.Close()

	go http.Serve(l, nil)

	asink.WaitOnExit()
}
Example #3
0
func StartRPC(sock string, tornDown chan int, adb *AsinkDB) {
	defer func() { tornDown <- 0 }() //the main thread waits for this to ensure the socket is closed

	usermod := new(UserModifier)
	usermod.adb = adb
	rpc.Register(usermod)

	serverstop := new(ServerStopper)
	rpc.Register(serverstop)

	rpc.HandleHTTP()
	l, err := net.Listen("unix", sock)
	if err != nil {
		panic(err)
	}
	defer l.Close()

	go http.Serve(l, nil)

	asink.WaitOnExit()
}