mirror of
https://github.com/aykhans/bsky-feedgen.git
synced 2025-07-17 21:34:00 +00:00
🦋
This commit is contained in:
27
pkg/api/response/json.go
Normal file
27
pkg/api/response/json.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/aykhans/bsky-feedgen/pkg/logger"
|
||||
)
|
||||
|
||||
type M map[string]any
|
||||
|
||||
func JSON(w http.ResponseWriter, statusCode int, data any) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(statusCode)
|
||||
if err := json.NewEncoder(w).Encode(data); err != nil {
|
||||
logger.Log.Error("Failed to encode JSON response", "error", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func JSON500(w http.ResponseWriter) {
|
||||
JSON(w, 500, M{"error": "Internal server error"})
|
||||
}
|
||||
|
||||
func JSON404(w http.ResponseWriter) {
|
||||
JSON(w, 404, M{"error": "Not found"})
|
||||
}
|
24
pkg/api/response/text.go
Normal file
24
pkg/api/response/text.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/aykhans/bsky-feedgen/pkg/logger"
|
||||
)
|
||||
|
||||
func Text(w http.ResponseWriter, statusCode int, content []byte) {
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
w.WriteHeader(statusCode)
|
||||
if _, err := w.Write(content); err != nil {
|
||||
logger.Log.Error("Failed to write text response", "error", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func Text404(w http.ResponseWriter) {
|
||||
Text(w, 404, []byte("Not found"))
|
||||
}
|
||||
|
||||
func Text500(w http.ResponseWriter) {
|
||||
Text(w, 500, []byte("Internal server error"))
|
||||
}
|
Reference in New Issue
Block a user