open flag

This commit is contained in:
Nazar Kanaev 2021-01-18 23:16:13 +00:00
parent e79cb9e6e0
commit 20a0a6724a
3 changed files with 16 additions and 5 deletions

11
main.go
View File

@ -7,6 +7,7 @@ import (
"github.com/nkanaev/yarr/platform" "github.com/nkanaev/yarr/platform"
"github.com/nkanaev/yarr/server" "github.com/nkanaev/yarr/server"
"github.com/nkanaev/yarr/storage" "github.com/nkanaev/yarr/storage"
sdopen "github.com/skratchdot/open-golang/open"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
@ -18,13 +19,14 @@ var GitHash string = "unknown"
func main() { func main() {
var addr, db, authfile, certfile, keyfile string var addr, db, authfile, certfile, keyfile string
var ver bool var ver, open bool
flag.StringVar(&addr, "addr", "127.0.0.1:7070", "address to run server on") flag.StringVar(&addr, "addr", "127.0.0.1:7070", "address to run server on")
flag.StringVar(&authfile, "auth-file", "", "path to a file containing username:password") flag.StringVar(&authfile, "auth-file", "", "path to a file containing username:password")
flag.StringVar(&certfile, "cert-file", "", "path to cert file for https") flag.StringVar(&certfile, "cert-file", "", "path to cert file for https")
flag.StringVar(&keyfile, "key-file", "", "path to key file for https") flag.StringVar(&keyfile, "key-file", "", "path to key file for https")
flag.StringVar(&db, "db", "", "storage file path") flag.StringVar(&db, "db", "", "storage file path")
flag.BoolVar(&ver, "version", false, "print application version") flag.BoolVar(&ver, "version", false, "print application version")
flag.BoolVar(&open, "open", false, "open the server in browser")
flag.Parse() flag.Parse()
if ver { if ver {
@ -80,11 +82,9 @@ func main() {
srv := server.New(store, logger, addr) srv := server.New(store, logger, addr)
proto := "http"
if certfile != "" && keyfile != "" { if certfile != "" && keyfile != "" {
srv.CertFile = certfile srv.CertFile = certfile
srv.KeyFile = keyfile srv.KeyFile = keyfile
proto = "https"
} }
if username != "" && password != "" { if username != "" && password != "" {
@ -92,6 +92,9 @@ func main() {
srv.Password = password srv.Password = password
} }
logger.Printf("starting server at %s://%s", proto, addr) logger.Printf("starting server at %s", srv.GetAddr())
if open {
sdopen.Run(srv.GetAddr())
}
platform.Start(srv) platform.Start(srv)
} }

View File

@ -20,7 +20,7 @@ func Start(s *server.Handler) {
for { for {
select { select {
case <-menuOpen.ClickedCh: case <-menuOpen.ClickedCh:
open.Run("http://" + s.Addr) open.Run(s.GetAddr())
case <-menuQuit.ClickedCh: case <-menuQuit.ClickedCh:
systray.Quit() systray.Quit()
} }

View File

@ -37,6 +37,14 @@ func New(db *storage.Storage, logger *log.Logger, addr string) *Handler {
} }
} }
func (h *Handler) GetAddr() string {
proto := "http"
if (h.CertFile != "" && h.KeyFile != "") {
proto = "https"
}
return proto + "://" + h.Addr
}
func (h *Handler) Start() { func (h *Handler) Start() {
h.startJobs() h.startJobs()
s := &http.Server{Addr: h.Addr, Handler: h} s := &http.Server{Addr: h.Addr, Handler: h}