57 lines
1.6 KiB
Go
57 lines
1.6 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"gitlab.com/mstarongitlab/linstrom/ap"
|
|
)
|
|
|
|
// Helper func for redirecting a request to another url if that request does not the proper content type headers
|
|
func redirectIfNotApRequest(w http.ResponseWriter, r *http.Request, redirectTarget string) bool {
|
|
logger := ContextGetLogger(w, r)
|
|
if logger == nil {
|
|
return false
|
|
}
|
|
if ap.ContainsApContentHeader(r.Header.Get("Content-Type")) {
|
|
return false
|
|
}
|
|
// redirect with code 307 temporary redirect so that the client sends the same request, but to the given redirect target url instead
|
|
http.Redirect(w, r, redirectTarget, http.StatusTemporaryRedirect)
|
|
logger.Info().
|
|
Str("from-url", r.URL.RawPath).
|
|
Str("to-url", redirectTarget).
|
|
Msg("Redirecting non-ap request to ap endpoint to proper endpoint")
|
|
return true
|
|
}
|
|
|
|
// Mount under /api/ap/note/{id}
|
|
// Handles requests for the AP version of a note with the given id
|
|
// And redirects non-ap requests to the web version
|
|
func noteApHandler(w http.ResponseWriter, r *http.Request) {
|
|
store := ContextGetStorage(w, r)
|
|
if store == nil {
|
|
return
|
|
}
|
|
logger := ContextGetLogger(w, r)
|
|
if logger == nil {
|
|
return
|
|
}
|
|
// First things first, get the note id from the url
|
|
noteId := r.PathValue("id")
|
|
// If there is no id (empty string means no value was provided), error out
|
|
if noteId == "" {
|
|
logger.Info().Msg("Attempted to request a note without providing an ID")
|
|
http.Error(w, "missing note id", http.StatusBadRequest)
|
|
return
|
|
}
|
|
if redirectIfNotApRequest(w, r, "/notes/"+noteId) {
|
|
return
|
|
}
|
|
|
|
note, err := store.FindNoteById(noteId)
|
|
}
|
|
|
|
// Mount under /api/ap/user/{id}
|
|
func userApHandler(w http.ResponseWriter, _ *http.Request) {
|
|
|
|
}
|