server

package
v0.0.15 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2022 License: Apache-2.0 Imports: 24 Imported by: 0

README

Server

package service

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

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

    "github.com/go-sdk/pb/admin"
    "github.com/go-sdk/pb/common"
    "github.com/go-sdk/pb/doc"
)

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

type UserService struct {
    admin.UnimplementedUserServiceServer
}

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

func (s *UserService) CreateUser(ctx context.Context, _ *admin.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 Dial added in v0.0.14

func Dial(addr string, opts ...grpc.DialOption) (*grpc.ClientConn, error)

func TestClient

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

Types

type HandlerFunc

type HandlerFunc func(ctx *cx.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