General HTTP helper library aims to be customizable.
package main
import (
"context""os""github.com/infiniteloopcloud/hyper""github.com/infiniteloopcloud/log"
)
funcmain() {
varh= 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()
}
funcpreHookSetLogLevel(_ context.Context) {
log.SetLevel(func() uint8 {
return1
}())
}package main
import (
"context""os""github.com/infiniteloopcloud/hyper""github.com/infiniteloopcloud/log"
)
funcmain() {
varh= 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 informationifos.Getenv("SERVICE_TLS_SUPPORT") =="PEM_BUILDER" {
h.TLSEnabled=trueh.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
} elseifos.Getenv("SERVICE_TLS_SUPPORT") =="FILE" {
h.TLSEnabled=trueh.TLS=hyper.NewFileTLS(hyper.FileTLSOpts{
TLSCertPath: "SERVICE_TLS_CERT",
TLSKeyPath: "SERVICE_TLS_KEY",
})
}
h.Run()
}
funcpreHookSetLogLevel(_ context.Context) {
log.SetLevel(func() uint8 {
return1
}())
}package main
import (
"net/http""github.com/infiniteloopcloud/hyper"
)
typeRequeststruct {
Field1string`json:"field1"`
}
funcmain() {}
funchandler(w http.ResponseWriter, r*http.Request) {
ctx:=r.Context()
varreqStructRequest// Bind the JSON requestiferr:=hyper.Bind(ctx, r, &reqStruct); err!=nil {
hyper.ReturnBadRequest(ctx, w, "invalid request body", err)
return
}
w.WriteHeader(http.StatusOK)
}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"
)
typeRequeststruct {
Field1string`json:"field1"`
}
funcmain() {}
funchandler(w http.ResponseWriter, r*http.Request) {
ctx:=r.Context()
varreqStructRequestiferr:=hyper.Bind(ctx, r, &reqStruct); err!=nil {
// Return bad request will write the response into whyper.ReturnBadRequest(ctx, w, "invalid request body", err)
return
}
w.WriteHeader(http.StatusOK)
}HTTP client-side helper functions.
package main
import (
"context""github.com/infiniteloopcloud/hyper"
)
typeResponsestruct {
Field1string`json:"f"`
}
funcmain() {
ctx:=context.Background()
varrespResponsehyper.Request(ctx, &resp, hyper.RequestOpts{
Method: "POST",
Endpoint: "/api/endpoint",
// RequestStruct: req,Client: hyper.Client(),
})
// handle err// use resp
}