jsonpipe

package module
v0.0.0-...-53a9afd Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2019 License: MIT Imports: 7 Imported by: 0

README

JSONpipe

Golang TCP/IP socket server for handling JSON requests with ids. Each JSON message will need to have a reqId property which will be included in the response message.

Features:
- Each connection has it's own go routine
- Each request on a connection has it's own go routine
- If an attempted socket flood is happening, server will automatically close the connection.
Install the package:
go get https://github.com/ARolek/jsonpipe
Use the package:
package main

import (
	"github.com/ARolek/jsonpipe"
)

func main (){
	jsonpipe.Handle("MyAction", myHandler)
	jsonpipe.ListenAndServe(":8080")
}

//	data var does not include the reqId or the action properties. only the request data property.
func myHandler(data *json.RawMessage) (map[string]interface{}, error){
	//	typicaly I unmarshal the JSON data into a struct here
}
Request requirements:
  • reqId (string): will be sent back to the client with the response
  • action (string): used to match up a request with a handler
  • data (map[string]interface{}): will be sent to the registered handler as *json.RawMessage. Perfect for unmarshalling into a struct

IMPORTANT: requests must be on a single line. JSONpipe scans for new lines (\n) to decide when a message ends. Do not send multiline JSON data.

Example

Request (indented for readability. must be on a single line)

{
	"reqId": "0",
	"action": "AnalyzeFile", 
	"data": {
		"src": "/path/to/some/file.png"
	}
}

Response (success):

{
	"reqId": "0",
	"success": true,
	"data": {
		//response data here
	}
}

Response(fail):

{
	"reqId": "0",
	"success": false,
	"error": "Invalid JSON format"
}

Documentation

Index

Constants

View Source
const (
	//	4 megabytes
	MaxScanTokenSize = 4 * 1024 * 1024
)

Variables

View Source
var Pipe = NewJSONpipe()

our pipe instance

Functions

func Handle

func Handle(action string, handler Handler)

func ListenAndServe

func ListenAndServe(port string)

pass in the port to bind to. ie. :8080

Types

type Action

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

type Handler

type Handler func(*json.RawMessage) (map[string]interface{}, error)

type JSONpipe

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

func NewJSONpipe

func NewJSONpipe() *JSONpipe

NewServeMux allocates and returns a new ServeMux.

type Request

type Request struct {
	Action string           `json:"action"`
	ReqId  string           `json:"reqId"`
	Data   *json.RawMessage `json:"data"`
}

type Response

type Response struct {
	ReqId   string                 `json:"reqId"`
	Success bool                   `json:"success"`
	Error   string                 `json:"error,omitempty"`
	Data    map[string]interface{} `json:"data,omitempty"`
}

type Server

type Server struct {
	Conn     net.Conn
	Reader   *bufio.Reader
	Encoder  *json.Encoder
	LineData []byte
}

func (*Server) Decode

func (server *Server) Decode()

func (*Server) HandleRequest

func (server *Server) HandleRequest(req *Request)

func (*Server) Read

func (server *Server) Read()

Jump to

Keyboard shortcuts

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