Diffdown is a real-time collaborative Markdown editor/previewer built on the AT Protocol diffdown.com

fix: return JSON from invite handler, use BaseURL from config

+21 -26
+5 -1
cmd/server/main.go
··· 28 28 if sessionSecret == "" { 29 29 sessionSecret = "dev-secret-change-in-production" 30 30 } 31 + baseURL := os.Getenv("BASE_URL") 32 + if baseURL == "" { 33 + baseURL = "http://127.0.0.1:8080" 34 + } 31 35 32 36 // Init 33 37 auth.InitStore(sessionSecret) ··· 65 69 "templates/"+name, 66 70 )) 67 71 } 68 - h := handler.New(database, tmpls) 72 + h := handler.New(database, tmpls, baseURL) 69 73 70 74 // Routes 71 75 mux := http.NewServeMux()
+16 -25
internal/handler/handler.go
··· 6 6 "html/template" 7 7 "log" 8 8 "net/http" 9 - "os" 10 9 "regexp" 11 10 "strings" 12 11 "time" ··· 22 21 const collectionDocument = "com.diffdown.document" 23 22 24 23 type Handler struct { 25 - DB *db.DB 26 - Tmpls map[string]*template.Template 24 + DB *db.DB 25 + Tmpls map[string]*template.Template 26 + BaseURL string 27 27 } 28 28 29 - func New(database *db.DB, tmpls map[string]*template.Template) *Handler { 30 - return &Handler{DB: database, Tmpls: tmpls} 29 + func New(database *db.DB, tmpls map[string]*template.Template, baseURL string) *Handler { 30 + return &Handler{DB: database, Tmpls: tmpls, BaseURL: baseURL} 31 31 } 32 32 33 33 // --- Template helpers --- ··· 67 67 } 68 68 } 69 69 70 - func (h *Handler) jsonResponse(w http.ResponseWriter, data interface{}) { 70 + func (h *Handler) jsonResponse(w http.ResponseWriter, data interface{}, statusCode int) { 71 71 w.Header().Set("Content-Type", "application/json") 72 + w.WriteHeader(statusCode) 72 73 json.NewEncoder(w).Encode(data) 73 74 } 74 75 ··· 391 392 return 392 393 } 393 394 394 - h.jsonResponse(w, map[string]string{"status": "ok"}) 395 + h.jsonResponse(w, map[string]string{"status": "ok"}, http.StatusOK) 395 396 } 396 397 397 398 // APIDocumentAutoSave is the same as save, called on debounce from editor. ··· 420 421 return 421 422 } 422 423 423 - h.jsonResponse(w, map[string]string{"status": "ok"}) 424 + h.jsonResponse(w, map[string]string{"status": "ok"}, http.StatusOK) 424 425 } 425 426 426 427 // DocumentInvite creates an invite link for a document. ··· 463 464 return 464 465 } 465 466 466 - ownerDID := strings.TrimPrefix(doc.URI, "at://") 467 - if idx := strings.Index(ownerDID, "/"); idx > 0 { 468 - ownerDID = ownerDID[:idx] 467 + parts := strings.Split(doc.URI, "/") 468 + ownerDID := "" 469 + if len(parts) >= 2 { 470 + ownerDID = parts[1] 469 471 } 470 472 if ownerDID == "" || session.DID != ownerDID { 471 473 http.Error(w, "Unauthorized", http.StatusForbidden) ··· 484 486 return 485 487 } 486 488 487 - baseURL := os.Getenv("BASE_URL") 488 - if baseURL == "" { 489 - baseURL = "http://127.0.0.1:8080" 490 - } 491 - inviteLink := fmt.Sprintf("%s/doc/%s?invite=%s", baseURL, rkey, invite.Token) 492 - h.render(w, "document_edit.html", PageData{ 493 - Title: "Edit " + doc.Title, 494 - User: user, 495 - Content: map[string]interface{}{ 496 - "document": doc, 497 - "inviteLink": inviteLink, 498 - }, 499 - }) 489 + inviteLink := fmt.Sprintf("%s/doc/%s?invite=%s", h.BaseURL, rkey, invite.Token) 490 + h.jsonResponse(w, map[string]string{"inviteLink": inviteLink}, http.StatusOK) 500 491 } 501 492 502 493 // --- API: Render markdown --- ··· 516 507 return 517 508 } 518 509 519 - h.jsonResponse(w, map[string]string{"html": rendered}) 510 + h.jsonResponse(w, map[string]string{"html": rendered}, http.StatusOK) 520 511 }