fixes for deployment

This commit is contained in:
Rob Watson 2021-02-25 21:18:59 +01:00
parent e432b6c4d2
commit 4c8532fc7d
5 changed files with 31 additions and 15 deletions

View File

@ -3,7 +3,13 @@
## Usage ## Usage
``` ```
INSECURE_TLS=no BASE_URL=https://matrix.server.com LISTEN_ADDR=0.0.0.0:8888 ACCESS_TOKEN=hackme go run . INSECURE_TLS=no \
BASE_URL=https://matrix.server.com \
LISTEN_ADDR=0.0.0.0:8888 \
ACCESS_TOKEN=hackme \
VERBS_FILE=verbs.csv \
MATRIX_USER=@esbot:server.com \
go run .
``` ```
## Licence ## Licence

View File

@ -35,12 +35,12 @@ func (h *handler) handleTransactions(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("{}")) w.Write([]byte("{}"))
} }
func ListenAndServe(listenAddr, baseURL, accessToken string, insecureTLS bool, logger zerolog.Logger) error { func ListenAndServe(listenAddr, baseURL, user, accessToken string, insecureTLS bool, logger zerolog.Logger) error {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: insecureTLS} http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: insecureTLS}
h := handler{ h := handler{
logger: logger, logger: logger,
ep: matrix.NewProcessor(baseURL, accessToken, logger), ep: matrix.NewProcessor(baseURL, user, accessToken, logger),
} }
mux := http.NewServeMux() mux := http.NewServeMux()

View File

@ -46,11 +46,11 @@ func GetVerb(q string) *Verb {
return verbs[q] return verbs[q]
} }
func Load() (int, error) { func Load(verbsFile string) (int, error) {
var err error var err error
f, err := os.Open("./internal/lang/resources/jehle_verb_database.csv") f, err := os.Open(verbsFile)
if err != nil { if err != nil {
return 0, fmt.Errorf("could not open verb database: %v", err) return 0, fmt.Errorf("could not open verbs file: %v", err)
} }
csv := csv.NewReader(f) csv := csv.NewReader(f)

View File

@ -20,16 +20,16 @@ type EventProcessor interface {
} }
type processor struct { type processor struct {
baseURL string baseURL, user, accessToken string
accessToken string httpclient *http.Client
httpclient *http.Client processed map[string]bool
processed map[string]bool logger zerolog.Logger
logger zerolog.Logger
} }
func NewProcessor(baseURL, accessToken string, logger zerolog.Logger) EventProcessor { func NewProcessor(baseURL, user, accessToken string, logger zerolog.Logger) EventProcessor {
return &processor{ return &processor{
baseURL: baseURL, baseURL: baseURL,
user: user,
accessToken: accessToken, accessToken: accessToken,
httpclient: &http.Client{}, httpclient: &http.Client{},
processed: make(map[string]bool), processed: make(map[string]bool),
@ -45,7 +45,7 @@ func (p *processor) ProcessEvents(events []*RawEvent) error {
} }
if e.StateKey != "" { if e.StateKey != "" {
if e.Content.Membership == "invite" { if e.Type == "m.room.member" && e.Content.Membership == "invite" && e.StateKey == p.user {
if err := p.acceptInvite(e); err != nil { if err := p.acceptInvite(e); err != nil {
return err return err
} }

14
main.go
View File

@ -27,15 +27,25 @@ func main() {
logger.Fatal().Msg("missing BASE_URL") logger.Fatal().Msg("missing BASE_URL")
} }
user := os.Getenv("MATRIX_USER")
if user == "" {
logger.Fatal().Msg("missing MATRIX_USER")
}
verbsFile := os.Getenv("VERBS_FILE")
if verbsFile == "" {
logger.Fatal().Msg("missing VERBS_FILE")
}
insecureTLS := os.Getenv("INSECURE_TLS") == "yes" insecureTLS := os.Getenv("INSECURE_TLS") == "yes"
if n, err := lang.Load(); err == nil { if n, err := lang.Load(verbsFile); err == nil {
logger.Info().Msgf("Loaded %d verbs", n) logger.Info().Msgf("Loaded %d verbs", n)
} else { } else {
logger.Fatal().Msgf("could not initialize languages: %v", err) logger.Fatal().Msgf("could not initialize languages: %v", err)
} }
if err := http.ListenAndServe(listenAddr, baseURL, accessToken, insecureTLS, logger); err != nil { if err := http.ListenAndServe(listenAddr, baseURL, user, accessToken, insecureTLS, logger); err != nil {
panic(err) panic(err)
} }
} }