From 49e134573e72bacbab2cf3456d8fbb6579cb74ad Mon Sep 17 00:00:00 2001 From: linyows Date: Sun, 8 Oct 2023 17:21:55 +0900 Subject: [PATCH] move plugins to hook_* --- hook.go | 26 ++++ hook_file.go | 69 +++++++++++ .../file/main_test.go => hook_file_test.go | 45 +++---- hook_mysql.go | 82 +++++++++++++ .../mysql/main_test.go => hook_mysql_test.go | 45 +++---- hook_sqlite.go | 113 ++++++++++++++++++ .../main_test.go => hook_sqlite_test.go | 62 +++++----- {plugins/mysql => misc}/setup.sql | 0 plugins.go | 22 ---- plugins/file/main.go | 70 ----------- plugins/mysql/main.go | 84 ------------- plugins/sqlite/main.go | 111 ----------------- 12 files changed, 366 insertions(+), 363 deletions(-) create mode 100644 hook.go create mode 100644 hook_file.go rename plugins/file/main_test.go => hook_file_test.go (83%) create mode 100644 hook_mysql.go rename plugins/mysql/main_test.go => hook_mysql_test.go (77%) create mode 100644 hook_sqlite.go rename plugins/sqlite/main_test.go => hook_sqlite_test.go (75%) rename {plugins/mysql => misc}/setup.sql (100%) delete mode 100644 plugins/file/main.go delete mode 100644 plugins/mysql/main.go delete mode 100644 plugins/sqlite/main.go diff --git a/hook.go b/hook.go new file mode 100644 index 0000000..dafb0f3 --- /dev/null +++ b/hook.go @@ -0,0 +1,26 @@ +package warp + +import ( + "time" +) + +type Hook interface { + AfterInit() + AfterComm(*AfterCommData) + AfterConn(*AfterConnData) +} + +type AfterCommData struct { + ConnID string + OccurredAt time.Time + Data + Direction +} + +type AfterConnData struct { + ConnID string + OccurredAt time.Time + MailFrom []byte + MailTo []byte + Elapse +} diff --git a/hook_file.go b/hook_file.go new file mode 100644 index 0000000..fb300b3 --- /dev/null +++ b/hook_file.go @@ -0,0 +1,69 @@ +package warp + +import ( + "fmt" + "io" + "os" + "time" +) + +const ( + fileCommJson string = `{"type":"comm","occurred_at":"%s","connection_id":"%s","direction":"%s","data":"%s"} +` + fileConnJson string = `{"type":"conn","occurred_at":"%s","connection_id":"%s","from":"%s","to":"%s","elapse":"%s"} +` +) + +type HookFile struct { + file io.Writer +} + +func (h *HookFile) prefix() string { + return "file" +} + +func (h *HookFile) writer() (io.Writer, error) { + if h.file != nil { + return h.file, nil + } + + path := os.Getenv("FILE_PATH") + if len(path) == 0 { + return nil, fmt.Errorf("missing path for file, please set `FILE_PATH`") + } + + var err error + h.file, err = os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + return nil, fmt.Errorf("os.OpenFile error: %s\n", err) + } + + return h.file, nil +} + +func (h *HookFile) AfterInit() { +} + +func (h *HookFile) AfterComm(d *AfterCommData) { + writer, err := h.writer() + if err != nil { + fmt.Printf("[%s] %s\n", h.prefix(), err) + return + } + + if _, err := fmt.Fprintf(writer, fileCommJson, d.OccurredAt.Format(time.RFC3339), d.ConnID, d.Direction, d.Data); err != nil { + fmt.Printf("[%s] file append error: %s\n", h.prefix(), err) + } +} + +func (h *HookFile) AfterConn(d *AfterConnData) { + writer, err := h.writer() + if err != nil { + fmt.Printf("[%s] %s\n", h.prefix(), err) + return + } + + if _, err := fmt.Fprintf(writer, fileConnJson, d.OccurredAt.Format(time.RFC3339), d.ConnID, d.MailFrom, d.MailTo, d.Elapse); err != nil { + fmt.Printf("[%s] file append error: %s\n", h.prefix(), err) + } +} diff --git a/plugins/file/main_test.go b/hook_file_test.go similarity index 83% rename from plugins/file/main_test.go rename to hook_file_test.go index 8994058..31d7532 100644 --- a/plugins/file/main_test.go +++ b/hook_file_test.go @@ -1,4 +1,4 @@ -package main +package warp import ( "bytes" @@ -7,20 +7,12 @@ import ( "strings" "testing" "time" - - "github.com/linyows/warp" ) -func TestConst(t *testing.T) { +func TestHookFileConst(t *testing.T) { var expect string var got string - expect = "file-plugin" - got = prefix - if got != expect { - t.Errorf("expected %s, got %s", expect, got) - } - replace := func(str string) string { return strings.ReplaceAll( strings.ReplaceAll(str, "\n", ""), @@ -36,7 +28,7 @@ func TestConst(t *testing.T) { "data":"%s" } `) - got = commJson + got = fileCommJson if got != expect { t.Errorf("expected %s, got %s", expect, got) } @@ -51,13 +43,22 @@ func TestConst(t *testing.T) { "elapse":"%s" } `) - got = connJson + got = fileConnJson + if got != expect { + t.Errorf("expected %s, got %s", expect, got) + } +} + +func TestHookFilePrefix(t *testing.T) { + f := &HookFile{} + expect := "file" + got := f.prefix() if got != expect { t.Errorf("expected %s, got %s", expect, got) } } -func TestWriter(t *testing.T) { +func TestHookFileWriter(t *testing.T) { var tests = []struct { expectFileName string expectError string @@ -71,10 +72,10 @@ func TestWriter(t *testing.T) { envVal: "", }, { - expectFileName: "/tmp/warp-plugin-file", + expectFileName: "/tmp/warp-file", expectError: "", envName: "FILE_PATH", - envVal: "/tmp/warp-plugin-file", + envVal: "/tmp/warp-file", }, } @@ -84,7 +85,7 @@ func TestWriter(t *testing.T) { defer os.Unsetenv(v.envName) } - f := File{} + f := &HookFile{} w, err := f.writer() if w != nil || v.expectFileName != "" { @@ -99,13 +100,13 @@ func TestWriter(t *testing.T) { } } -func TestAfterComm(t *testing.T) { +func TestHookFileAfterComm(t *testing.T) { ti := time.Date(2023, time.August, 16, 14, 48, 0, 0, time.UTC) buffer := new(bytes.Buffer) - f := File{ + f := &HookFile{ file: buffer, } - data := &warp.AfterCommData{ + data := &AfterCommData{ ConnID: "abcdefg", OccurredAt: ti, Data: []byte("hello"), @@ -120,13 +121,13 @@ func TestAfterComm(t *testing.T) { } } -func TestAfterConn(t *testing.T) { +func TestHookFileAfterConn(t *testing.T) { ti := time.Date(2023, time.August, 16, 14, 48, 0, 0, time.UTC) buffer := new(bytes.Buffer) - f := File{ + f := &HookFile{ file: buffer, } - data := &warp.AfterConnData{ + data := &AfterConnData{ ConnID: "abcdefg", OccurredAt: ti, MailFrom: []byte("alice@example.local"), diff --git a/hook_mysql.go b/hook_mysql.go new file mode 100644 index 0000000..038a5a5 --- /dev/null +++ b/hook_mysql.go @@ -0,0 +1,82 @@ +package warp + +import ( + "database/sql" + "fmt" + "os" +) + +const ( + mysqlCommQuery string = "insert into communications (id, connection_id, occurred_at, direction, data) values (?, ?, ?, ?, ?)" + mysqlConnQuery string = "insert into connections (id, occurred_at, mail_from, mail_to, elapse) values (?, ?, ?, ?, ?)" +) + +type HookMysql struct { + pool *sql.DB // Database connection pool. +} + +func (h *HookMysql) prefix() string { + return "mysql" +} + +func (h *HookMysql) conn() (*sql.DB, error) { + if h.pool != nil { + return h.pool, nil + } + + dsn := os.Getenv("DSN") + if len(dsn) == 0 { + return nil, fmt.Errorf("missing dsn for mysql, please set `DSN`") + } + + var err error + h.pool, err = sql.Open("mysql", dsn) + if err != nil { + return nil, fmt.Errorf("sql.Open error: s%s\n", err) + } + + return h.pool, nil +} + +func (h *HookMysql) AfterInit() { +} + +func (h *HookMysql) AfterComm(d *AfterCommData) { + conn, err := h.conn() + if err != nil { + fmt.Printf("[%s] %s\n", h.prefix(), err) + return + } + + _, err = conn.Exec( + mysqlCommQuery, + GenID().String(), + d.ConnID, + d.OccurredAt.Format(TimeFormat), + d.Direction, + d.Data, + ) + if err != nil { + fmt.Printf("[%s] db exec error: %s\n", h.prefix(), err) + } +} + +func (h *HookMysql) AfterConn(d *AfterConnData) { + conn, err := h.conn() + if err != nil { + fmt.Printf("[%s] %s\n", h.prefix(), err) + return + } + + _, err = conn.Exec( + mysqlConnQuery, + d.ConnID, + d.OccurredAt.Format(TimeFormat), + d.MailFrom, + d.MailTo, + d.Elapse, + ) + if err != nil { + fmt.Printf("[%s] db exec error: %s\n", h.prefix(), err) + } +} diff --git a/plugins/mysql/main_test.go b/hook_mysql_test.go similarity index 77% rename from plugins/mysql/main_test.go rename to hook_mysql_test.go index 2721a65..1c9039e 100644 --- a/plugins/mysql/main_test.go +++ b/hook_mysql_test.go @@ -1,4 +1,4 @@ -package main +package warp import ( "database/sql/driver" @@ -8,36 +8,41 @@ import ( "github.com/DATA-DOG/go-sqlmock" _ "github.com/go-sql-driver/mysql" - "github.com/linyows/warp" ) -func TestConst(t *testing.T) { +func TestHookMysqlConst(t *testing.T) { var expect string var got string - expect = "mysql-plugin" - got = prefix + expect = "insert into communications (id, connection_id, occurred_at, direction, data) values (?, ?, ?, ?, ?)" + got = mysqlCommQuery if got != expect { t.Errorf("expected %s, got %s", expect, got) } - expect = "insert into communications (id, connection_id, occurred_at, direction, data) values (?, ?, ?, ?, ?)" - got = commQuery + expect = "insert into connections (id, occurred_at, mail_from, mail_to, elapse) values (?, ?, ?, ?, ?)" + got = mysqlConnQuery if got != expect { t.Errorf("expected %s, got %s", expect, got) } +} - expect = "insert into connections (id, occurred_at, mail_from, mail_to, elapse) values (?, ?, ?, ?, ?)" - got = connQuery +func TestHookMysqlPrefix(t *testing.T) { + mysql := &HookMysql{} + var expect string + var got string + + expect = "mysql" + got = mysql.prefix() if got != expect { t.Errorf("expected %s, got %s", expect, got) } } -func TestWriter(t *testing.T) { +func TestHookMysqlConn(t *testing.T) { expectError := "missing dsn for mysql, please set `DSN`" - mysql := Mysql{} - _, err := mysql.Conn() + mysql := &HookMysql{} + _, err := mysql.conn() if err != nil && fmt.Sprintf("%s", err) != expectError { t.Errorf("expected %s, got %s", expectError, err) @@ -51,7 +56,7 @@ func (a AnyID) Match(v driver.Value) bool { return ok } -func TestAfterComm(t *testing.T) { +func TestHookMysqlAfterComm(t *testing.T) { db, mock, err := sqlmock.New() if err != nil { t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) @@ -63,23 +68,23 @@ func TestAfterComm(t *testing.T) { mock.ExpectExec("insert into communications").WithArgs( AnyID{}, "abcdefg", - ti.Format(warp.TimeFormat), + ti.Format(TimeFormat), "--", []byte("hello"), ).WillReturnResult(sqlmock.NewResult(1, 1)) - data := &warp.AfterCommData{ + data := &AfterCommData{ ConnID: "abcdefg", OccurredAt: ti, Data: []byte("hello"), Direction: "--", } - mysql := Mysql{pool: db} + mysql := &HookMysql{pool: db} mysql.AfterComm(data) } -func TestAfterConn(t *testing.T) { +func TestHookMysqlAfterConn(t *testing.T) { db, mock, err := sqlmock.New() if err != nil { t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) @@ -90,13 +95,13 @@ func TestAfterConn(t *testing.T) { mock.ExpectExec("insert into connections").WithArgs( "abcdefg", - ti.Format(warp.TimeFormat), + ti.Format(TimeFormat), []byte("alice@example.local"), []byte("bob@example.test"), 20, ).WillReturnResult(sqlmock.NewResult(1, 1)) - data := &warp.AfterConnData{ + data := &AfterConnData{ ConnID: "abcdefg", OccurredAt: ti, MailFrom: []byte("alice@example.local"), @@ -104,6 +109,6 @@ func TestAfterConn(t *testing.T) { Elapse: 20, } - mysql := Mysql{pool: db} + mysql := &HookMysql{pool: db} mysql.AfterConn(data) } diff --git a/hook_sqlite.go b/hook_sqlite.go new file mode 100644 index 0000000..747fb18 --- /dev/null +++ b/hook_sqlite.go @@ -0,0 +1,113 @@ +package warp + +import ( + "database/sql" + "fmt" + "os" +) + +const ( + sqliteCommQuery string = "insert into communications (id, connection_id, occurred_at, direction, data) values ($1, $2, $3, $4, $5)" + sqliteConnQuery string = "insert into connections (id, occurred_at, mail_from, mail_to, elapse) values ($1, $2, $3, $4, $5)" + sqliteConnCreateTable string = ` + create table if not exists connections ( + id text primary key, + mail_from text, + mail_to text, + occurred_at datetime default CURRENT_TIMESTAMP, + elapse integer + )` + sqliteCommCreateTable string = ` + create table if not exists communications ( + id text primary key, + connection_id text, + direction text, + data text, + occurred_at datetime default CURRENT_TIMESTAMP + )` +) + +type HookSqlite struct { + pool *sql.DB // Database connection pool. +} + +func (h *HookSqlite) prefix() string { + return "sqlite" +} + +func (h *HookSqlite) conn() (*sql.DB, error) { + if h.pool != nil { + return h.pool, nil + } + + dsn := os.Getenv("DSN") + if len(dsn) == 0 { + return nil, fmt.Errorf("missing dsn for sqlite, please set `DSN`") + } + + var err error + h.pool, err = sql.Open("sqlite3", dsn) + if err != nil { + return nil, fmt.Errorf("sql.Open error: %s(%#v)\n", err.Error(), err) + } + + return h.pool, nil +} + +func (h *HookSqlite) AfterInit() { + conn, err := h.conn() + if err != nil { + fmt.Printf("[%s] %s\n", h.prefix(), err) + return + } + + _, err = conn.Exec(sqliteConnCreateTable) + if err != nil { + fmt.Printf("[%s] db exec error: %s\n", h.prefix(), err) + } + + _, err = conn.Exec(sqliteCommCreateTable) + if err != nil { + fmt.Printf("[%s] db exec error: %s\n", h.prefix(), err) + } +} + +func (h *HookSqlite) AfterComm(d *AfterCommData) { + conn, err := h.conn() + if err != nil { + fmt.Printf("[%s] %s\n", h.prefix(), err) + return + } + + _, err = conn.Exec( + sqliteCommQuery, + GenID().String(), + d.ConnID, + d.OccurredAt.Format(TimeFormat), + d.Direction, + d.Data, + ) + if err != nil { + fmt.Printf("[%s] db exec error: %s\n", h.prefix(), err) + } +} + +func (h *HookSqlite) AfterConn(d *AfterConnData) { + conn, err := h.conn() + if err != nil { + fmt.Printf("[%s] %s\n", h.prefix(), err) + return + } + + _, err = conn.Exec( + sqliteConnQuery, + d.ConnID, + d.OccurredAt.Format(TimeFormat), + d.MailFrom, + d.MailTo, + d.Elapse, + ) + if err != nil { + fmt.Printf("[%s] db exec error: %s\n", h.prefix(), err) + } +} diff --git a/plugins/sqlite/main_test.go b/hook_sqlite_test.go similarity index 75% rename from plugins/sqlite/main_test.go rename to hook_sqlite_test.go index a559fbd..cc7f4f3 100644 --- a/plugins/sqlite/main_test.go +++ b/hook_sqlite_test.go @@ -1,58 +1,52 @@ -package main +package warp import ( - "database/sql/driver" "fmt" "os" "testing" "time" "github.com/DATA-DOG/go-sqlmock" - "github.com/linyows/warp" _ "github.com/mattn/go-sqlite3" ) -func TestConst(t *testing.T) { +func TestHookSqliteConst(t *testing.T) { var expect string var got string - expect = "sqlite-plugin" - got = prefix + expect = "insert into communications (id, connection_id, occurred_at, direction, data) values ($1, $2, $3, $4, $5)" + got = sqliteCommQuery if got != expect { t.Errorf("expected %s, got %s", expect, got) } - expect = "insert into communications (id, connection_id, occurred_at, direction, data) values ($1, $2, $3, $4, $5)" - got = commQuery + expect = "insert into connections (id, occurred_at, mail_from, mail_to, elapse) values ($1, $2, $3, $4, $5)" + got = sqliteConnQuery if got != expect { t.Errorf("expected %s, got %s", expect, got) } +} - expect = "insert into connections (id, occurred_at, mail_from, mail_to, elapse) values ($1, $2, $3, $4, $5)" - got = connQuery +func TestHookSqlitePrefix(t *testing.T) { + sqlite := &HookSqlite{} + expect := "sqlite" + got := sqlite.prefix() if got != expect { t.Errorf("expected %s, got %s", expect, got) } } -func TestWriter(t *testing.T) { +func TestHookSqliteConn(t *testing.T) { expectError := "missing dsn for sqlite, please set `DSN`" - sqlite := Sqlite{} - _, err := sqlite.Conn() + sqlite := &HookSqlite{} + _, err := sqlite.conn() if err != nil && fmt.Sprintf("%s", err) != expectError { t.Errorf("expected %s, got %s", expectError, err) } } -type AnyID struct{} - -func (a AnyID) Match(v driver.Value) bool { - _, ok := v.(string) - return ok -} - -func TestAfterComm(t *testing.T) { +func TestHookSqliteAfterComm(t *testing.T) { db, mock, err := sqlmock.New() if err != nil { t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) @@ -64,23 +58,23 @@ func TestAfterComm(t *testing.T) { mock.ExpectExec("insert into communications").WithArgs( AnyID{}, "abcdefg", - ti.Format(warp.TimeFormat), + ti.Format(TimeFormat), "--", []byte("hello"), ).WillReturnResult(sqlmock.NewResult(1, 1)) - data := &warp.AfterCommData{ + data := &AfterCommData{ ConnID: "abcdefg", OccurredAt: ti, Data: []byte("hello"), Direction: "--", } - sqlite := Sqlite{pool: db} + sqlite := &HookSqlite{pool: db} sqlite.AfterComm(data) } -func TestAfterConn(t *testing.T) { +func TestHookSqliteAfterConn(t *testing.T) { db, mock, err := sqlmock.New() if err != nil { t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) @@ -91,13 +85,13 @@ func TestAfterConn(t *testing.T) { mock.ExpectExec("insert into connections").WithArgs( "abcdefg", - ti.Format(warp.TimeFormat), + ti.Format(TimeFormat), []byte("alice@example.local"), []byte("bob@example.test"), 20, ).WillReturnResult(sqlmock.NewResult(1, 1)) - data := &warp.AfterConnData{ + data := &AfterConnData{ ConnID: "abcdefg", OccurredAt: ti, MailFrom: []byte("alice@example.local"), @@ -105,29 +99,29 @@ func TestAfterConn(t *testing.T) { Elapse: 20, } - sqlite := Sqlite{pool: db} + sqlite := &HookSqlite{pool: db} sqlite.AfterConn(data) } -func TestIntegration(t *testing.T) { - err := os.Setenv("DSN", "../../testdata/warp.sqlite") +func TestHookSqliteIntegration(t *testing.T) { + err := os.Setenv("DSN", "./testdata/warp.sqlite") if err != nil { t.Fatalf("Setenv error: '%s'", err) } - sqlite := &Sqlite{} + sqlite := &HookSqlite{} sqlite.AfterInit() - id := warp.GenID().String() + id := GenID().String() - sqlite.AfterComm(&warp.AfterCommData{ + sqlite.AfterComm(&AfterCommData{ ConnID: id, OccurredAt: time.Now(), Data: []byte("hello"), Direction: "->", }) - sqlite.AfterConn(&warp.AfterConnData{ + sqlite.AfterConn(&AfterConnData{ ConnID: id, OccurredAt: time.Now(), MailFrom: []byte("alice@example.local"), diff --git a/plugins/mysql/setup.sql b/misc/setup.sql similarity index 100% rename from plugins/mysql/setup.sql rename to misc/setup.sql diff --git a/plugins.go b/plugins.go index b532a92..1f78869 100644 --- a/plugins.go +++ b/plugins.go @@ -8,7 +8,6 @@ import ( "path" "path/filepath" "plugin" - "time" ) const ( @@ -16,27 +15,6 @@ const ( TimeFormat string = "2006-01-02T15:04:05.999999" ) -type Hook interface { - AfterInit() - AfterComm(*AfterCommData) - AfterConn(*AfterConnData) -} - -type AfterCommData struct { - ConnID string - OccurredAt time.Time - Data - Direction -} - -type AfterConnData struct { - ConnID string - OccurredAt time.Time - MailFrom []byte - MailTo []byte - Elapse -} - type Plugins struct { path string hooks []Hook diff --git a/plugins/file/main.go b/plugins/file/main.go deleted file mode 100644 index becf0a6..0000000 --- a/plugins/file/main.go +++ /dev/null @@ -1,70 +0,0 @@ -package main - -import ( - "fmt" - "io" - "os" - "time" - - "github.com/linyows/warp" -) - -const ( - prefix string = "file-plugin" - commJson string = `{"type":"comm","occurred_at":"%s","connection_id":"%s","direction":"%s","data":"%s"} -` - connJson string = `{"type":"conn","occurred_at":"%s","connection_id":"%s","from":"%s","to":"%s","elapse":"%s"} -` -) - -type File struct { - file io.Writer -} - -func (f *File) writer() (io.Writer, error) { - if f.file != nil { - return f.file, nil - } - - path := os.Getenv("FILE_PATH") - if len(path) == 0 { - return nil, fmt.Errorf("missing path for file, please set `FILE_PATH`") - } - - var err error - f.file, err = os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) - if err != nil { - return nil, fmt.Errorf("os.OpenFile error: %#v\n", err) - } - - return f.file, nil -} - -func (f *File) AfterInit() { -} - -func (f *File) AfterComm(d *warp.AfterCommData) { - writer, err := f.writer() - if err != nil { - fmt.Printf("[%s] %#v\n", prefix, err) - return - } - - if _, err := fmt.Fprintf(writer, commJson, d.OccurredAt.Format(time.RFC3339), d.ConnID, d.Direction, d.Data); err != nil { - fmt.Printf("[%s] file append error: %#v\n", prefix, err) - } -} - -func (f *File) AfterConn(d *warp.AfterConnData) { - writer, err := f.writer() - if err != nil { - fmt.Printf("[%s] %#v\n", prefix, err) - return - } - - if _, err := fmt.Fprintf(writer, connJson, d.OccurredAt.Format(time.RFC3339), d.ConnID, d.MailFrom, d.MailTo, d.Elapse); err != nil { - fmt.Printf("[%s] file append error: %#v\n", prefix, err) - } -} - -var Hook File //nolint diff --git a/plugins/mysql/main.go b/plugins/mysql/main.go deleted file mode 100644 index ce0a70b..0000000 --- a/plugins/mysql/main.go +++ /dev/null @@ -1,84 +0,0 @@ -package main - -import ( - "database/sql" - "fmt" - "os" - - _ "github.com/go-sql-driver/mysql" - "github.com/linyows/warp" -) - -const ( - prefix string = "mysql-plugin" - commQuery string = "insert into communications (id, connection_id, occurred_at, direction, data) values (?, ?, ?, ?, ?)" - connQuery string = "insert into connections (id, occurred_at, mail_from, mail_to, elapse) values (?, ?, ?, ?, ?)" -) - -type Mysql struct { - pool *sql.DB // Database connection pool. -} - -func (m *Mysql) Conn() (*sql.DB, error) { - if m.pool != nil { - return m.pool, nil - } - - dsn := os.Getenv("DSN") - if len(dsn) == 0 { - return nil, fmt.Errorf("missing dsn for mysql, please set `DSN`") - } - - var err error - m.pool, err = sql.Open("mysql", dsn) - if err != nil { - return nil, fmt.Errorf("sql.Open error: %#v\n", err) - } - - return m.pool, nil -} - -func (m *Mysql) AfterInit() { -} - -func (m *Mysql) AfterComm(d *warp.AfterCommData) { - conn, err := m.Conn() - if err != nil { - fmt.Printf("[%s] %#v\n", prefix, err) - return - } - - _, err = conn.Exec( - commQuery, - warp.GenID().String(), - d.ConnID, - d.OccurredAt.Format(warp.TimeFormat), - d.Direction, - d.Data, - ) - if err != nil { - fmt.Printf("[%s] db exec error: %#v\n", prefix, err) - } -} - -func (m *Mysql) AfterConn(d *warp.AfterConnData) { - conn, err := m.Conn() - if err != nil { - fmt.Printf("[%s] %#v\n", prefix, err) - return - } - - _, err = conn.Exec( - connQuery, - d.ConnID, - d.OccurredAt.Format(warp.TimeFormat), - d.MailFrom, - d.MailTo, - d.Elapse, - ) - if err != nil { - fmt.Printf("[%s] db exec error: %#v\n", prefix, err) - } -} - -var Hook Mysql //nolint diff --git a/plugins/sqlite/main.go b/plugins/sqlite/main.go deleted file mode 100644 index 6301128..0000000 --- a/plugins/sqlite/main.go +++ /dev/null @@ -1,111 +0,0 @@ -package main - -import ( - "database/sql" - "fmt" - "os" - - "github.com/linyows/warp" - _ "github.com/mattn/go-sqlite3" -) - -const ( - prefix string = "sqlite-plugin" - commQuery string = "insert into communications (id, connection_id, occurred_at, direction, data) values ($1, $2, $3, $4, $5)" - connQuery string = "insert into connections (id, occurred_at, mail_from, mail_to, elapse) values ($1, $2, $3, $4, $5)" - connCreateTable string = `create table if not exists connections ( - id text primary key, - mail_from text, - mail_to text, - occurred_at datetime default CURRENT_TIMESTAMP, - elapse integer);` - commCreateTable string = `create table if not exists communications ( - id text primary key, - connection_id text, - direction text, - data text, - occurred_at datetime default CURRENT_TIMESTAMP)` -) - -type Sqlite struct { - pool *sql.DB // Database connection pool. -} - -func (s *Sqlite) Conn() (*sql.DB, error) { - if s.pool != nil { - return s.pool, nil - } - - dsn := os.Getenv("DSN") - if len(dsn) == 0 { - return nil, fmt.Errorf("missing dsn for sqlite, please set `DSN`") - } - - var err error - s.pool, err = sql.Open("sqlite3", dsn) - if err != nil { - return nil, fmt.Errorf("sql.Open error: %s(%#v)\n", err.Error(), err) - } - - return s.pool, nil -} - -func (s *Sqlite) AfterInit() { - conn, err := s.Conn() - if err != nil { - fmt.Printf("[%s] %s(%#v)\n", prefix, err.Error(), err) - return - } - - _, err = conn.Exec(connCreateTable) - if err != nil { - fmt.Printf("[%s] db exec error: %s(%#v)\n", prefix, err.Error(), err) - } - - _, err = conn.Exec(commCreateTable) - if err != nil { - fmt.Printf("[%s] db exec error: %s(%#v)\n", prefix, err.Error(), err) - } -} - -func (s *Sqlite) AfterComm(d *warp.AfterCommData) { - conn, err := s.Conn() - if err != nil { - fmt.Printf("[%s] %s(%#v)\n", prefix, err.Error(), err) - return - } - - _, err = conn.Exec( - commQuery, - warp.GenID().String(), - d.ConnID, - d.OccurredAt.Format(warp.TimeFormat), - d.Direction, - d.Data, - ) - if err != nil { - fmt.Printf("[%s] db exec error: %s(%#v)\n", prefix, err.Error(), err) - } -} - -func (s *Sqlite) AfterConn(d *warp.AfterConnData) { - conn, err := s.Conn() - if err != nil { - fmt.Printf("[%s] %s(%#v)\n", prefix, err.Error(), err) - return - } - - _, err = conn.Exec( - connQuery, - d.ConnID, - d.OccurredAt.Format(warp.TimeFormat), - d.MailFrom, - d.MailTo, - d.Elapse, - ) - if err != nil { - fmt.Printf("[%s] db exec error: %s(%#v)\n", prefix, err.Error(), err) - } -} - -var Hook Sqlite //nolint