Skip to content

Commit

Permalink
add plugin options to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
linyows committed Nov 26, 2023
1 parent a17334b commit 283fdb6
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
warp
plugin/*.so
plugins/*.so
dist/
testdata/server.*
testdata/warp.sqlite
16 changes: 8 additions & 8 deletions cmd/warp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"os"
"strings"

"github.com/linyows/warp"
)
Expand All @@ -16,7 +17,7 @@ var (
ip = flag.String("ip", "127.0.0.1", "listen ip")
port = flag.Int("port", 0, "listen port")
oip = flag.String("outbound-ip", "0.0.0.0", "outbound ip")
storage = flag.String("storage", "", "sspecify extended storage from: mysql, sqlite, file")
plugins = flag.String("plugins", "", "use plugin names: mysql, sqlite, file, slack")
maxSize = flag.Int("message-size-limit", 10240000, "The maximal size in bytes of a message")
verbose = flag.Bool("verbose", false, "verbose logging")
verFlag = flag.Bool("version", false, "show build version")
Expand All @@ -40,13 +41,12 @@ func main() {
MessageSizeLimit: *maxSize,
}

switch *storage {
case "mysql":
w.Hooks = []warp.Hook{&warp.HookMysql{}}
case "sqlite":
w.Hooks = []warp.Hook{&warp.HookSqlite{}}
case "file":
w.Hooks = []warp.Hook{&warp.HookFile{}}
if "" != *plugins {
pp := strings.Split(*plugins, ",")
for i := range pp {
pp[i] = strings.TrimSpace(pp[i])
}
w.Plugins = pp
}

err := w.Start()
Expand Down
15 changes: 15 additions & 0 deletions plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (

type Plugins struct {
path string
list []string
hooks []Hook
}

Expand Down Expand Up @@ -69,6 +70,11 @@ func (p *Plugins) load() error {
if filepath.Ext(n) != ".so" {
continue
}
nWithoutExt := n[:len(n)-len(filepath.Ext(n))]
if !p.isAvailable(nWithoutExt) {
fmt.Printf("a plugin of %s is not available!\n", nWithoutExt)
continue
}

plug, err := p.lookup(n)
if err != nil {
Expand All @@ -81,3 +87,12 @@ func (p *Plugins) load() error {

return nil
}

func (p *Plugins) isAvailable(name string) bool {
for _, plugin := range p.list {
if name == plugin {
return true
}
}
return false
}
5 changes: 4 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Server struct {
Addr string
Port int
Hooks []Hook
Plugins []string
OutboundAddr string
Verbose bool
log *log.Logger
Expand All @@ -36,7 +37,9 @@ func (s *Server) Start() error {
s.MessageSizeLimit = 10240000
}

pl := &Plugins{}
pl := &Plugins{
list: s.Plugins,
}
if err := pl.load(); err != nil {
return err
}
Expand Down
Binary file removed testdata/warp.sqlite
Binary file not shown.

0 comments on commit 283fdb6

Please sign in to comment.