2016-03-05 02:18:33 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2016-09-12 07:09:02 +03:00
|
|
|
"net"
|
2016-03-20 04:31:59 +03:00
|
|
|
"net/http"
|
2016-03-05 02:18:33 +03:00
|
|
|
"os"
|
2017-02-24 17:05:28 +03:00
|
|
|
"os/signal"
|
2016-03-05 02:18:33 +03:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2016-03-08 03:37:39 +03:00
|
|
|
"strings"
|
2017-02-24 17:05:28 +03:00
|
|
|
"sync"
|
|
|
|
"syscall"
|
2016-03-05 02:18:33 +03:00
|
|
|
|
2016-09-12 07:09:02 +03:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2016-03-05 02:18:33 +03:00
|
|
|
"github.com/tidwall/tile38/controller"
|
2016-03-06 17:55:00 +03:00
|
|
|
"github.com/tidwall/tile38/controller/log"
|
2016-03-05 02:18:33 +03:00
|
|
|
"github.com/tidwall/tile38/core"
|
2016-09-12 07:09:02 +03:00
|
|
|
"github.com/tidwall/tile38/hservice"
|
2016-03-05 02:18:33 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-03-08 16:11:03 +03:00
|
|
|
dir string
|
|
|
|
port int
|
|
|
|
host string
|
|
|
|
verbose bool
|
|
|
|
veryVerbose bool
|
|
|
|
devMode bool
|
|
|
|
quiet bool
|
2017-02-24 17:05:28 +03:00
|
|
|
pidfile string
|
2016-03-05 02:18:33 +03:00
|
|
|
)
|
|
|
|
|
2017-01-13 18:45:28 +03:00
|
|
|
// TODO: Set to false in 2.*
|
|
|
|
var httpTransport bool = true
|
|
|
|
|
2016-12-29 18:50:54 +03:00
|
|
|
// Fire up a webhook test server by using the --webhook-http-consumer-port
|
2016-05-23 23:10:43 +03:00
|
|
|
// for example
|
2016-09-12 07:09:02 +03:00
|
|
|
// $ ./tile38-server --webhook-http-consumer-port 9999
|
2016-05-23 23:10:43 +03:00
|
|
|
//
|
|
|
|
// The create hooks like such...
|
|
|
|
// SETHOOK myhook http://localhost:9999/myhook NEARBY mykey FENCE POINT 33.5 -115.5 1000
|
|
|
|
|
2016-09-12 07:09:02 +03:00
|
|
|
type hserver struct{}
|
|
|
|
|
|
|
|
func (s *hserver) Send(ctx context.Context, in *hservice.MessageRequest) (*hservice.MessageReply, error) {
|
|
|
|
return &hservice.MessageReply{true}, nil
|
|
|
|
}
|
|
|
|
|
2016-03-05 02:18:33 +03:00
|
|
|
func main() {
|
2016-09-12 07:09:02 +03:00
|
|
|
if len(os.Args) == 3 && os.Args[1] == "--webhook-http-consumer-port" {
|
2017-10-03 18:49:33 +03:00
|
|
|
log.SetOutput(os.Stderr)
|
2016-03-20 04:31:59 +03:00
|
|
|
port, err := strconv.ParseUint(os.Args[2], 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
data, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2016-09-12 07:09:02 +03:00
|
|
|
log.Fatal(err)
|
2016-03-20 04:31:59 +03:00
|
|
|
}
|
|
|
|
log.HTTPf("http: %s : %s", r.URL.Path, string(data))
|
|
|
|
})
|
|
|
|
log.Infof("webhook server http://localhost:%d/", port)
|
|
|
|
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-09-12 07:09:02 +03:00
|
|
|
if len(os.Args) == 3 && os.Args[1] == "--webhook-grpc-consumer-port" {
|
2017-10-03 18:49:33 +03:00
|
|
|
log.SetOutput(os.Stderr)
|
2016-09-12 07:09:02 +03:00
|
|
|
port, err := strconv.ParseUint(os.Args[2], 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
s := grpc.NewServer()
|
|
|
|
hservice.RegisterHookServiceServer(s, &hserver{})
|
|
|
|
log.Infof("webhook server grpc://localhost:%d/", port)
|
|
|
|
if err := s.Serve(lis); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-08 03:37:39 +03:00
|
|
|
// parse non standard args.
|
|
|
|
nargs := []string{os.Args[0]}
|
|
|
|
for i := 1; i < len(os.Args); i++ {
|
|
|
|
switch os.Args[i] {
|
|
|
|
case "--protected-mode", "-protected-mode":
|
|
|
|
i++
|
|
|
|
if i < len(os.Args) {
|
|
|
|
switch strings.ToLower(os.Args[i]) {
|
|
|
|
case "no":
|
2016-03-08 16:11:03 +03:00
|
|
|
core.ProtectedMode = "no"
|
2016-03-08 03:37:39 +03:00
|
|
|
case "yes":
|
2016-03-08 16:11:03 +03:00
|
|
|
core.ProtectedMode = "yes"
|
2016-03-08 03:37:39 +03:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fmt.Fprintf(os.Stderr, "protected-mode must be 'yes' or 'no'\n")
|
|
|
|
os.Exit(1)
|
|
|
|
case "--dev", "-dev":
|
|
|
|
devMode = true
|
|
|
|
continue
|
2017-01-13 18:53:01 +03:00
|
|
|
case "--http-transport", "-http-transport":
|
2017-01-13 18:45:28 +03:00
|
|
|
i++
|
|
|
|
if i < len(os.Args) {
|
|
|
|
switch strings.ToLower(os.Args[i]) {
|
|
|
|
case "1", "true", "yes":
|
|
|
|
httpTransport = true
|
|
|
|
case "0", "false", "no":
|
|
|
|
httpTransport = false
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2017-01-13 18:53:01 +03:00
|
|
|
fmt.Fprintf(os.Stderr, "http-transport must be 'yes' or 'no'\n")
|
|
|
|
os.Exit(1)
|
2016-03-08 03:37:39 +03:00
|
|
|
}
|
|
|
|
nargs = append(nargs, os.Args[i])
|
|
|
|
}
|
|
|
|
os.Args = nargs
|
|
|
|
|
2016-03-05 23:39:36 +03:00
|
|
|
flag.IntVar(&port, "p", 9851, "The listening port.")
|
2017-02-24 17:05:28 +03:00
|
|
|
flag.StringVar(&pidfile, "pidfile", "", "A file that contains the pid")
|
2016-03-08 03:37:39 +03:00
|
|
|
flag.StringVar(&host, "h", "", "The listening host.")
|
2016-03-05 02:18:33 +03:00
|
|
|
flag.StringVar(&dir, "d", "data", "The data directory.")
|
|
|
|
flag.BoolVar(&verbose, "v", false, "Enable verbose logging.")
|
|
|
|
flag.BoolVar(&quiet, "q", false, "Quiet logging. Totally silent.")
|
|
|
|
flag.BoolVar(&veryVerbose, "vv", false, "Enable very verbose logging.")
|
|
|
|
flag.Parse()
|
2017-01-13 18:45:28 +03:00
|
|
|
|
2016-03-05 02:18:33 +03:00
|
|
|
var logw io.Writer = os.Stderr
|
2017-10-03 18:53:09 +03:00
|
|
|
if quiet {
|
|
|
|
logw = ioutil.Discard
|
|
|
|
}
|
2017-10-03 18:49:33 +03:00
|
|
|
log.SetOutput(logw)
|
2016-03-05 02:18:33 +03:00
|
|
|
if quiet {
|
2017-10-03 18:49:33 +03:00
|
|
|
log.Level = 0
|
|
|
|
} else if veryVerbose {
|
|
|
|
log.Level = 3
|
|
|
|
} else if verbose {
|
|
|
|
log.Level = 2
|
|
|
|
} else {
|
|
|
|
log.Level = 1
|
2016-03-05 02:18:33 +03:00
|
|
|
}
|
2016-03-06 17:55:00 +03:00
|
|
|
core.DevMode = devMode
|
|
|
|
core.ShowDebugMessages = veryVerbose
|
2016-03-08 03:37:39 +03:00
|
|
|
|
|
|
|
hostd := ""
|
|
|
|
if host != "" {
|
|
|
|
hostd = "Addr: " + host + ", "
|
|
|
|
}
|
2016-03-14 02:39:04 +03:00
|
|
|
gitsha := " (" + core.GitSHA + ")"
|
|
|
|
if gitsha == " (0000000)" {
|
|
|
|
gitsha = ""
|
|
|
|
}
|
2017-02-24 17:05:28 +03:00
|
|
|
var pidferr error
|
|
|
|
|
|
|
|
var cleanedup bool
|
|
|
|
var cleanupMu sync.Mutex
|
|
|
|
cleanup := func() {
|
|
|
|
cleanupMu.Lock()
|
|
|
|
defer cleanupMu.Unlock()
|
|
|
|
if cleanedup {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// cleanup code
|
|
|
|
if pidfile != "" {
|
|
|
|
os.Remove(pidfile)
|
|
|
|
}
|
|
|
|
cleanedup = true
|
|
|
|
}
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
if pidfile != "" {
|
|
|
|
pidferr := ioutil.WriteFile(pidfile, []byte(fmt.Sprintf("%d\n", os.Getpid())), 0666)
|
|
|
|
if pidferr == nil {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
|
|
|
|
signal.Notify(c, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
|
|
|
|
go func() {
|
|
|
|
s := <-c
|
|
|
|
log.Warnf("signal: %v", s)
|
|
|
|
if pidfile != "" {
|
|
|
|
cleanup()
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
default:
|
|
|
|
os.Exit(-1)
|
|
|
|
case s == syscall.SIGHUP:
|
|
|
|
os.Exit(1)
|
|
|
|
case s == syscall.SIGINT:
|
|
|
|
os.Exit(2)
|
|
|
|
case s == syscall.SIGQUIT:
|
|
|
|
os.Exit(3)
|
|
|
|
case s == syscall.SIGTERM:
|
|
|
|
os.Exit(0xf)
|
|
|
|
}
|
|
|
|
}()
|
2016-03-05 02:18:33 +03:00
|
|
|
|
|
|
|
// _____ _ _ ___ ___
|
|
|
|
// |_ _|_| |___|_ | . |
|
|
|
|
// | | | | | -_|_ | . |
|
|
|
|
// |_| |_|_|___|___|___|
|
|
|
|
fmt.Fprintf(logw, `
|
|
|
|
_______ _______
|
|
|
|
| | |
|
2016-03-14 02:39:04 +03:00
|
|
|
|____ | _ | Tile38 %s%s %d bit (%s/%s)
|
2016-03-08 03:37:39 +03:00
|
|
|
| | | %sPort: %d, PID: %d
|
2017-01-13 18:53:01 +03:00
|
|
|
|____ | _ |
|
2017-02-12 17:12:15 +03:00
|
|
|
| | | tile38.com
|
|
|
|
|_______|_______|
|
2017-01-13 18:53:01 +03:00
|
|
|
`+"\n", core.Version, gitsha, strconv.IntSize, runtime.GOARCH, runtime.GOOS, hostd, port, os.Getpid())
|
2017-02-24 17:05:28 +03:00
|
|
|
if pidferr != nil {
|
|
|
|
log.Warnf("pidfile: %v", pidferr)
|
|
|
|
}
|
2017-01-13 18:45:28 +03:00
|
|
|
if err := controller.ListenAndServe(host, port, dir, httpTransport); err != nil {
|
2016-03-05 02:18:33 +03:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|