drop diskv in favor of stdlib

This commit is contained in:
Aaron Bieber 2020-10-21 00:51:53 +00:00
parent 41007c4ad2
commit e27cc689d4
4 changed files with 96 additions and 97 deletions

2
go.mod
View File

@ -5,9 +5,7 @@ go 1.13
require (
github.com/caneroj1/stemmer v0.0.0-20170128035808-c9f2ce1504d5
github.com/gomarkdown/markdown v0.0.0-20200127000047-1813ea067497
github.com/google/btree v1.0.0 // indirect
github.com/matrix-org/gomatrix v0.0.0-20200128155335-9e7906b6766d
github.com/peterbourgon/diskv v2.0.1+incompatible
golang.org/x/crypto v0.0.0-20200128174031-69ecbb4d6d5d
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3
suah.dev/protect v1.0.0

4
go.sum
View File

@ -2,12 +2,8 @@ github.com/caneroj1/stemmer v0.0.0-20170128035808-c9f2ce1504d5 h1:KrgIOxLMw9OvGi
github.com/caneroj1/stemmer v0.0.0-20170128035808-c9f2ce1504d5/go.mod h1:FX8SGAdUYnFYgGoy+xeGdnVIEq/ITKM7iMewnmng4Y4=
github.com/gomarkdown/markdown v0.0.0-20200127000047-1813ea067497 h1:wJkj+x9gPYlDyM34C6r3SXPs270coWeh85wu1CsusDo=
github.com/gomarkdown/markdown v0.0.0-20200127000047-1813ea067497/go.mod h1:aii0r/K0ZnHv7G0KF7xy1v0A7s2Ljrb5byB7MO5p6TU=
github.com/google/btree v1.0.0 h1:0udJVsspx3VBr5FwtLhQQtuAsVc79tTq0ocGIPAU6qo=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/matrix-org/gomatrix v0.0.0-20200128155335-9e7906b6766d h1:Vf/EQgAfg8/CBUQv9te7UJreZ9iKKouB2gb8UIRM4jQ=
github.com/matrix-org/gomatrix v0.0.0-20200128155335-9e7906b6766d/go.mod h1:3fxX6gUjWyI/2Bt7J1OLhpCzOfO/bB3AiX0cJtEKud0=
github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+vxiaj6gdUUzhl4XmI=
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
golang.org/dl v0.0.0-20190829154251-82a15e2f2ead/go.mod h1:IUMfjQLJQd4UTqG1Z90tenwKoCX93Gn3MAQJMOSBsDQ=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200128174031-69ecbb4d6d5d h1:9FCpayM9Egr1baVnV1SX0H87m+XB0B8S0hAMi99X/3U=

100
main.go
View File

@ -166,62 +166,64 @@ func main() {
}
})
go func() {
var htpass, _ = store.Get("got_htpass")
var got_room, _ = store.Get("got_room")
var got_port, _ = store.Get("got_listen")
var gotPort, _ = store.Get("got_listen")
if gotPort != "" {
go func() {
var htpass, _ = store.Get("got_htpass")
var gotRoom, _ = store.Get("got_room")
log.Printf("GOT: listening on %q and sending messages to %q\n", got_port, got_room)
log.Printf("GOT: listening on %q and sending messages to %q\n", gotPort, gotRoom)
http.HandleFunc("/_got", func(w http.ResponseWriter, r *http.Request) {
var msg string
user, pass, ok := r.BasicAuth()
err := bcrypt.CompareHashAndPassword([]byte(htpass), []byte(pass))
if !(ok && err == nil && user == "got") {
log.Printf("GOT: failed auth '%s'\n", user)
w.Header().Set("WWW-Authenticate", `Basic realm="got notify"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
err = r.ParseForm()
if err != nil {
http.Error(w, "invalid request", http.StatusBadRequest)
return
}
switch r.Method {
case http.MethodGet:
msg = r.Form.Get("message")
case http.MethodPost:
msg = r.Form.Get("file")
default:
http.Error(w, fmt.Sprintf("method %q not implemented", r.Method), http.StatusMethodNotAllowed)
return
}
msg = strings.TrimSuffix(msg, "\n")
if msg == "" {
fmt.Fprintf(w, "empty message")
return
}
for _, line := range strings.Split(msg, "\n") {
log.Printf("GOT: sending '%s'\n", line)
err = plugins.SendNotice(cli, got_room, line)
if err != nil {
http.Error(w, fmt.Sprintf("can not send commit info: %s", err), http.StatusInternalServerError)
http.HandleFunc("/_got", func(w http.ResponseWriter, r *http.Request) {
var msg string
user, pass, ok := r.BasicAuth()
err := bcrypt.CompareHashAndPassword([]byte(htpass), []byte(pass))
if !(ok && err == nil && user == "got") {
log.Printf("GOT: failed auth '%s'\n", user)
w.Header().Set("WWW-Authenticate", `Basic realm="got notify"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
}
fmt.Fprintf(w, "ok")
err = r.ParseForm()
if err != nil {
http.Error(w, "invalid request", http.StatusBadRequest)
return
}
})
switch r.Method {
case http.MethodGet:
msg = r.Form.Get("message")
case http.MethodPost:
msg = r.Form.Get("file")
default:
http.Error(w, fmt.Sprintf("method %q not implemented", r.Method), http.StatusMethodNotAllowed)
return
}
log.Fatal(http.ListenAndServe(got_port, nil))
}()
msg = strings.TrimSuffix(msg, "\n")
if msg == "" {
fmt.Fprintf(w, "empty message")
return
}
for _, line := range strings.Split(msg, "\n") {
log.Printf("GOT: sending '%s'\n", line)
err = plugins.SendNotice(cli, gotRoom, line)
if err != nil {
http.Error(w, fmt.Sprintf("can not send commit info: %s", err), http.StatusInternalServerError)
return
}
}
fmt.Fprintf(w, "ok")
})
log.Fatal(http.ListenAndServe(gotPort, nil))
}()
}
go func() {
for {

View File

@ -4,44 +4,32 @@ import (
"bytes"
"encoding/gob"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"github.com/matrix-org/gomatrix"
"github.com/peterbourgon/diskv"
)
// MCStore implements a gomatrix.Storer and exposes a diskv db to be used for
// application storage (account info, config info etc).
type MCStore struct {
db *diskv.Diskv
// FStore is the path to a directory which will contain our data.
type FStore string
// NewStore creates a new instance of FStore
func NewStore(s string) (*FStore, error) {
fi, err := os.Lstat(s)
if err != nil {
return nil, err
}
if !fi.IsDir() {
return nil, fmt.Errorf("not a directory")
}
fstore := FStore(s)
return &fstore, nil
}
// NewStore creates a new MCStore instance.
func NewStore(path string) (*MCStore, error) {
flatTransform := func(s string) []string { return []string{} }
db := diskv.New(diskv.Options{
BasePath: path,
Transform: flatTransform,
CacheSizeMax: 1024 * 1024,
})
s := &MCStore{db: db}
return s, nil
}
// Set takes a key value pair and shoves it in a db.
func (s *MCStore) Set(key string, value string) {
v := []byte(value)
_ = s.db.Write(key, v)
}
// Get retrives a value from the db
func (s *MCStore) Get(key string) (string, error) {
b, err := s.db.Read(key)
return string(b), err
}
func (s *MCStore) encodeRoom(room *gomatrix.Room) ([]byte, error) {
func (s *FStore) encodeRoom(room *gomatrix.Room) ([]byte, error) {
buf := new(bytes.Buffer)
enc := gob.NewEncoder(buf)
err := enc.Encode(room)
@ -51,7 +39,7 @@ func (s *MCStore) encodeRoom(room *gomatrix.Room) ([]byte, error) {
return buf.Bytes(), nil
}
func (s *MCStore) decodeRoom(room []byte) (*gomatrix.Room, error) {
func (s *FStore) decodeRoom(room []byte) (*gomatrix.Room, error) {
var r *gomatrix.Room
buf := bytes.NewBuffer(room)
dec := gob.NewDecoder(buf)
@ -62,37 +50,52 @@ func (s *MCStore) decodeRoom(room []byte) (*gomatrix.Room, error) {
return r, nil
}
// SaveFilterID exposed for gomatrix
func (s *MCStore) SaveFilterID(userID, filterID string) {
s.Set(fmt.Sprintf("filter_%s", userID), filterID)
// Set dumps value into a file named key
func (s FStore) Set(key string, value string) {
err := ioutil.WriteFile(path.Join(string(s), key), []byte(value), 0600)
if err != nil {
log.Println(err)
}
}
// Get pulls value from a file named key
func (s FStore) Get(key string) (string, error) {
data, err := ioutil.ReadFile(path.Join(string(s), key))
if err != nil {
return "", nil
}
return string(data), nil
}
// SaveFilterID exposed for gomatrix
func (s *FStore) SaveFilterID(userID, filterID string) {
s.Set(fmt.Sprintf("filter_%s", userID), filterID)
}
// LoadFilterID exposed for gomatrix
func (s *MCStore) LoadFilterID(userID string) string {
func (s *FStore) LoadFilterID(userID string) string {
filter, _ := s.Get(fmt.Sprintf("filter_%s", userID))
return filter
}
// SaveNextBatch exposed for gomatrix
func (s *MCStore) SaveNextBatch(userID, nextBatchToken string) {
func (s *FStore) SaveNextBatch(userID, nextBatchToken string) {
s.Set(fmt.Sprintf("batch_%s", userID), nextBatchToken)
}
// LoadNextBatch exposed for gomatrix
func (s *MCStore) LoadNextBatch(userID string) string {
func (s *FStore) LoadNextBatch(userID string) string {
batch, _ := s.Get(fmt.Sprintf("batch_%s", userID))
return batch
}
// SaveRoom exposed for gomatrix
func (s *MCStore) SaveRoom(room *gomatrix.Room) {
func (s *FStore) SaveRoom(room *gomatrix.Room) {
b, _ := s.encodeRoom(room)
s.Set(fmt.Sprintf("room_%s", room.ID), string(b))
}
// LoadRoom exposed for gomatrix
func (s *MCStore) LoadRoom(roomID string) *gomatrix.Room {
func (s *FStore) LoadRoom(roomID string) *gomatrix.Room {
b, _ := s.Get(fmt.Sprintf("room_%s", roomID))
room, _ := s.decodeRoom([]byte(b))
return room