server

package
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2022 License: Apache-2.0 Imports: 23 Imported by: 0

README

Server

package service

import (
	"context"
	"fmt"
	"net/http"

	"github.com/go-sdk/lib/app"
	"github.com/go-sdk/lib/errx"
	"github.com/go-sdk/lib/seq"
	"github.com/go-sdk/lib/server"

	"github.com/go-sdk/pb/common"
	"github.com/go-sdk/pb/user"
)

func Start(ctx context.Context) error {
	s := server.New(ctx)
	s.RegisterService(&user.UserService_ServiceDesc, &UserService{})
	s.RegisterServiceHandler(user.RegisterUserServiceHandlerFromEndpoint)
	g := s.Group("/v1")
	{
		g.HandlePath(http.MethodPost, "/ping", func(ctx *server.Context) (interface{}, error) {
			fmt.Println("ping")
			return "pong", nil
		})
		gg := g.Group("/user")
		{
			gg.HandlePath(http.MethodGet, "/create", func(ctx *server.Context) (interface{}, error) {
				return "world", errx.New(http.StatusBadRequest, "no").WithMetadata(errx.Metadata{"x": "y"})
			})
		}
	}
	s.HandlePath(http.MethodGet, "/v2", func(ctx *server.Context) (interface{}, error) {
		fmt.Println("v2")
		return "v2", nil
	})
	return s.Start(":8999")
}

type UserService struct {
	user.UnimplementedUserServiceServer
}

func (s *UserService) Health(_ context.Context, _ *common.Empty) (*common.Struct, error) {
	return common.NewStruct(app.VersionInfoMap())
}

func (s *UserService) CreateUser(ctx context.Context, _ *user.User) (*common.Id, error) {
	return &common.Id{Id: seq.NewXID().String()}, nil
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TestClient

func TestClient(desc *grpc.ServiceDesc, impl interface{}) *grpc.ClientConn

Types

type Context

type Context struct {
	W http.ResponseWriter
	R *http.Request
	// contains filtered or unexported fields
}

func WithContext

func WithContext(w http.ResponseWriter, r *http.Request, p map[string]string) *Context

func (*Context) Deadline

func (c *Context) Deadline() (deadline time.Time, ok bool)

func (*Context) Done

func (c *Context) Done() <-chan struct{}

func (*Context) Err

func (c *Context) Err() error

func (*Context) Get

func (c *Context) Get(key string) (val.Value, bool)

func (*Context) Set

func (c *Context) Set(key string, value interface{})

func (*Context) Value

func (c *Context) Value(key interface{}) interface{}

type HandlerFunc

type HandlerFunc func(ctx *Context) (interface{}, error)

type MHandler

type MHandler interface {
	ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)
}

type MHandlerFunc

type MHandlerFunc func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)

func WrapHandlerFunc

func WrapHandlerFunc(h HandlerFunc) MHandlerFunc

func (MHandlerFunc) ServeHTTP

func (h MHandlerFunc) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)

type Server

type Server struct {
	// contains filtered or unexported fields
}

func New

func New(ctx context.Context) *Server

func (*Server) Group

func (s *Server) Group(path string, hs ...MHandlerFunc) *httpRouterGroup

func (*Server) HandlePath

func (s *Server) HandlePath(method, path string, h HandlerFunc)

func (*Server) RegisterService

func (s *Server) RegisterService(desc *grpc.ServiceDesc, impl interface{})

func (*Server) RegisterServiceHandler

func (s *Server) RegisterServiceHandler(h ServiceHandler, opts ...grpc.DialOption)

func (*Server) SetHandlersFunc

func (s *Server) SetHandlersFunc(hfs ...MHandlerFunc)

func (*Server) SetServeMuxOptions

func (s *Server) SetServeMuxOptions(opts ...runtime.ServeMuxOption)

func (*Server) SetServerOptions

func (s *Server) SetServerOptions(opts ...grpc.ServerOption)

func (*Server) Start

func (s *Server) Start(addr string) error

func (*Server) Stop

func (s *Server) Stop() error

type ServiceHandler

type ServiceHandler func(ctx context.Context, mux *runtime.ServeMux, endpoint string, opts []grpc.DialOption) error

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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