add documentation

This commit is contained in:
Zeni Kim 2025-02-24 09:59:07 -05:00
parent 1b23363f6f
commit 695f1f57ba
8 changed files with 116 additions and 14 deletions

View file

@ -11,6 +11,7 @@ import (
"net/http"
)
// Response represents an HTTP response to be sent to the client, including headers, body, status code, and metadata.
type Response struct {
headers []header
body []byte
@ -22,12 +23,13 @@ type Response struct {
HttpResponseWriter http.ResponseWriter
}
// header represents a single HTTP header with a key-value pair.
type header struct {
key string
val string
}
// TODO add doc
// writes the contents of a buffer to the HTTP response with specified file name and type if not terminated.
func (rs *Response) BufferFile(name string, filetype string, b bytes.Buffer) *Response {
if rs.isTerminated == false {
@ -38,7 +40,7 @@ func (rs *Response) BufferFile(name string, filetype string, b bytes.Buffer) *Re
return rs
}
// TODO add doc
// sets the response's content type to HTML and assigns the provided body as the response body if not terminated.
func (rs *Response) Any(body any) *Response {
if rs.isTerminated == false {
rs.contentType = CONTENT_TYPE_HTML
@ -48,7 +50,7 @@ func (rs *Response) Any(body any) *Response {
return rs
}
// TODO add doc
// sets the response's content type to JSON and assigns the provided string as the body if the response is not terminated.
func (rs *Response) Json(body string) *Response {
if rs.isTerminated == false {
rs.contentType = CONTENT_TYPE_JSON
@ -57,7 +59,7 @@ func (rs *Response) Json(body string) *Response {
return rs
}
// TODO add doc
// sets the response's content type to plain text and assigns the provided string as the body if not terminated.
func (rs *Response) Text(body string) *Response {
if rs.isTerminated == false {
rs.contentType = CONTENT_TYPE_TEXT
@ -66,7 +68,7 @@ func (rs *Response) Text(body string) *Response {
return rs
}
// TODO add doc
// sets the response's content type to HTML and assigns the provided string as the body if the response is not terminated.
func (rs *Response) HTML(body string) *Response {
if rs.isTerminated == false {
rs.contentType = CONTENT_TYPE_HTML
@ -75,7 +77,7 @@ func (rs *Response) HTML(body string) *Response {
return rs
}
// TODO add doc
// renders the specified template with the provided data and writes the result to the HTTP response if not terminated.
func (rs *Response) Template(name string, data interface{}) *Response {
var buffer bytes.Buffer
if rs.isTerminated == false {
@ -89,7 +91,7 @@ func (rs *Response) Template(name string, data interface{}) *Response {
return rs
}
// TODO add doc
// sets the response status code if the response is not yet terminated. Returns the updated Response object.
func (rs *Response) SetStatusCode(code int) *Response {
if rs.isTerminated == false {
rs.statusCode = code
@ -98,7 +100,7 @@ func (rs *Response) SetStatusCode(code int) *Response {
return rs
}
// TODO add doc
// sets a custom content type for the response if it is not terminated. Returns the updated Response object.
func (rs *Response) SetContentType(c string) *Response {
if rs.isTerminated == false {
rs.overrideContentType = c
@ -107,7 +109,7 @@ func (rs *Response) SetContentType(c string) *Response {
return rs
}
// TODO add doc
// adds or updates a header to the response if it is not terminated. Returns the updated Response object.
func (rs *Response) SetHeader(key string, val string) *Response {
if rs.isTerminated == false {
h := header{
@ -119,10 +121,12 @@ func (rs *Response) SetHeader(key string, val string) *Response {
return rs
}
// terminates the response processing, preventing any further modifications or actions.
func (rs *Response) ForceSendResponse() {
rs.isTerminated = true
}
// updates the redirect URL for the response and returns the modified Response. Validates the URL before setting it.
func (rs *Response) Redirect(url string) *Response {
validator := resolveValidator()
v := validator.Validate(map[string]interface{}{
@ -142,6 +146,7 @@ func (rs *Response) Redirect(url string) *Response {
return rs
}
// converts various primitive data types to their string representation or triggers a panic for unsupported types.
func (rs *Response) castBasicVarsToString(data interface{}) string {
switch dataType := data.(type) {
case string:
@ -199,6 +204,7 @@ func (rs *Response) castBasicVarsToString(data interface{}) string {
}
}
// reinitializes the Response object to its default state, clearing its body, headers, and resetting other properties.
func (rs *Response) reset() {
rs.body = nil
rs.statusCode = http.StatusOK