fix(source): add /services/{name}/source endpoint; fix 404 for service code view in funcs-console
This commit is contained in:
@@ -66,6 +66,43 @@ func (h *Handler) GetSource(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, files)
|
||||
}
|
||||
|
||||
// GetServiceSource — GET /v1/namespaces/{namespace}/services/{name}/source
|
||||
// Аналог GetSource для sless_service: скачивает tar.gz из S3 и возвращает пользовательские файлы.
|
||||
func (h *Handler) GetServiceSource(w http.ResponseWriter, r *http.Request) {
|
||||
ns := namespace(r)
|
||||
name := pathVar(r, "name")
|
||||
|
||||
svc := &slessv1alpha1.Service{}
|
||||
if err := h.K8s.Get(r.Context(), client.ObjectKey{Name: name, Namespace: ns}, svc); err != nil {
|
||||
if errors.IsNotFound(err) {
|
||||
writeJSON(w, http.StatusNotFound, errResp("service not found"))
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusInternalServerError, errResp(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
if svc.Spec.S3Key == "" {
|
||||
writeJSON(w, http.StatusOK, []sourceFileEntry{})
|
||||
return
|
||||
}
|
||||
|
||||
rc, err := h.S3.Download(r.Context(), svc.Spec.S3Key)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, errResp("download from S3: "+err.Error()))
|
||||
return
|
||||
}
|
||||
defer rc.Close()
|
||||
|
||||
files, err := extractSourceFilesFromTarGz(rc)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, errResp("extract source: "+err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, files)
|
||||
}
|
||||
|
||||
// extractSourceFilesFromTarGz читает tar.gz и возвращает пользовательские файлы.
|
||||
// Dockerfile пропускается — он генерируется builder-ом автоматически.
|
||||
// Бинарные файлы (содержат null bytes) возвращаются в base64 с binary=true.
|
||||
|
||||
Reference in New Issue
Block a user