package ginext import ( "github.com/gin-gonic/gin" "gogs.mikescher.com/BlackForestBytes/goext/langext" ) type textHTTPResponse struct { statusCode int data string headers []headerval cookies []cookieval } func (j textHTTPResponse) Write(g *gin.Context) { for _, v := range j.headers { g.Header(v.Key, v.Val) } for _, v := range j.cookies { g.SetCookie(v.name, v.value, v.maxAge, v.path, v.domain, v.secure, v.httpOnly) } g.String(j.statusCode, "%s", j.data) } func (j textHTTPResponse) WithHeader(k string, v string) HTTPResponse { j.headers = append(j.headers, headerval{k, v}) return j } func (j textHTTPResponse) WithCookie(name string, value string, maxAge int, path string, domain string, secure bool, httpOnly bool) HTTPResponse { j.cookies = append(j.cookies, cookieval{name, value, maxAge, path, domain, secure, httpOnly}) return j } func (j textHTTPResponse) IsSuccess() bool { return j.statusCode >= 200 && j.statusCode <= 399 } func (j textHTTPResponse) Statuscode() int { return j.statusCode } func (j textHTTPResponse) BodyString(*gin.Context) *string { return langext.Ptr(j.data) } func (j textHTTPResponse) ContentType() string { return "text/plain" } func (j textHTTPResponse) Headers() []string { return langext.ArrMap(j.headers, func(v headerval) string { return v.Key + "=" + v.Val }) } func Text(sc int, data string) HTTPResponse { return &textHTTPResponse{statusCode: sc, data: data} }