Coder Social home page Coder Social logo

hrpc's Introduction

hrpc

Build Status codecov Go Report Card GoDoc

Convert RPC style function into http.Handler

Support input types

  • context.Context
  • *http.Request
  • http.ResponseWriter
  • any

Support output types

  • any
  • error

Usage

import "github.com/acoshift/hrpc/v3"

Create new hrpc Manager

m := hrpc.Manager{
	Decoder: func(r *http.Request, dst any) error {
		return json.NewDecoder(r.Body).Decode(dst)
	},
	Encoder: func(w http.ResponseWriter, r *http.Request, res any) {
		w.Header().Set("Content-Type", "application/json; charset=utf-8")
		json.NewEncoder(w).Encode(res)
	},
	ErrorEncoder: func(w http.ResponseWriter, r *http.Request, err error) {
		res := &struct {
			Error string `json:"error"`
		}{err.Error()}
		w.Header().Set("Content-Type", "application/json; charset=utf-8")
		w.WriteHeader(http.StatusInternalServerError)
		json.NewEncoder(w).Encode(res)
	},
	Validate: true,
}

RPC style function

type UserRequest struct {
	ID int `json:"id"`
}

func (req *UserRequest) Valid() error {
	// Valid will be called when decode, if set validate to true
	if req.ID <= 0 {
		return fmt.Errorf("invalid id")
	}
	return nil
}

type UserResponse struct {
	ID int `json:"id"`
	Username string `json:"username"`
}

http.Handle("/user.get", m.Handler(func(ctx context.Context, req *UserRequest) (*UserResponse, error) {
	return &UserResponse{ID: 1, Username: "acoshift"}, nil
}))

// or use non-ptr struct
http.Handle("/user.get2", m.Handler(func(ctx context.Context, req UserRequest) (res UserResponse, err error) {
	res.ID = 1
	res.Username = "acoshift"
	return
}))

gRPC service client (generated from .proto)

m.Handler(func(ctx context.Context, in *UserRequest, opts ...grpc.CallOption) (*UserResponse, error) {
	return nil, errors.New("not implemented")
})

Mixed types

m.Handler(func(r *http.Request) error {
	buf := &bytes.Buffer{}
	_, err := io.Copy(buf, r.Body)
	if err != nil {
		return err
	}
	fmt.Printf("upload data: %s\n", buf.String())
	return nil
})

m.Handler(func(w http.ResponseWriter, r *http.Request) {})

m.Handler(func(ctx context.Context, r *http.Request, w http.ResponseWriter) {})

hrpc's People

Contributors

acoshift avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.