-
-
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 7 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 |
---|---|---|
|
@@ -11,17 +11,24 @@ import ( | |
) | ||
|
||
func main() { | ||
// bootstrap logger | ||
logger := log.NewLogger() | ||
|
||
// Load connection string from .env file | ||
err := godotenv.Load() | ||
if err != nil { | ||
log.Warn(fmt.Sprintf("failed to load env, %v", err)) | ||
logger.Warn(fmt.Sprintf("failed to load env, %v", err)) | ||
} | ||
|
||
conn, err := db.Connect() | ||
defer conn.Close() | ||
if err != nil { | ||
log.Fatal("failed connecting to db", err) | ||
logger.Fatal("failed connecting to db", err) | ||
} | ||
defer conn.Close() | ||
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. We should keep this one and remove the one on line 29. Better to call defer early |
||
db.RunMigrations(conn) | ||
http.RunServer() | ||
|
||
s := http.NewServer(logger) | ||
s.RunServer() | ||
|
||
os.Exit(0) | ||
} |
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 (s Server) SetupActivityRoutes() { | ||
s.Router.HandleFunc("/activities/{action}/{id}", s.getActivityHandler).Methods("GET") | ||
} | ||
|
||
func getActivityHandler(w http.ResponseWriter, r *http.Request) { | ||
func (s Server) getActivityHandler(w http.ResponseWriter, r *http.Request) { | ||
vars := mux.Vars(r) | ||
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. Maybe we can compromise and use 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. Oh yes absolutely. I know go devs tend to use single character vars but I much prefer verbosity. I'll change all the single char vars to "server" |
||
var content []byte | ||
switch vars["action"] { | ||
case "create": | ||
obj, err := GetPostActivityObject(vars["id"]) | ||
obj, err := s.GetPostActivityObject(vars["id"]) | ||
if err != nil { | ||
log.Error("Error reading object", err) | ||
s.Logger.Error("Error reading object", err) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
|
@@ -54,12 +52,12 @@ func getActivityHandler(w http.ResponseWriter, r *http.Request) { | |
w.Write(content) | ||
} | ||
|
||
func GetPostActivityObject(id string) (*activitypub.Post, error) { | ||
func (s 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) | ||
s.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 (s Server) SetupApubRoutes() { | ||
s.Router.HandleFunc("/users/{user}/inbox", s.getInboxHandler).Methods("GET") | ||
s.Router.HandleFunc("/users/{user}/inbox", s.postInboxHandler).Methods("POST") | ||
s.Router.HandleFunc("/users/{user}/outbox", s.getOutboxHandler).Methods("GET") | ||
s.Router.HandleFunc("/users/{user}/outbox", s.postOutboxHandler).Methods("POST") | ||
} | ||
|
||
func (s Server) getInboxHandler(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
w.Header().Add("content-type", "application/activity+json") | ||
} | ||
|
||
func (s Server) postInboxHandler(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
w.Header().Add("content-type", "application/activity+json") | ||
} | ||
|
||
func (s Server) getOutboxHandler(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
w.Header().Add("content-type", "application/activity+json") | ||
} | ||
|
||
func (s 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 (s Server) logMiddleware(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
s.Logger.Request("", r) | ||
next.ServeHTTP(w, r) | ||
}) | ||
} | ||
|
||
func (s Server) notFound(w http.ResponseWriter, r *http.Request) { | ||
s.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 (s Server) notAllowedMethod(w http.ResponseWriter, r *http.Request) { | ||
s.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.
Hmm... I think the other line I commented on is better no? Shouldn't we check whether an error occurred before trying to defer closing? (not sure if the Fatal logger will still call deferred functions and cause more errors?)