feat: added auth and crazy more things
This commit is contained in:
1 parent
1ecf468140
commit
be9d6b610f
10 files changed
+1500
-117
No files matched your search
@@ -1,76 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func registerAPI(mux *http.ServeMux, sqlite *sqliteStore, logger *zap.Logger) {
|
||||
mux.HandleFunc("/api/v1/flashcards", func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
func serveFlashcardDetail(w http.ResponseWriter, r *http.Request, store *sqliteStore) {
|
||||
segments := strings.Split(strings.TrimPrefix(r.URL.Path, "/api/v1/flashcards/"), "/")
|
||||
if len(segments) == 0 || segments[0] == "" {
|
||||
writeJSON(w, http.StatusNotFound, map[string]string{"error": "not found"})
|
||||
return
|
||||
}
|
||||
|
||||
summaries, err := sqlite.listAll(r.Context())
|
||||
if err != nil {
|
||||
logger.Error("list flashcards", zap.Error(err))
|
||||
http.Error(w, `{"error":"internal error"}`, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string][]flashcardSummary{"flashcards": summaries})
|
||||
})
|
||||
|
||||
mux.HandleFunc("/api/v1/flashcards/", func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
segments := strings.Split(strings.TrimPrefix(r.URL.Path, "/api/v1/flashcards/"), "/")
|
||||
if len(segments) == 0 || segments[0] == "" {
|
||||
http.Error(w, `{"error":"not found"}`, http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
publicID, err := strconv.ParseInt(segments[0], 10, 64)
|
||||
if err != nil {
|
||||
http.Error(w, `{"error":"invalid id"}`, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if len(segments) >= 2 && segments[1] != "" {
|
||||
version, err := strconv.Atoi(segments[1])
|
||||
if err != nil {
|
||||
http.Error(w, `{"error":"invalid version"}`, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
e, err := sqlite.getByPublicIDAndVersion(r.Context(), publicID, version)
|
||||
writeEntry(w, e, err)
|
||||
return
|
||||
}
|
||||
|
||||
e, err := sqlite.getLatestByPublicID(r.Context(), publicID)
|
||||
writeEntry(w, e, err)
|
||||
})
|
||||
}
|
||||
|
||||
func writeEntry(w http.ResponseWriter, e *entry, err error) {
|
||||
publicID, err := strconv.ParseInt(segments[0], 10, 64)
|
||||
if err != nil {
|
||||
http.Error(w, `{"error":"internal error"}`, http.StatusInternalServerError)
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid id"})
|
||||
return
|
||||
}
|
||||
|
||||
if len(segments) >= 2 && segments[1] != "" {
|
||||
version, err := strconv.Atoi(segments[1])
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid version"})
|
||||
return
|
||||
}
|
||||
e, err := store.getByPublicIDAndVersion(r.Context(), publicID, version)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "internal error"})
|
||||
return
|
||||
}
|
||||
if e == nil {
|
||||
writeJSON(w, http.StatusNotFound, map[string]string{"error": "not found"})
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, e)
|
||||
return
|
||||
}
|
||||
|
||||
e, err := store.getLatestByPublicID(r.Context(), publicID)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "internal error"})
|
||||
return
|
||||
}
|
||||
if e == nil {
|
||||
http.Error(w, `{"error":"not found"}`, http.StatusNotFound)
|
||||
writeJSON(w, http.StatusNotFound, map[string]string{"error": "not found"})
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(e)
|
||||
writeJSON(w, http.StatusOK, e)
|
||||
}
|
||||
Reference in new issue
Block a user