Try to reduce memory usage
This commit is contained in:
parent
c6ab26b63d
commit
6310ff356d
28
actor.go
28
actor.go
|
@ -59,17 +59,15 @@ func GetActor(name, iri, feed, pk string) (Actor, error) {
|
|||
}
|
||||
jsonFile := storage + slash + "actors" + slash + name + slash + name + ".json"
|
||||
fileHandle, err := os.Open(jsonFile)
|
||||
defer func() {
|
||||
_ = fileHandle.Close()
|
||||
}()
|
||||
if os.IsNotExist(err) {
|
||||
// File doesn't exist, maybe it's a new actor
|
||||
return actor, nil
|
||||
}
|
||||
byteValue, err := ioutil.ReadAll(fileHandle)
|
||||
if err != nil {
|
||||
// Ignore error, but return actor
|
||||
return actor, nil
|
||||
}
|
||||
savedActor := ActorToSave{}
|
||||
err = json.Unmarshal(byteValue, &savedActor)
|
||||
err = json.NewDecoder(fileHandle).Decode(&savedActor)
|
||||
if err != nil {
|
||||
// Ignore error, but return actor
|
||||
return actor, nil
|
||||
|
@ -91,11 +89,16 @@ func (a *Actor) save() error {
|
|||
Name: a.Name,
|
||||
Followers: a.followers,
|
||||
}
|
||||
actorJSON, err := json.MarshalIndent(actorToSave, "", "\t")
|
||||
f, err := os.OpenFile(storage+slash+"actors"+slash+a.Name+slash+a.Name+".json", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ioutil.WriteFile(storage+slash+"actors"+slash+a.Name+slash+a.Name+".json", actorJSON, 0644)
|
||||
defer func() {
|
||||
_ = f.Close()
|
||||
}()
|
||||
jsonEncoder := json.NewEncoder(f)
|
||||
jsonEncoder.SetIndent("", "\t")
|
||||
err = jsonEncoder.Encode(actorToSave)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -121,14 +124,9 @@ func (a *Actor) PostArticle(url string) error {
|
|||
if err != nil || !isSuccess(resp.StatusCode) {
|
||||
return errors.New("failed to fetch article")
|
||||
}
|
||||
respBody, err := ioutil.ReadAll(resp.Body)
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
err = json.NewDecoder(resp.Body).Decode(&article)
|
||||
if err != nil {
|
||||
return errors.New("failed to read response from fetched article")
|
||||
}
|
||||
err = json.Unmarshal(respBody, &article)
|
||||
if err != nil {
|
||||
return errors.New("failed to unmarshal fetched article")
|
||||
return errors.New("failed to decode fetched article")
|
||||
}
|
||||
create["object"] = article
|
||||
go a.sendToFollowers(create)
|
||||
|
|
8
feed.go
8
feed.go
|
@ -4,7 +4,6 @@ import (
|
|||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
|
@ -23,12 +22,7 @@ func allFeedItems(url string) ([]string, error) {
|
|||
if err != nil {
|
||||
return nil, errors.New("failed to get json feed")
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, errors.New("failed to read json feed")
|
||||
}
|
||||
err = json.Unmarshal(body, &jsonFeed)
|
||||
err = json.NewDecoder(resp.Body).Decode(&jsonFeed)
|
||||
if err != nil {
|
||||
return nil, errors.New("failed to parse json feed")
|
||||
}
|
||||
|
|
16
http.go
16
http.go
|
@ -4,7 +4,6 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gorilla/mux"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"math"
|
||||
"net/http"
|
||||
|
@ -62,17 +61,12 @@ func Serve() {
|
|||
link1["href"] = actor.iri
|
||||
links[0] = link1
|
||||
responseMap["links"] = links
|
||||
response, _ := json.Marshal(responseMap)
|
||||
_, _ = w.Write(response)
|
||||
_ = json.NewEncoder(w).Encode(responseMap)
|
||||
}
|
||||
|
||||
inboxHandler := func(w http.ResponseWriter, r *http.Request) {
|
||||
b, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
activity := make(map[string]interface{})
|
||||
err = json.Unmarshal(b, &activity)
|
||||
err := json.NewDecoder(r.Body).Decode(&activity)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
|
@ -129,7 +123,7 @@ func Serve() {
|
|||
}
|
||||
default:
|
||||
// Log inbox request
|
||||
logInbox(actor, b)
|
||||
logInbox(actor, &activity)
|
||||
}
|
||||
// Return 201
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
|
@ -145,13 +139,13 @@ func Serve() {
|
|||
log.Fatal(http.ListenAndServe(":8081", nil))
|
||||
}
|
||||
|
||||
func logInbox(actor *Actor, b []byte) {
|
||||
func logInbox(actor *Actor, activity *map[string]interface{}) {
|
||||
f, err := os.OpenFile(storage+slash+"actors"+slash+actor.Name+slash+"inbox", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer func() { _ = f.Close() }()
|
||||
_, _ = f.Write(b)
|
||||
_ = json.NewEncoder(f).Encode(activity)
|
||||
_, _ = f.WriteString("\n---\n")
|
||||
}
|
||||
|
||||
|
|
|
@ -4,7 +4,6 @@ import (
|
|||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
|
@ -47,12 +46,11 @@ func get(iri string) (info map[string]interface{}, err error) {
|
|||
if err != nil {
|
||||
return
|
||||
}
|
||||
responseData, _ := ioutil.ReadAll(resp.Body)
|
||||
if !isSuccess(resp.StatusCode) {
|
||||
return
|
||||
}
|
||||
var e interface{}
|
||||
err = json.Unmarshal(responseData, &e)
|
||||
err = json.NewDecoder(resp.Body).Decode(&e)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
Reference in New Issue