feat(httpapi): REST resources for endpoints/tasks/accounts/csv/run
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/vasyansk/imap-copier/internal/store"
|
||||
)
|
||||
|
||||
func writeJSON(w http.ResponseWriter, code int, v any) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(code)
|
||||
_ = json.NewEncoder(w).Encode(v)
|
||||
}
|
||||
|
||||
func (s *Server) handleCreateEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
var e store.Endpoint
|
||||
if err := json.NewDecoder(r.Body).Decode(&e); err != nil {
|
||||
http.Error(w, "bad json", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if e.TLSMode != "ssl" && e.TLSMode != "starttls" && e.TLSMode != "plain" {
|
||||
http.Error(w, "tls_mode must be ssl|starttls|plain", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
id, err := s.store.CreateEndpoint(r.Context(), e)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusCreated, map[string]int64{"id": id})
|
||||
}
|
||||
|
||||
func (s *Server) handleListEndpoints(w http.ResponseWriter, r *http.Request) {
|
||||
eps, err := s.store.ListEndpoints(r.Context())
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, eps)
|
||||
}
|
||||
Reference in New Issue
Block a user