web

package
v0.0.67 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2024 License: Apache-2.0, NCSA Imports: 11 Imported by: 0

Documentation

Overview

Package web defines utility functions for exposing services over HTTP.

Index

Constants

This section is empty.

Variables

View Source
var JSONMarshaler = Marshaler{
	protojson.MarshalOptions{
		UseProtoNames: true,
	},
}

JSONMarshaler is the marshaler used to encode all JSON web requests.

Functions

func Arg

func Arg(r *http.Request, name string) string

Arg returns the first query value for the named parameter or "" if it was not set.

func ArgOrNil

func ArgOrNil(r *http.Request, name string) *string

ArgOrNil returns a pointer to first query value for the named parameter or nil if it was not set.

func Call

func Call(server, method string, req, reply proto.Message) error

Call sends req to the given server method as a JSON-encoded body and unmarshals the response body as JSON into reply.

func ReadJSONBody

func ReadJSONBody(r *http.Request, msg proto.Message) error

ReadJSONBody reads the entire body of r and unmarshals it from JSON into msg. If the request body is empty, no error is returned and msg is unchanged.

func RegisterQuitHandler

func RegisterQuitHandler(mux *http.ServeMux)

RegisterQuitHandler adds a handler for /quitquitquit that call os.Exit(0).

func WriteJSONResponse

func WriteJSONResponse(w http.ResponseWriter, r *http.Request, v any) error

WriteJSONResponse encodes v as JSON and writes it to w.

func WriteProtoResponse

func WriteProtoResponse(w http.ResponseWriter, r *http.Request, msg proto.Message) error

WriteProtoResponse serializes msg to w.

func WriteResponse

func WriteResponse(w http.ResponseWriter, r *http.Request, msg proto.Message) error

WriteResponse writes msg to w as a serialized protobuf if the "proto" query parameter is set; otherwise as JSON.

Types

type Marshaler added in v0.0.49

type Marshaler struct{ Options protojson.MarshalOptions }

A Marshaler writes JSON-encoded protobufs.

func (Marshaler) Marshal added in v0.0.49

func (m Marshaler) Marshal(w io.Writer, msg proto.Message) error

Marshal JSON-encodings msg and writes it to w.

func (Marshaler) MarshalToString added in v0.0.49

func (m Marshaler) MarshalToString(msg proto.Message) ([]byte, error)

MarshalToString returns msg as a JSON-encoded string.

Jump to

Keyboard shortcuts

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