Skip to content

Commit

Permalink
add HW_POLL to config
Browse files Browse the repository at this point in the history
  • Loading branch information
minoritea committed May 18, 2022
1 parent bd71bde commit 8a3ced6
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 29 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.13
require (
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/hpcloud/tail v1.0.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/wangjia184/sortedset v0.0.0-20210325043434-64dd27e173e2
go.etcd.io/bbolt v1.3.6
golang.org/x/sys v0.0.0-20210326220804-49726bf1d181 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWo
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
github.com/wangjia184/sortedset v0.0.0-20210325043434-64dd27e173e2 h1:ViCftzy+iQHmxIJPZqJH3i0znSxFufq075LnMwt2/VQ=
github.com/wangjia184/sortedset v0.0.0-20210325043434-64dd27e173e2/go.mod h1:YkocrP2K2tcw938x9gCOmT5G5eCD6jsTz0SZuyAqwIE=
go.etcd.io/bbolt v1.3.6 h1:/ecaJf0sk1l4l6V4awd65v2C3ILy7MSj+s/x1ADCIMU=
Expand Down
83 changes: 54 additions & 29 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,33 @@ package main

import (
"github.com/hpcloud/tail"
"github.com/kelseyhightower/envconfig"
"github.com/wangjia184/sortedset"
"go.etcd.io/bbolt"
"log"
"net"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
)

type Config struct {
Host string `default:"127.0.0.1"`
Port string `default:"14444"`
Token string
DbFile string
Poll bool
}

type watcher struct {
token *string
db *bbolt.DB
db *bbolt.DB
sync.RWMutex
*sortedset.SortedSet
lastIndex int
errch chan error
Config
}

var bucketName = []byte("HISTORY_WATCHER")
Expand Down Expand Up @@ -50,12 +60,24 @@ func (watcher *watcher) load() error {
}

func (watcher *watcher) watch() error {
err := watcher.load()
homedir, err := os.UserHomeDir()
if err != nil {
return err
}

t, err := tail.TailFile(filepath.Join(os.Getenv("HOME"), ".bash_history"), tail.Config{ReOpen: true, Follow: true})
err = watcher.load()
if err != nil {
return err
}

t, err := tail.TailFile(
filepath.Join(homedir, ".bash_history"),
tail.Config{
ReOpen: true,
Follow: true,
Poll: watcher.Poll,
},
)
if err != nil {
return err
}
Expand Down Expand Up @@ -98,28 +120,22 @@ func (watcher *watcher) watch() error {
}
}

func newWatcher() *watcher {
func newWatcher(conf Config) *watcher {
var (
db *bbolt.DB
token *string
db *bbolt.DB
)
dbpath := os.Getenv("HW_DBFILE")
if dbpath != "" {
if conf.DbFile != "" {
var err error
db, err = bbolt.Open(dbpath, 0600, nil)
db, err = bbolt.Open(conf.DbFile, 0600, nil)
if err != nil {
log.Println(err)
db = nil
}
}

tokenStr := os.Getenv("HW_TOKEN")
if tokenStr != "" {
token = &tokenStr
}
return &watcher{
Config: conf,
db: db,
token: token,
SortedSet: sortedset.New(),
errch: make(chan error),
}
Expand All @@ -132,14 +148,14 @@ type streamWriter interface {
}

func (watcher *watcher) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if watcher.token != nil {
if watcher.Token != "" {
authHeader := r.Header.Get("authorization")
if !strings.HasPrefix(authHeader, "Bearer ") {
w.WriteHeader(http.StatusUnauthorized)
return
}
token := strings.TrimPrefix(authHeader, "Bearer ")
if *watcher.token != token {
if watcher.Token != token {
w.WriteHeader(http.StatusUnauthorized)
return
}
Expand Down Expand Up @@ -170,21 +186,30 @@ func (watcher *watcher) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}

func main() {
watcher := newWatcher()

ip := os.Getenv("HW_IP")
if ip == "" {
ip = "127.0.0.1"
func run() error {
var conf Config
err := envconfig.Process("hw", &conf)
if err != nil {
return err
}

port := os.Getenv("HW_PORT")
if port == "" {
port = "14444"
log.Printf(
"bind_address=%s:%s, db_file=%s, token_authentication_enabled=%t, polling=%t",
conf.Host, conf.Port,
conf.DbFile,
conf.Token != "",
conf.Poll,
)
watcher := newWatcher(conf)
go func() { watcher.errch <- http.ListenAndServe(net.JoinHostPort(conf.Host, conf.Port), watcher) }()
if err := watcher.watch(); err != nil {
return err
}
return nil
}

go func() { watcher.errch <- http.ListenAndServe(ip+":"+port, watcher) }()
if err := watcher.watch(); err != nil {
func main() {
err := run()
if err != nil {
log.Fatal(err)
}
}

0 comments on commit 8a3ced6

Please sign in to comment.