Some checks failed
CI / Run tests and lints (push) Failing after 29s
CI / Build CLI docs (push) Has been skipped
CI / Run Playwright end-to-end tests (mysql) (push) Failing after 3s
CI / Run Playwright end-to-end tests (postgres) (push) Failing after 2s
CI / Run Playwright end-to-end tests (sqlite) (push) Failing after 3s
CodeQL / Analyze (go) (push) Failing after 4s
CodeQL / Analyze (javascript) (push) Failing after 3s
Docker Image Scanners / scanners (push) Failing after 4s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Run end-to-end tests (postgres) (push) Failing after 19s
CI / Run end-to-end tests (sqlite) (push) Failing after 2s
CI / Run Playwright end-to-end tests (cockroach) (push) Failing after 2s
CI / Draft newsletter (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
68 lines
1.3 KiB
Go
68 lines
1.3 KiB
Go
// Copyright © 2024 Ory Corp
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package main
|
|
|
|
import (
|
|
"cmp"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"sync"
|
|
)
|
|
|
|
var (
|
|
documentsLock sync.RWMutex
|
|
documents = make(map[string][]byte)
|
|
)
|
|
|
|
func main() {
|
|
port := cmp.Or(os.Getenv("PORT"), "4471")
|
|
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
|
|
}
|
|
|
|
func init() {
|
|
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
|
|
_, _ = w.Write([]byte("OK"))
|
|
})
|
|
|
|
http.HandleFunc("GET /documents/{id}", func(w http.ResponseWriter, r *http.Request) {
|
|
id := r.PathValue("id")
|
|
|
|
documentsLock.RLock()
|
|
doc, ok := documents[id]
|
|
documentsLock.RUnlock()
|
|
|
|
if ok {
|
|
_, _ = w.Write(doc)
|
|
} else {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
}
|
|
})
|
|
|
|
http.HandleFunc("PUT /documents/{id}", func(w http.ResponseWriter, r *http.Request) {
|
|
documentsLock.Lock()
|
|
defer documentsLock.Unlock()
|
|
id := r.PathValue("id")
|
|
|
|
body, err := io.ReadAll(r.Body)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
documents[id] = body
|
|
})
|
|
|
|
http.HandleFunc("DELETE /documents/{id}", func(w http.ResponseWriter, r *http.Request) {
|
|
documentsLock.Lock()
|
|
defer documentsLock.Unlock()
|
|
id := r.PathValue("id")
|
|
|
|
delete(documents, id)
|
|
w.WriteHeader(http.StatusNoContent)
|
|
})
|
|
}
|