almost posts now

This commit is contained in:
Jeffrey Paul 2020-09-22 07:27:14 -07:00
parent ceb6d16dcf
commit e2ab505b47

View File

@ -9,12 +9,15 @@ import (
"time" "time"
"git.eeqj.de/sneak/goutil" "git.eeqj.de/sneak/goutil"
"git.eeqj.de/sneak/mothership/apitypes"
// db driver: // db driver:
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
const jsonContentType = "application/json; charset=utf-8"
func (hp *HistoryPoster) postURLs(ctx context.Context, cancel context.CancelFunc) { func (hp *HistoryPoster) postURLs(ctx context.Context, cancel context.CancelFunc) {
log.Info().Msg("finding history files") log.Info().Msg("finding history files")
files, err := findHistoryFiles() files, err := findHistoryFiles()
@ -57,8 +60,48 @@ func (hp *HistoryPoster) processURLFromHistory(hi historyItem) {
} }
func (hp *HistoryPoster) postURL(hi historyItem) error { func (hp *HistoryPoster) postURL(hi historyItem) error {
// FIXME
// panic("unimplemented") req := &apitypes.MothershipHistoryRequest{
PSK: viper.GetString("PSK"),
Visit: &apitypes.MothershipHistoryItem{
LastVisitTime: hi.LastVisitTime.Format(time.RFC3339Nano),
URL: hi.URL,
}
}
url := viper.GetString("APIURL")
reqBody := new(bytes.Buffer)
json.NewEncoder(reqBody).Encode(req)
res, err := http.Post(url, jsonContentType, reqBody)
if err != nil {
log.Error().Err(err).Msg("unable to POST url to mothership")
return err
}
if res.StatusCode != http.StatusOK {
log.Error().
Int("statuscode", res.StatusCode).
Msg("unable to POST url to mothership")
return err
}
var apiresp apitypes.MothershipHistoryResponse
err = json.NewDecoder(res.Body).Decode(&apiresp)
if err != nil {
log.Error().Err(err).Msg("unable to decode mothership response")
return err
}
if apiresp.Result != "ok" {
log.Error().Msg("mothership response non-ok")
return errors.New("mothership response non-ok")
}
log.Info().Str("url",hi.URL).Msg("url sent to mothership")
return nil return nil
} }