CHANGE: Switched The Interface From HTML to JSON API
Build, Push and Deploy CMS / build-push-deploy (push) Successful in 1m53s

This commit is contained in:
FahdShalhoub
2026-08-16 23:15:53 +03:00
parent 8bf0eae425
commit acae765948
14 changed files with 65 additions and 817 deletions
+15 -12
View File
@@ -1,20 +1,23 @@
package handlers
import (
"encoding/json"
"log"
"net/http"
"thamanyah/cms/v2/internal/views"
)
func Home(w http.ResponseWriter, r *http.Request) {
views.Home().Render(r.Context(), w)
}
func Hello(w http.ResponseWriter, r *http.Request) {
views.Greeting("Fahd Shalhoub").Render(r.Context(), w)
}
func Health(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Write([]byte("ok"))
writeJSON(w, http.StatusOK, map[string]string{"status": "ok"})
}
func writeJSON(w http.ResponseWriter, status int, body any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
if err := json.NewEncoder(w).Encode(body); err != nil {
log.Printf("Something Went Wrong Writing The Response: %s", err)
}
}
func writeJSONError(w http.ResponseWriter, status int, message string) {
writeJSON(w, status, map[string]string{"error": message})
}
+46 -34
View File
@@ -10,7 +10,6 @@ import (
"path/filepath"
"strings"
"thamanyah/cms/v2/internal/services"
"thamanyah/cms/v2/internal/views"
"time"
)
@@ -20,15 +19,22 @@ const (
maxJSONBodySize = 1 << 20 // 1 MiB
)
func NewVideo(w http.ResponseWriter, r *http.Request) {
type categoriesResponse struct {
Categories []string `json:"categories"`
}
func ListCategories(w http.ResponseWriter, r *http.Request) {
categories, err := services.DB.ListCategories(r.Context())
if err != nil {
log.Printf("Something Went Wrong Loading Categories: %s", err)
http.Error(w, "Something Went Wrong Loading Categories", http.StatusInternalServerError)
writeJSONError(w, http.StatusInternalServerError, "Something Went Wrong Loading Categories")
return
}
views.VideoUpload(categories).Render(r.Context(), w)
if categories == nil {
categories = []string{}
}
writeJSON(w, http.StatusOK, categoriesResponse{Categories: categories})
}
type presignRequest struct {
@@ -68,8 +74,7 @@ func PresignVideoUpload(w http.ResponseWriter, r *http.Request) {
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(presignResponse{UploadURL: presignedURL, Key: key})
writeJSON(w, http.StatusOK, presignResponse{UploadURL: presignedURL, Key: key})
}
type completeRequest struct {
@@ -81,24 +86,39 @@ type completeRequest struct {
Key string `json:"key"`
}
type videoResponse struct {
ID string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Categories []string `json:"categories"`
Tags string `json:"tags"`
FileName string `json:"fileName"`
StorageKey string `json:"storageKey"`
MediaConvertJobID string `json:"mediaConvertJobId"`
Status string `json:"status"`
SizeBytes int64 `json:"sizeBytes"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
func CompleteVideoUpload(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, maxJSONBodySize)
var req completeRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
renderUploadError(w, r, "Invalid request.")
writeJSONError(w, http.StatusBadRequest, "Invalid request.")
return
}
title := strings.TrimSpace(req.Title)
if title == "" {
renderUploadError(w, r, "Title is required.")
writeJSONError(w, http.StatusUnprocessableEntity, "Title is required.")
return
}
key := strings.TrimSpace(req.Key)
if key == "" || !strings.HasPrefix(key, videoUploadPrefix+"/") {
renderUploadError(w, r, "A video file is required.")
writeJSONError(w, http.StatusUnprocessableEntity, "A video file is required.")
return
}
@@ -112,11 +132,11 @@ func CompleteVideoUpload(w http.ResponseWriter, r *http.Request) {
jobID, err := services.MediaConvertClient.QueueEncodingJob(r.Context(), key)
if err != nil {
log.Printf("Something Went Wrong On Creation Of Transcoding Job: %s", err)
renderUploadError(w, r, "Something Went Wrong On Creation Of Transcoding Job")
writeJSONError(w, http.StatusInternalServerError, "Something Went Wrong On Creation Of Transcoding Job")
return
}
_, err = services.DB.CreateVideo(r.Context(), services.Video{
video, err := services.DB.CreateVideo(r.Context(), services.Video{
Title: title,
Description: strings.TrimSpace(req.Description),
Categories: categories,
@@ -129,32 +149,24 @@ func CompleteVideoUpload(w http.ResponseWriter, r *http.Request) {
})
if err != nil {
log.Printf("Something Went Wrong Saving The Video Record: %s", err)
renderUploadError(w, r, "Something Went Wrong Saving The Video Record")
writeJSONError(w, http.StatusInternalServerError, "Something Went Wrong Saving The Video Record")
return
}
views.VideoUploadSuccess(views.VideoMetadata{
Title: title,
Description: strings.TrimSpace(req.Description),
Categories: categories,
Tags: strings.TrimSpace(req.Tags),
FileName: strings.TrimSpace(req.FileName),
JobID: jobID,
StoredAs: key,
SizeBytes: 60,
}).Render(r.Context(), w)
}
func writeJSONError(w http.ResponseWriter, status int, message string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
json.NewEncoder(w).Encode(map[string]string{"error": message})
}
func renderUploadError(w http.ResponseWriter, r *http.Request, message string) {
fmt.Println("Error Ocurred: " + message)
w.WriteHeader(http.StatusUnprocessableEntity)
views.VideoUploadError(message).Render(r.Context(), w)
writeJSON(w, http.StatusCreated, videoResponse{
ID: video.ID,
Title: video.Title,
Description: video.Description,
Categories: video.Categories,
Tags: video.Tags,
FileName: video.FileName,
StorageKey: video.StorageKey,
MediaConvertJobID: video.MediaConvertJobID,
Status: video.Status,
SizeBytes: video.SizeBytes,
CreatedAt: video.CreatedAt,
UpdatedAt: video.UpdatedAt,
})
}
func randomFilename(original string) (string, error) {