diff options
Diffstat (limited to 'backend/main.go')
| -rw-r--r-- | backend/main.go | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/backend/main.go b/backend/main.go index 234d57d..c7a463e 100644 --- a/backend/main.go +++ b/backend/main.go @@ -7,6 +7,7 @@ import ( "llostre/backend/fetchers" "log/slog" "net/http" + "net/url" "os" "strings" "sync" @@ -317,6 +318,48 @@ func postsHandler(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(posts) } +func postHandler(w http.ResponseWriter, r *http.Request) { + uri := strings.TrimPrefix(r.URL.Path, "/api/post/") + if uri == "" { + http.Error(w, "uri required", http.StatusBadRequest) + return + } + + // decode if URL-encoded + uri, err := url.QueryUnescape(uri) + if err != nil { + http.Error(w, "invalid uri", http.StatusBadRequest) + return + } + + var currentSession *auth.OAuthState + cookie, err := r.Cookie("session_token") + if err != nil { + logger.Warn("unauthorized") + } else { + sessionsMutex.RLock() + session, exists := sessions[cookie.Value] + sessionsMutex.RUnlock() + + if !exists { + logger.Warn("invalid session") + } else { + refreshTokens(session, cookie.Value) + + currentSession = session + } + } + + note, err := fetchers.GetAppBskyFeedPostByURI(logger, uri, slingshotURL, currentSession, cache) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(note) +} + func main() { // serve static files fs := http.FileServer(http.Dir("../frontend/dist")) @@ -327,5 +370,6 @@ func main() { http.HandleFunc("/api/session", sessionHandler) http.HandleFunc("/api/login", loginHandler) http.HandleFunc("/api/callback", callbackHandler) + http.HandleFunc("/api/post/", postHandler) // /api/post/{uri} http.ListenAndServe("0.0.0.0:8080", nil) } |
