ehttp

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2022 License: BSD-3-Clause Imports: 6 Imported by: 0

README

ehttp

handle http requests in a natural way

the big idea

import "gitlab.com/efronlicht/estd/ehttp"
type Match struct {
    line int
    text string
}
// POST /grep
// req: {
//    pattern <string>
//    text: <string>
//    literal: <bool>
// }
// resp:
//   [ {line <int>, match: string}]
type Request struct {
    Pattern, Text string
    Literal bool
}

func main() {
    http.ListenAndServe(ehttp.JSONHandler(grep))
}
func grep(ctx context.Context, r Request) (matches []Match, err error){
    if ctx.Err() {
        return nil, err
    }
    if literal {
        for line, text in strings.Split(r.Text) {
            if strings.Contains(text, pattern) {
                matches = append(matches, Match{line, text})
            }
        }
        return matches
    }
    re, err := regex.Compile(pattern)
    if err != nil {
        return nil, ErrorWithStatus(fmt.Errorf("invalid regex pattern %q: %w", pattern, err), http.StatusBadRequest)
    }
    for line, text in strings.Split(lines) {
        if re.MatchString(text) {
            matches = append(matches, Match{line, text})
        }
    }
    return matches
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BodyAsJSON

func BodyAsJSON[T any](r *http.Request) (t T, err error)

func BuildHandler

func BuildHandler[T any](f func(*http.Request) (t T, err error)) http.HandlerFunc

BuildHandler converts an ordinary function to a http.Handlerfunc, calling WriteSuccess(w, t) if there's no error and WriteError(w, err) if there is.

func FromBodyAsJSON

func FromBodyAsJSON[T any](r *http.Request) (T, error)

FromBodyASJSON gets a T from the request body by calling json.NewDecoder.Decode.

func Handler

func Handler[I, O any](
	fromRequest func(*http.Request) (input I, err error),
	handle func(context.Context, I) (output O, err error),
) http.HandlerFunc

func StatusCode

func StatusCode(v any) int

StatusCode is a sensible default. If v implements interface{StatusCode() int}, or is an error that wraps something that does, Otherwise, if it's an error, use i

func WriteResponse

func WriteResponse(w http.ResponseWriter, resp Response) (int64, error)

WriteResponse writes resp to the ResponseWriter.

Types

type JSON

type JSON[T any] struct{ Inner T }

func (JSON[T]) WriterTo

func (j JSON[T]) WriterTo(w io.Writer) (int64, error)

type Response

type Response interface {
	// StatusCode like like http.StatusOK or http.InternalServerError.
	StatusCode() int
	io.WriterTo
}

type Status

type Status int

func (Status) StatusCode

func (s Status) StatusCode() int

Directories

Path Synopsis
middleware
srv
srv contains server middleware.
srv contains server middleware.

Jump to

Keyboard shortcuts

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