Use viper as Configuration manager. (#11)

* Use viper in CLI.

* Use viper in Server.

* Use viper in Worker.
This commit is contained in:
Naoki Kosaka
2018-12-23 14:52:43 +09:00
committed by GitHub
parent 93a00a13e2
commit e0c0f1a68d
13 changed files with 341 additions and 246 deletions

View File

@ -1,58 +0,0 @@
package activitypub
import (
"bytes"
"crypto/rsa"
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"
"net/http"
"os"
"time"
"github.com/Songmu/go-httpdate"
"github.com/yukimochi/httpsig"
)
// UaString : Use for User-Agent
var UaString = os.Getenv("RELAY_SERVICENAME") + " (golang net/http; Activity-Relay v0.2.0rc2; " + os.Getenv("RELAY_DOMAIN") + ")"
func appendSignature(request *http.Request, body *[]byte, KeyID string, publicKey *rsa.PrivateKey) error {
hash := sha256.New()
hash.Write(*body)
b := hash.Sum(nil)
request.Header.Set("Digest", "SHA-256="+base64.StdEncoding.EncodeToString(b))
request.Header.Set("Host", request.Host)
signer, _, err := httpsig.NewSigner([]httpsig.Algorithm{httpsig.RSA_SHA256}, []string{httpsig.RequestTarget, "Host", "Date", "Digest", "Content-Type"}, httpsig.Signature)
if err != nil {
return err
}
err = signer.SignRequest(publicKey, KeyID, request)
if err != nil {
return err
}
return nil
}
// SendActivity : Send ActivityPub activity
func SendActivity(inboxURL string, KeyID string, body []byte, publicKey *rsa.PrivateKey) error {
req, _ := http.NewRequest("POST", inboxURL, bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/activity+json, application/ld+json")
req.Header.Set("User-Agent", UaString)
req.Header.Set("Date", httpdate.Time2Str(time.Now()))
appendSignature(req, &body, KeyID, publicKey)
client := &http.Client{Timeout: time.Duration(5) * time.Second}
resp, err := client.Do(req)
if err != nil {
return err
}
fmt.Println(inboxURL, resp.StatusCode)
if resp.StatusCode/100 != 2 {
return errors.New(resp.Status)
}
return nil
}

View File

@ -52,7 +52,7 @@ func (actor *Actor) GenerateSelfKey(hostname *url.URL, publickey *rsa.PublicKey)
}
// RetrieveRemoteActor : Retrieve Actor from remote instance.
func (actor *Actor) RetrieveRemoteActor(url string, cache *cache.Cache) error {
func (actor *Actor) RetrieveRemoteActor(url string, uaString string, cache *cache.Cache) error {
var err error
cacheData, found := cache.Get(url)
if found {
@ -65,7 +65,7 @@ func (actor *Actor) RetrieveRemoteActor(url string, cache *cache.Cache) error {
}
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("Accept", "application/activity+json, application/ld+json")
req.Header.Set("User-Agent", UaString)
req.Header.Set("User-Agent", uaString)
client := new(http.Client)
resp, err := client.Do(req)
if err != nil {