2024-03-25 11:55:01 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-03-30 16:49:51 +02:00
|
|
|
"flag"
|
2024-03-25 11:55:01 +02:00
|
|
|
"fmt"
|
|
|
|
"github.com/joho/godotenv"
|
2024-03-30 16:49:51 +02:00
|
|
|
"io"
|
2024-03-25 11:55:01 +02:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2024-03-30 16:49:51 +02:00
|
|
|
"net/http/httptest"
|
2024-03-25 11:55:01 +02:00
|
|
|
"os"
|
2024-03-30 16:49:51 +02:00
|
|
|
"strings"
|
2024-03-25 11:55:01 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// https://api.monobank.ua/docs/index.html#tag/Kliyentski-personalni-dani/paths/~1personal~1statement~1{account}~1{from}~1{to}/get
|
|
|
|
// https://api-docs.firefly-iii.org/#/accounts/listAccount
|
|
|
|
|
2024-03-26 19:06:31 +02:00
|
|
|
// curl -X POST https://api.monobank.ua/personal/webhook -H 'Content-TransactionType: application/json' -H 'X-Token: ' -d '{"webHookUrl":"https://monobank-firefly3.stuzer.link/webhook"}'
|
2024-03-25 11:55:01 +02:00
|
|
|
|
2024-03-26 19:06:31 +02:00
|
|
|
// curl -X POST https://monobank-firefly3.stuzer.link/webhook -H 'Content-TransactionType: application/json' -d '{"test":123}'
|
2024-03-25 11:55:01 +02:00
|
|
|
|
|
|
|
func main() {
|
2024-03-27 20:12:24 +02:00
|
|
|
// load .env
|
2024-03-25 11:55:01 +02:00
|
|
|
err := godotenv.Load(".env")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error loading .env file")
|
|
|
|
}
|
|
|
|
|
2024-03-27 21:22:51 +02:00
|
|
|
// test config read
|
|
|
|
_, err = ReadConfig(os.Getenv("CONFIG_PATH"))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("cannot read config - " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-30 16:49:51 +02:00
|
|
|
// flags
|
|
|
|
flagDoTransaction := flag.String("monobank-transaction", "", "run monobank transaction JSON manually")
|
2024-03-26 19:06:31 +02:00
|
|
|
|
2024-03-30 16:49:51 +02:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
// manual transaction
|
|
|
|
if len(*flagDoTransaction) > 0 {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
r := &http.Request{
|
|
|
|
Method: http.MethodPost,
|
|
|
|
Header: make(http.Header),
|
|
|
|
Body: io.NopCloser(strings.NewReader(*flagDoTransaction)),
|
|
|
|
}
|
|
|
|
r.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
handleWebhook(w, r)
|
|
|
|
|
|
|
|
// @todo error logging
|
|
|
|
//response := w.Result()
|
|
|
|
//if response.StatusCode != http.StatusOK {
|
|
|
|
// os.Exit(1)
|
|
|
|
//}
|
|
|
|
} else {
|
|
|
|
// set webhook
|
|
|
|
http.HandleFunc("/webhook", handleWebhook)
|
|
|
|
|
|
|
|
// listen server
|
|
|
|
fmt.Println("Webhook server listening on " + os.Getenv("LISTEN"))
|
|
|
|
err = http.ListenAndServe(os.Getenv("LISTEN"), nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
2024-03-26 19:06:31 +02:00
|
|
|
}
|
2024-03-25 11:55:01 +02:00
|
|
|
}
|