Categorygithub.com/infiniteloopcloud/hyper
modulepackage
0.4.1
Repository: https://github.com/infiniteloopcloud/hyper.git
Documentation: pkg.go.dev

# README

Hyper

General HTTP helper library aims to be customizable.

Usage

Non-TLS

package main

import (
	"context"
	"os"

	"github.com/infiniteloopcloud/hyper"
	"github.com/infiniteloopcloud/log"
)

func main() {
	var h = hyper.HTTP{
		PreHooks: []func(ctx context.Context){
			preHookSetLogLevel,
		},
		Log: hyper.Logger{
			Infof:  log.Infof,
			Errorf: log.Errorf,
		},
		Address: os.Getenv("SERVICE_HTTP_ADDRESS"),
		Handler: chi.NewRouter(),
	}
	
	h.Run()
}

func preHookSetLogLevel(_ context.Context) {
	log.SetLevel(func() uint8 {
		return 1
	}())
}

TLS

package main

import (
	"context"
	"os"

	"github.com/infiniteloopcloud/hyper"
	"github.com/infiniteloopcloud/log"
)

func main() {
	var h = hyper.HTTP{
		PreHooks: []func(ctx context.Context){
			preHookSetLogLevel,
		},
		Log: hyper.Logger{
			Infof:  log.Infof,
			Errorf: log.Errorf,
		},
		Address: os.Getenv("SERVICE_HTTP_ADDRESS"),
		Handler: chi.NewRouter(),
	}

	// NewEnvironmentTLS able to build PEM block based on the provided information
	if os.Getenv("SERVICE_TLS_SUPPORT") == "PEM_BUILDER" {
		h.TLSEnabled = true
		h.TLS = hyper.NewEnvironmentTLS(hyper.EnvironmentTLSOpts{
			TLSCert:          "SERVICE_TLS_CERT",
			TLSCertBlockType: "SERVICE_TLS_CERT_BLOCK_NAME",
			TLSKey:           "SERVICE_TLS_KEY",
			TLSKeyBlockType:  "SERVICE_TLS_KEY_BLOCK_NAME",
		})
    // NewFileTLS reads the files from the provided location
	} else if os.Getenv("SERVICE_TLS_SUPPORT") == "FILE" {
		h.TLSEnabled = true
		h.TLS = hyper.NewFileTLS(hyper.FileTLSOpts{
			TLSCertPath: "SERVICE_TLS_CERT",
			TLSKeyPath:  "SERVICE_TLS_KEY",
		})
	}
	
	h.Run()
}

func preHookSetLogLevel(_ context.Context) {
	log.SetLevel(func() uint8 {
		return 1
	}())
}

Bind request body

package main

import (
	"net/http"

	"github.com/infiniteloopcloud/hyper"
)

type Request struct {
	Field1 string `json:"field1"`
}

func main() {}

func handler(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()

	var reqStruct Request
	// Bind the JSON request
	if err := hyper.Bind(ctx, r, &reqStruct); err != nil {
		hyper.ReturnBadRequest(ctx, w, "invalid request body", err)
		return
	}

	w.WriteHeader(http.StatusOK)
}

Error handling

There are pure error wrappers which wraps the error into a weird.Error:

  • BadRequest(...)
  • NotFound(...)
  • Unauthorized(...)
  • Forbidden(...)
  • InternalServerError(...)

All of these has a Return{...} function if the function has access to http.ResponseWriter

  • ReturnBadRequest(...)
  • ReturnNotFound(...)
  • ReturnUnauthorized(...)
  • ReturnForbidden(...)
  • ReturnInternalServerError(...)
package main

import (
	"net/http"

	"github.com/infiniteloopcloud/hyper"
)

type Request struct {
	Field1 string `json:"field1"`
}

func main() {}

func handler(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()

	var reqStruct Request
	if err := hyper.Bind(ctx, r, &reqStruct); err != nil {
		// Return bad request will write the response into w
		hyper.ReturnBadRequest(ctx, w, "invalid request body", err)
		return
	}

	w.WriteHeader(http.StatusOK)
}

Client

HTTP client-side helper functions.

package main

import (
	"context"

	"github.com/infiniteloopcloud/hyper"
)

type Response struct {
	Field1 string `json:"f"`
}

func main() {
	ctx := context.Background()
	var resp Response
	hyper.Request(ctx, &resp, hyper.RequestOpts{
		Method:         "POST",
		Endpoint:       "/api/endpoint",
		// RequestStruct:  req,
		Client:         hyper.Client(),
	})
	// handle err
	// use resp
}

# Functions

No description provided by the author
Bind is binding the request body into v variable.
No description provided by the author
Created build a success response with HTTP Status Created.
No description provided by the author
Error build an error response, the HTTP status will get from the error, default 500.
No description provided by the author
No description provided by the author
Generic build a generic response.
No description provided by the author
GetQueryStringParam getting a query param if exist or return with null.String{Valid: false}.
GetQueryTimeParam getting a query param if exist or return with null.Time{Valid: false}.
GetQueryUint64Param getting a query param if exist or return with null.Uint64{Valid: false}.
No description provided by the author
Hello returns http.StatusOk and the Hello response.
No description provided by the author
No description provided by the author
Livez returns http.StatusOk when the service does not need to be killed.
No description provided by the author
No description provided by the author
No description provided by the author
NewWriter creates a new Writes.
NoContent build a no content success response.
No description provided by the author
NotReady disables the server to accept connections.
Ready enables the server to accept connections.
Readyz returns http.StatusOk when the service is ready accept connections, otherwise Http.StatusServiceUnavailable.
No description provided by the author
nolint:gocritic.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Success build a success response with HTTP Status OK.
SuccessDownload build a success response with content type octet/stream.
No description provided by the author

# Constants

No description provided by the author
No description provided by the author
No description provided by the author

# Variables

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
MockReadCloser implements the io.ReadCloser interface Mocks the request.Body.
No description provided by the author
No description provided by the author
Wrapper wraps the response.
Writer also implements the http.ResponseWriter interface In the future we may add other fields as well.

# Interfaces

No description provided by the author
No description provided by the author

# Type aliases

No description provided by the author