Example #1
0
func main() {
	srv := redeo.NewServer(nil)
	srv.HandleFunc("ping", func(out *redeo.Responder, _ *redeo.Request) error {
		out.WriteInlineString("PONG")
		return nil
	})
	srv.HandleFunc("info", func(out *redeo.Responder, _ *redeo.Request) error {
		out.WriteString(srv.Info().String())
		return nil
	})
	srv.HandleFunc("client", func(out *redeo.Responder, req *redeo.Request) error {
		if len(req.Args) != 1 {
			return req.WrongNumberOfArgs()
		}

		switch req.Args[0] {
		case "list":
			out.WriteString(srv.Info().ClientsString())
		default:
			return req.UnknownCommand()
		}
		return nil
	})

	log.Printf("Listening on tcp://%s", srv.Addr())
	log.Fatal(srv.ListenAndServe())
}
Example #2
0
// Start starts a server. It listens on a random port on localhost. See also
// Addr().
func (m *Miniredis) Start() error {
	m.Lock()
	defer m.Unlock()

	l, err := listen("127.0.0.1:0")
	if err != nil {
		return err
	}
	m.listen = l
	m.listenAddr = l.Addr().String()
	m.srv = redeo.NewServer(&redeo.Config{Addr: m.listenAddr})

	commandsConnection(m, m.srv)
	commandsGeneric(m, m.srv)
	commandsString(m, m.srv)
	commandsHash(m, m.srv)
	commandsList(m, m.srv)
	commandsSet(m, m.srv)
	commandsSortedSet(m, m.srv)
	commandsTransaction(m, m.srv)

	go func() {
		m.srv.Serve(m.listen)
		m.closed <- struct{}{}
	}()
	return nil
}
Example #3
0
// Start starts a server. It listens on a random port on localhost. See also Addr().
func (m *Miniredis) Start() error {
	m.Lock()
	defer m.Unlock()
	l, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		if l, err = net.Listen("tcp6", "[::1]:0"); err != nil {
			return fmt.Errorf("failed to listen on a port: %v", err)
		}
	}
	m.listen = l
	srv := redeo.NewServer(&redeo.Config{Addr: "localhost:0"})

	m.info = srv.Info()

	commandsConnection(m, srv)
	commandsGeneric(m, srv)
	commandsString(m, srv)
	commandsHash(m, srv)
	commandsList(m, srv)
	commandsSet(m, srv)
	commandsSortedSet(m, srv)
	commandsTransaction(m, srv)

	go func() {
		e := make(chan error)
		go srv.Serve(e, m.listen)
		<-e
		m.closed <- struct{}{}
	}()
	return nil
}
Example #4
0
func main() {
	srv := redeo.NewServer(nil)

	srv.HandleFunc("ping", pingCmd)
	srv.HandleFunc("file", fileCmd)

	log.Printf("Listening on tcp://%s", srv.Addr())
	log.Fatal(srv.ListenAndServe())
}