Lots of work

This commit is contained in:
mStar 2024-05-31 11:54:39 +02:00
parent a2a937791d
commit 3086b0e9b4
28 changed files with 1284 additions and 2 deletions

36
server/endpoints_ap.go Normal file
View file

@ -0,0 +1,36 @@
package server
import (
"fmt"
"net/http"
"strings"
"github.com/sirupsen/logrus"
"gitlab.com/mstarongitlab/linstrom/server/middlewares"
"gitlab.com/mstarongitlab/linstrom/storage"
)
// Mount under /.well-known/webfinger
func webfingerHandler(w http.ResponseWriter, r *http.Request) {
logEntry, ok := r.Context().Value(middlewares.CONTEXT_KEY_LOGRUS).(*logrus.Entry)
if !ok {
http.Error(w, "couldn't get logging entry from context", http.StatusInternalServerError)
return
}
store := storage.Storage{}
requestedResource := r.FormValue("resource")
if requestedResource == "" {
http.Error(w, "bad request. Include \"resource\" parameter", http.StatusBadRequest)
logEntry.Infoln("No resource parameter. Cancelling")
return
}
accName := strings.TrimPrefix(requestedResource, "acc:")
acc, err := store.FindLocalAccount(accName)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
logEntry.WithError(err).Warningln("couldn't find account")
return
}
fmt.Fprint(w, acc)
}

View file

@ -0,0 +1,20 @@
package middlewares
import (
"context"
"net/http"
"github.com/sirupsen/logrus"
)
const CONTEXT_KEY_LOGRUS = ContextKey("logrus")
// Inject a logrus entry into the context that has the url path already set
func InjectLogrusMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
reqContext := r.Context()
entry := logrus.WithField("url-path", r.URL.Path)
newContext := context.WithValue(reqContext, CONTEXT_KEY_LOGRUS, entry)
next.ServeHTTP(w, r.WithContext(newContext))
})
}

View file

@ -0,0 +1,22 @@
package middlewares
import (
"context"
"net/http"
"gitlab.com/mstarongitlab/linstrom/storage"
)
const CONTEXT_KEY_STORAGE = ContextKey("storage")
// Build a middleware for injecting a storage reference into the context
func InjectStorageMiddlewareBuilder(store *storage.Storage) func(http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
reqContext := r.Context()
newContext := context.WithValue(reqContext, CONTEXT_KEY_STORAGE, store)
newRequest := r.WithContext(newContext)
h.ServeHTTP(w, newRequest)
})
}
}

View file

@ -0,0 +1,14 @@
package middlewares
import "net/http"
type ContextKey string
type MiddlewareFunc func(http.Handler) http.Handler
func ChainMiddlewares(start http.Handler, middlewares ...MiddlewareFunc) http.Handler {
next := start
for _, h := range middlewares {
next = h(next)
}
return next
}

23
server/notes.go Normal file
View file

@ -0,0 +1,23 @@
package server
import (
"net/http"
"gitlab.com/mstarongitlab/linstrom/ap"
)
// Mount at /notes/{note-id}
// Handles the note endpoint
// Serves the json-ld representation of a note OR the frontend view
func noteHandler(w http.ResponseWriter, r *http.Request) {
if ap.ContainsApContentHeader(r.Header.Get("Content-Type")) {
apNote(w, r)
} else {
renderNote(w, r)
}
}
func renderNote(w http.ResponseWriter, r *http.Request) {
http.Error(w, "not implemented yet", http.StatusInternalServerError)
}
func apNote(w http.ResponseWriter, r *http.Request) {}

27
server/server.go Normal file
View file

@ -0,0 +1,27 @@
package server
import (
"net/http"
"gitlab.com/mstarongitlab/linstrom/server/middlewares"
"gitlab.com/mstarongitlab/linstrom/storage"
)
type Server struct {
handler http.Handler
}
func NewServer(store *storage.Storage) *Server {
handler := http.NewServeMux()
handler.HandleFunc("/.well-known/webfinger", webfingerHandler)
// handler.Handle("/api/", http.StripPrefix("/ap", buildApiRouter()))
withMiddlewares := middlewares.ChainMiddlewares(
handler,
middlewares.InjectLogrusMiddleware,
middlewares.InjectStorageMiddlewareBuilder(store),
)
return &Server{
handler: withMiddlewares,
}
}