mirror of
https://gitlab.durp.info/durfy/modules/durpify.git
synced 2026-05-07 08:00:30 -05:00
update to new location
This commit is contained in:
parent
d530803872
commit
264fc0c72a
8 changed files with 102 additions and 102 deletions
|
|
@ -4,8 +4,11 @@ import (
|
|||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/charmbracelet/log"
|
||||
"net/http"
|
||||
|
||||
"github.com/a-h/templ"
|
||||
"github.com/charmbracelet/log"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type BasicMessage struct {
|
||||
|
|
@ -35,6 +38,17 @@ type Response interface {
|
|||
func (message *StandardMessage) SendReponse(w http.ResponseWriter) {
|
||||
setHeader(&w, message.Status)
|
||||
|
||||
contentType := w.Header().Get("Content-Type")
|
||||
switch contentType {
|
||||
case "application/yaml":
|
||||
|
||||
err := yaml.NewEncoder(w).Encode(message.Message)
|
||||
if err != nil {
|
||||
log.Error("Failed to Encode YAML", "error", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Write the message to the response body.
|
||||
err := json.NewEncoder(w).Encode(message.Message)
|
||||
if err != nil {
|
||||
|
|
@ -95,7 +109,7 @@ func setHeader(w *http.ResponseWriter, statusCode int) {
|
|||
type APIFunc func(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) (*StandardMessage, error)
|
||||
) (interface{}, error)
|
||||
|
||||
func Make(handler APIFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
@ -112,7 +126,21 @@ func Make(handler APIFunc) http.HandlerFunc {
|
|||
[]string{err.Error()},
|
||||
)
|
||||
resp.SendReponse(w)
|
||||
return
|
||||
}
|
||||
resp.SendReponse(w)
|
||||
|
||||
message, ok := resp.(*StandardMessage)
|
||||
if ok {
|
||||
message.SendReponse(w)
|
||||
return
|
||||
}
|
||||
|
||||
templMessage, ok := resp.(templ.Component)
|
||||
if ok {
|
||||
templMessage.Render(r.Context(), w)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte("Internal Server Error"))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue