GithubHelp home page GithubHelp logo

go-hpc's Introduction

GoDoc Build Status

go-hpc

HPC is a Gorilla RPC v2 Codec that allows you to perform RPC-like requests using the pathname for the service and method, leaving the bodies for requests and responses.

This differs from JSON-RPC which uses a JSON body envelope. This package also hides the "Go-isms" by transforming snake-case to camel-case, for example "/files/list_all" would invoke Files.ListAll().

Example

Service:

package hpc_test

import (
  "log"
  "net/http"
  "strings"

  "github.com/gorilla/rpc/v2"
  "github.com/tj/go-hpc"
)

type Users struct {
  users []string
}

type ListInput struct {
  Prefix string `json:"prefix"`
}

type ListOutput struct {
  Names []string `json:"names"`
}

func (u *Users) List(r *http.Request, in *ListInput, out *ListOutput) error {
  out.Names = []string{}
  for _, name := range u.users {
    if strings.HasPrefix(name, in.Prefix) {
      out.Names = append(out.Names, name)
    }
  }
  return nil
}

func Example() {
  users := []string{"Tobi", "Loki", "Jane"}

  r := rpc.NewServer()
  r.RegisterCodec(hpc.NewCodec(), "application/json")
  r.RegisterService(&Users{users}, "")

  http.Handle("/", r)
  log.Fatalln(http.ListenAndServe(":3000", nil))
}

Request:

$ curl -d '{ "prefix": "T" }' -H "Content-Type: application/json" http://localhost:3000/users/list
{
  "names": ["Tobi"]
}

License

MIT

go-hpc's People

Contributors

tj 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.