-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from jvcoutinho/feature/result-package
Result package
- Loading branch information
Showing
5 changed files
with
106 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package lit | ||
|
||
// Result of an HTTP request. | ||
// | ||
// Use the result package in order to produce Result implementations. | ||
type Result interface { | ||
// Write writes the result into the HTTP response managed by ctx. | ||
Write(ctx *Context) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Package result contains functions that return responses for incoming requests. | ||
package result | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
// Ok responds the request with Status Code 200 (OK). | ||
func Ok() *TerminalResponse { | ||
return newTerminalResponse(http.StatusOK, nil) | ||
} | ||
|
||
// BadRequest responds the request with Status Code 400 (OK). | ||
func BadRequest() *TerminalResponse { | ||
return newTerminalResponse(http.StatusBadRequest, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package result | ||
|
||
import ( | ||
"github.com/jvcoutinho/lit" | ||
) | ||
|
||
// TerminalResponse writes a single chunk of bytes into the response and closes the request. | ||
type TerminalResponse struct { | ||
statusCode int | ||
body []byte | ||
header map[string]string | ||
} | ||
|
||
func newTerminalResponse(statusCode int, body []byte) *TerminalResponse { | ||
return &TerminalResponse{statusCode: statusCode, body: body} | ||
} | ||
|
||
func (r *TerminalResponse) AddHeader(key string, value string) { | ||
if r.header == nil { | ||
r.header = make(map[string]string) | ||
} | ||
|
||
r.header[key] = value | ||
} | ||
|
||
func (r *TerminalResponse) Write(ctx *lit.Context) { | ||
responseWriter := ctx.ResponseWriter | ||
|
||
header := responseWriter.Header() | ||
for key, value := range r.header { | ||
header.Set(key, value) | ||
} | ||
|
||
responseWriter.WriteHeader(r.statusCode) | ||
|
||
_, _ = responseWriter.Write(r.body) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters