server

package
v0.0.0-...-c9fc7fd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2021 License: MIT Imports: 8 Imported by: 2

Documentation

Overview

Package server implements a redis server

Example
package main

import (
	"log"
	"net"

	"github.com/Akagi201/redface/resp"
	"github.com/Akagi201/redface/server"
)

var pongSS = resp.NewRespSimple("PONG")

func pingHandler(conn net.Conn, args []string) (interface{}, error) {
	return pongSS, nil
}

func main() {
	defer func() {
		if err := recover(); err != nil {
			log.Printf("panic: %v\n", err)
		}
	}()

	srv, err := server.New(6389)
	if err != nil {
		panic(err)
	}

	srv.Handle("ping", pingHandler)

	if err := srv.ListenAndServe(); err != nil {
		panic(err)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HandlerFunc

type HandlerFunc func(conn net.Conn, args []string) (interface{}, error)

The HandlerFunc type is an adapter to allow the use of ordinary functions as HTTP handlers. If f is a function with the appropriate signature, HandlerFunc(f) is a Handler that calls f.

type Opts

type Opts struct {
	// "unix" for unix socket, "tcp" for tcp
	Proto string
	// The address to listen at
	Host string
	// The port to listen at
	Port int
}

Opts are Options which can be passed in to NewWithOpts. If any are set to their zero value the default value will be used instead

type Server

type Server struct {
	// "unix" for unix socket, "tcp" for tcp
	Proto string
	// TCP address to listen on, ":6379" if empty
	Addr string
	// contains filtered or unexported fields
}

Server A Server defines parameters for running an Redis API Server. The zero value for Server is a valid configuration.

func New

func New(port int) (*Server, error)

New creates a new tcp redis server with the port.

func NewWithOpts

func NewWithOpts(o Opts) (*Server, error)

NewWithOpts is the same as New, but with more fine-tuned configuration options. See Opts for more available options.

func (*Server) Dispatch

func (srv *Server) Dispatch(conn net.Conn, cmd string, args []string)

Dispatch takes in a client whose command has already been read off the socket, a list of arguments from that command (not including the command name itself), and handles that command

func (*Server) Handle

func (srv *Server) Handle(cmd string, handlerFunc HandlerFunc)

Handle registers the handler for the given cmd. If a handler already exists for pattern, Handle panics.

func (*Server) ListenAndServe

func (srv *Server) ListenAndServe() error

ListenAndServe listens on the TCP or Unix socket network address srv.Addr and then calls Serve to handle requests on incoming connections. If srv.Addr is blank, ":6379" is used. ListenAndServe always returns a non-nil error.

func (*Server) Serve

func (srv *Server) Serve(l net.Listener) error

Serve accepts incoming connections on the Listener l, creating a new service goroutine for each. The service goroutines read requests and then call srv.Handler to reply to them.

Serve always returns a non-nil error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL