-
-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: refactor to use interfaces #35
Changes from 12 commits
662e3d5
9353f43
9d52b3f
5b2259a
64bee7b
9978aff
9005795
8f85d1d
2725152
81feda2
74d44fc
dd67be0
5eeaf9b
76daff0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,6 @@ | |
|
||
# Go workspace file | ||
go.work | ||
|
||
# jetbrains editor | ||
.idea |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,31 @@ | ||
package routes | ||
package http | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"sublinks/sublinks-federation/internal/activitypub" | ||
"sublinks/sublinks-federation/internal/lemmy" | ||
"sublinks/sublinks-federation/internal/log" | ||
|
||
"fmt" | ||
|
||
"golang.org/x/text/cases" | ||
"golang.org/x/text/language" | ||
|
||
"github.com/gorilla/mux" | ||
) | ||
|
||
func SetupActivityRoutes(r *mux.Router) { | ||
r.HandleFunc("/activities/{action}/{id}", getActivityHandler).Methods("GET") | ||
func (server *Server) SetupActivityRoutes() { | ||
server.Router.HandleFunc("/activities/{action}/{id}", server.getActivityHandler).Methods("GET") | ||
} | ||
|
||
func getActivityHandler(w http.ResponseWriter, r *http.Request) { | ||
func (server *Server) getActivityHandler(w http.ResponseWriter, r *http.Request) { | ||
vars := mux.Vars(r) | ||
var content []byte | ||
switch vars["action"] { | ||
case "create": | ||
obj, err := GetPostActivityObject(vars["id"]) | ||
obj, err := server.GetPostActivityObject(vars["id"]) | ||
if err != nil { | ||
log.Error("Error reading object", err) | ||
server.Logger.Error("Error reading object", err) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
|
@@ -44,7 +42,7 @@ func getActivityHandler(w http.ResponseWriter, r *http.Request) { | |
|
||
break | ||
default: | ||
error.Error(fmt.Errorf("action %s not found", vars["action"])) | ||
error.Error(fmt.Errorf("action %server not found", vars["action"])) | ||
w.WriteHeader(http.StatusNotFound) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm guessing you searched and replaced? This one should stay as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep yep! just changed back |
||
return | ||
} | ||
|
@@ -54,12 +52,12 @@ func getActivityHandler(w http.ResponseWriter, r *http.Request) { | |
w.Write(content) | ||
} | ||
|
||
func GetPostActivityObject(id string) (*activitypub.Post, error) { | ||
func (server *Server) GetPostActivityObject(id string) (*activitypub.Post, error) { | ||
ctx := context.Background() | ||
c := lemmy.GetLemmyClient(ctx) | ||
post, err := c.GetPost(ctx, id) | ||
if err != nil { | ||
log.Error("Error reading post", err) | ||
server.Logger.Error("Error reading post", err) | ||
return nil, err | ||
} | ||
return activitypub.ConvertPostToApub(post), nil | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package http | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
func (server *Server) SetupApubRoutes() { | ||
server.Router.HandleFunc("/users/{user}/inbox", server.getInboxHandler).Methods("GET") | ||
server.Router.HandleFunc("/users/{user}/inbox", server.postInboxHandler).Methods("POST") | ||
server.Router.HandleFunc("/users/{user}/outbox", server.getOutboxHandler).Methods("GET") | ||
server.Router.HandleFunc("/users/{user}/outbox", server.postOutboxHandler).Methods("POST") | ||
} | ||
|
||
func (server *Server) getInboxHandler(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
w.Header().Add("content-type", "application/activity+json") | ||
} | ||
|
||
func (server *Server) postInboxHandler(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
w.Header().Add("content-type", "application/activity+json") | ||
} | ||
|
||
func (server *Server) getOutboxHandler(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
w.Header().Add("content-type", "application/activity+json") | ||
} | ||
|
||
func (server *Server) postOutboxHandler(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
w.Header().Add("content-type", "application/activity+json") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package http | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
) | ||
|
||
type RequestError struct { | ||
Msg string `json:"message"` | ||
} | ||
|
||
func (server *Server) logMiddleware(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
server.Logger.Request("", r) | ||
next.ServeHTTP(w, r) | ||
}) | ||
} | ||
|
||
func (server *Server) notFound(w http.ResponseWriter, r *http.Request) { | ||
server.Logger.Request("404 Not Found", r) | ||
w.WriteHeader(http.StatusNotFound) | ||
w.Header().Add("content-type", "application/activity+json") | ||
content, _ := json.Marshal(RequestError{Msg: "not found"}) | ||
w.Write(content) | ||
} | ||
|
||
func (server *Server) notAllowedMethod(w http.ResponseWriter, r *http.Request) { | ||
server.Logger.Request("405 Method Not Allowed", r) | ||
w.WriteHeader(http.StatusNotFound) | ||
w.Header().Add("content-type", "application/activity+json") | ||
content, _ := json.Marshal(RequestError{Msg: "method not allowed"}) | ||
w.Write(content) | ||
} |
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should keep this one and remove the one on line 29. Better to call defer early