package main import ( "log" "encoding/json" "net/http" "time" ) type VideoDetails struct { Author string `json:"author"` Channel string `json:"channel"` Published time.Time `json:"published"` Description string `json:"description"` Comments int `json:"comments"` // nunber of comments Likes int `json:"likes"` Views int `json:"views"` Tags []string `json:"tags"` } type Comment struct { Author string `json:"author"` Body string `json:"body"` Published time.Time `json:"published"` Updated time.Time `json:"updated,omitempty"` Likes int `json:"likes"` Replies []*Comment `json:"replies,omitempty"` } type APISource interface { getDetails(id string) (VideoDetails, error) getComments(id string) ([]Comment, error) } func apiDataHandler[T any](f func(id string) (T, error)) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { videoID := r.URL.Query().Get("id") if videoID == "" { msg := "Missing ?id=VIDEO_ID parameter" http.Error(w, msg, http.StatusBadRequest) return } data, err := f(videoID) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) log.Println(err.Error()) return } w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(data) } } func registerRoutesAPI(mux *http.ServeMux, api APISource) { mux.HandleFunc("/api/details", apiDataHandler(api.getDetails)) mux.HandleFunc("/api/comments", apiDataHandler(api.getComments)) }