initial commit

This commit is contained in:
DeveloperDurp 2024-09-02 13:38:46 -05:00
commit 35fa88b45b
17 changed files with 630 additions and 0 deletions

5
cmd/handlers/go.mod Normal file
View file

@ -0,0 +1,5 @@
module gitlab.com/developerdurp/durpify/handlers
go 1.23.0
require gitlab.com/developerdurp/logger v1.0.0

2
cmd/handlers/go.sum Normal file
View file

@ -0,0 +1,2 @@
gitlab.com/developerdurp/logger v1.0.0 h1:wozbKR26RVoFVaUgJV2x8WsZHLWOJBqaSCSTpK7crfk=
gitlab.com/developerdurp/logger v1.0.0/go.mod h1:x6gZvBeEq8oQUXeQoLY2m78mYJjvb5KE+7Vb5AcS8oo=

109
cmd/handlers/handlers.go Normal file
View file

@ -0,0 +1,109 @@
package handlers
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"gitlab.com/developerdurp/logger"
)
type BasicMessage struct {
Message string `json:"message"`
}
type StandardMessage struct {
Message interface{}
Status int `json:"status"`
}
type StandardError struct {
Message string `json:"message"`
Status int `json:"status"`
Description []string `json:"description"`
}
func (e StandardError) Error() string {
return fmt.Sprintf("Api error: %d", e.Status)
}
type Response interface {
SendResponse(w http.ResponseWriter)
Test(http.Handler)
}
func (message *StandardMessage) SendReponse(w http.ResponseWriter) {
setHeader(&w, message.Status)
// Write the message to the response body.
err := json.NewEncoder(w).Encode(message.Message)
if err != nil {
logger.LogError("Failed to Encode")
}
}
func (message *StandardError) SendReponse(w http.ResponseWriter) {
setHeader(&w, message.Status)
// Write the message to the response body.
err := json.NewEncoder(w).Encode(message)
if err != nil {
logger.LogError("Failed to Encode")
}
}
// NewFailureResponse returns a new instance of StandardError with the given message, status code and description.
func NewFailureResponse(message string, status int, description []string) StandardError {
return StandardError{
Message: message,
Status: status,
Description: description,
}
}
// NewMessageResponse returns a new instance of StandardMessage with the given message and status code.
func NewMessageResponse(message interface{}, status int) *StandardMessage {
return &StandardMessage{
Message: message,
Status: status,
}
}
// NewBasicResponse returns a new basic instance of StandardMessage with the message of OK and Status OK.
func NewBasicResponse() *StandardMessage {
return &StandardMessage{
Message: BasicMessage{
Message: "OK",
},
Status: http.StatusOK,
}
}
// SetHeader sets the HTTP response headers for a JSON response.
func setHeader(w *http.ResponseWriter, statusCode int) {
(*w).WriteHeader(statusCode)
(*w).Header().Set("Content-Type", "application/json")
}
type APIFunc func(w http.ResponseWriter, r *http.Request) (*StandardMessage, error)
func Make(handler APIFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
resp, err := handler(w, r)
if err != nil {
var apiErr StandardError
if errors.As(err, &apiErr) {
apiErr.SendReponse(w)
return
}
resp := NewFailureResponse(
"Internal Server Error",
http.StatusInternalServerError,
[]string{err.Error()},
)
resp.SendReponse(w)
}
resp.SendReponse(w)
}
}

View file

@ -0,0 +1,113 @@
package handlers
import (
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func TestSendResponseStandardMessage(t *testing.T) {
message := &BasicMessage{
Message: "Hello World!",
}
resp := &StandardMessage{
Status: http.StatusAccepted,
Message: message,
}
w := httptest.NewRecorder()
resp.SendReponse(w)
// Check the status code is set correctly
if w.Code != 202 {
t.Errorf("Expected status code to be 202, but got %d", w.Code)
}
// Check that the content type header is set to "application/json"
contentType := w.Header().Get("Content-Type")
if contentType != "application/json" {
t.Errorf("Expected content type to be 'application/json', but got %s", contentType)
}
// Check that the message is written to the response body correctly
response := &BasicMessage{}
err := json.NewDecoder(w.Body).Decode(response)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(message, response) {
t.Errorf("Expected body to be %s but got %s", message, response)
}
}
func TestSendResponseStandardError(t *testing.T) {
resp := &StandardError{
Status: http.StatusInternalServerError,
Message: "An error has occured",
Description: []string{"An Error"},
}
w := httptest.NewRecorder()
resp.SendReponse(w)
// Check the status code is set correctly
if w.Code != 500 {
t.Errorf("Expected status code to be 500, but got %d", w.Code)
}
// Check that the content type header is set to "application/json"
contentType := w.Header().Get("Content-Type")
if contentType != "application/json" {
t.Errorf("Expected content type to be 'application/json', but got %s", contentType)
}
// Check that the message is written to the response body correctly
response := &StandardError{}
err := json.NewDecoder(w.Body).Decode(response)
if err != nil {
t.Fatal(err)
}
if response.Message != resp.Message {
t.Errorf("Expected Message of %s but got %s", resp.Message, response.Message)
}
if !reflect.DeepEqual(resp, response) {
t.Errorf("Expected Message of %v but got %v", resp, response)
}
}
// NewFailureResponse returns a new instance of StandardError with the given message, status code and description.
func TestNewFailureResponse(t *testing.T) {
message := "An error has occured"
status := http.StatusInternalServerError
description := []string{"An Error"}
resp := NewFailureResponse(message, status, description)
if resp.Status != status {
t.Errorf("Expected Status to be %d but got %d", status, resp.Status)
}
if resp.Message != message {
t.Errorf("Expected Status to be %s but got %s", message, resp.Message)
}
if !reflect.DeepEqual(description, resp.Description) {
t.Errorf("Expected Status to be %v but got %v", description, resp.Description)
}
}
func TestNewMessageResponse(t *testing.T) {
message := &BasicMessage{
Message: "Hello World!",
}
resp := NewMessageResponse(message, http.StatusOK)
if resp.Status != http.StatusOK {
t.Errorf("Expected Status to be %d but got %d", http.StatusOK, resp.Status)
}
if !reflect.DeepEqual(message, resp.Message) {
t.Errorf("Expected Message to be %s but got %s", message, resp.Message)
}
}