159 lines
3.9 KiB
Go
159 lines
3.9 KiB
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/gorilla/mux"
|
|
"github.com/sirupsen/logrus"
|
|
"markdown-editor/internal/storage"
|
|
)
|
|
|
|
type Handlers struct {
|
|
logger *logrus.Logger
|
|
storage storage.Storage
|
|
}
|
|
|
|
func NewHandlers(logger *logrus.Logger, storage storage.Storage) *Handlers {
|
|
return &Handlers{
|
|
logger: logger,
|
|
storage: storage,
|
|
}
|
|
}
|
|
|
|
func (h *Handlers) ListFiles() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
files, err := h.storage.ListFiles()
|
|
if err != nil {
|
|
h.logger.Errorf("Failed to list files: %v", err)
|
|
h.writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(map[string]interface{}{
|
|
"files": files,
|
|
})
|
|
}
|
|
}
|
|
|
|
func (h *Handlers) CreateFile() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
var req struct {
|
|
Filename string `json:"filename"`
|
|
Content string `json:"content"`
|
|
}
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
h.logger.Errorf("Failed to decode request body: %v", err)
|
|
h.writeError(w, http.StatusBadRequest, "invalid request body")
|
|
return
|
|
}
|
|
|
|
if req.Filename == "" {
|
|
h.writeError(w, http.StatusBadRequest, "filename is required")
|
|
return
|
|
}
|
|
|
|
if err := h.storage.WriteFile(req.Filename, []byte(req.Content)); err != nil {
|
|
h.logger.Errorf("Failed to create file: %v", err)
|
|
h.writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(map[string]string{
|
|
"message": "file created successfully",
|
|
})
|
|
}
|
|
}
|
|
|
|
func (h *Handlers) GetFile() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
filename := vars["filename"]
|
|
|
|
if filename == "" {
|
|
h.writeError(w, http.StatusBadRequest, "filename is required")
|
|
return
|
|
}
|
|
|
|
content, err := h.storage.ReadFile(filename)
|
|
if err != nil {
|
|
h.logger.Errorf("Failed to read file: %v", err)
|
|
h.writeError(w, http.StatusNotFound, err.Error())
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(map[string]interface{}{
|
|
"filename": filename,
|
|
"content": string(content),
|
|
})
|
|
}
|
|
}
|
|
|
|
func (h *Handlers) UpdateFile() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
filename := vars["filename"]
|
|
|
|
if filename == "" {
|
|
h.writeError(w, http.StatusBadRequest, "filename is required")
|
|
return
|
|
}
|
|
|
|
var req struct {
|
|
Content string `json:"content"`
|
|
}
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
h.logger.Errorf("Failed to decode request body: %v", err)
|
|
h.writeError(w, http.StatusBadRequest, "invalid request body")
|
|
return
|
|
}
|
|
|
|
if err := h.storage.WriteFile(filename, []byte(req.Content)); err != nil {
|
|
h.logger.Errorf("Failed to update file: %v", err)
|
|
h.writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(map[string]string{
|
|
"message": "file updated successfully",
|
|
})
|
|
}
|
|
}
|
|
|
|
func (h *Handlers) DeleteFile() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
filename := vars["filename"]
|
|
|
|
if filename == "" {
|
|
h.writeError(w, http.StatusBadRequest, "filename is required")
|
|
return
|
|
}
|
|
|
|
if err := h.storage.DeleteFile(filename); err != nil {
|
|
h.logger.Errorf("Failed to delete file: %v", err)
|
|
h.writeError(w, http.StatusNotFound, err.Error())
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(map[string]string{
|
|
"message": "file deleted successfully",
|
|
})
|
|
}
|
|
}
|
|
|
|
func (h *Handlers) writeError(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,
|
|
})
|
|
}
|