Ver Fonte

Move main to main.go.

Steve Thielemann há 2 anos atrás
pai
commit
d268a70db6
3 ficheiros alterados com 96 adições e 84 exclusões
  1. 7 4
      go.mod
  2. 89 0
      main.go
  3. 0 80
      webserver.go

+ 7 - 4
go.mod

@@ -3,16 +3,19 @@ module red-green/go-roku
 go 1.17
 
 require (
-	github.com/beanzilla/glom v0.0.0-20220216210432-4eb98998b39d // indirect
-	github.com/beego/x2j v0.0.0-20131220205130-a0352aadc542 // indirect
+	github.com/beanzilla/glom v0.0.0-20220216210432-4eb98998b39d
+	github.com/beego/x2j v0.0.0-20131220205130-a0352aadc542
+	github.com/labstack/echo/v4 v4.6.3
+	github.com/sevlyar/go-daemon v0.1.5
+)
+
+require (
 	github.com/fatih/structs v1.1.0 // indirect
 	github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
 	github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 // indirect
-	github.com/labstack/echo/v4 v4.6.3 // indirect
 	github.com/labstack/gommon v0.3.1 // indirect
 	github.com/mattn/go-colorable v0.1.11 // indirect
 	github.com/mattn/go-isatty v0.0.14 // indirect
-	github.com/sevlyar/go-daemon v0.1.5 // indirect
 	github.com/valyala/bytebufferpool v1.0.0 // indirect
 	github.com/valyala/fasttemplate v1.2.1 // indirect
 	golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect

+ 89 - 0
main.go

@@ -0,0 +1,89 @@
+package main
+
+import (
+	"flag"
+	"fmt"
+	"log"
+	"os"
+	"path/filepath"
+	"strconv"
+
+	"github.com/labstack/echo/v4"
+	"github.com/labstack/echo/v4/middleware"
+	"github.com/sevlyar/go-daemon"
+)
+
+func main() {
+	// Find program's path/directory.
+	var pathExecutable string
+	pathExecutable, _ = os.Executable()
+	// Getting the directory path/name
+	var dirPathExecutable string = filepath.Dir(pathExecutable)
+	os.Chdir(dirPathExecutable)
+
+	var background bool
+	var port int
+	var bind string
+	flag.BoolVar(&background, "d", false, "Run in background detached")
+	flag.IntVar(&port, "p", 8000, "Port to listen on")
+	flag.StringVar(&bind, "b", "0.0.0.0", "Interface to bind to")
+	flag.Parse()
+
+	var cntxt *daemon.Context = &daemon.Context{
+		PidFileName: "roku.pid",
+		PidFilePerm: 0644,
+		LogFileName: "roku.log",
+		LogFilePerm: 0640,
+		WorkDir:     "./",
+		Umask:       027,
+		Args:        []string{},
+	}
+
+	var d *os.Process
+	var err error
+
+	if background {
+		d, err = cntxt.Reborn()
+
+		if err != nil {
+			log.Fatal("Unable to run:", err)
+		}
+		if d != nil {
+			return
+		}
+		defer cntxt.Release()
+
+		log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
+		log.Println("daemon started")
+		defer os.Remove("roku.pid")
+	} else {
+		// set up logging
+
+		// Write log.print ect into a file
+		var logfilename string = "roku.log"
+		var logf *os.File
+
+		logf, err = os.OpenFile(logfilename, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
+		if err != nil {
+			log.Panicf("Error creating log file %s: %v", logfilename, err)
+		}
+		log.SetOutput(logf)
+		log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
+	}
+
+	var web WebServer = WebServer{}
+	web.Init(bind, "", strconv.Itoa(port))
+
+	var e *echo.Echo = echo.New()
+	e.Use(middleware.Logger())
+	e.Use(middleware.Static("static"))
+	e.Renderer = NewRenderer("./templates/*.html", true)
+
+	e.GET("/", web.homepage)
+	e.GET("/tv/:cmd", web.tvcmd)
+	e.GET("/connect", web.connectpage)
+	e.GET("/con/:cmd", web.connector)
+	e.GET("/conn_refresh", web.con_refresh)
+
+	e.Logger.Fatal(e.Start(fmt.Sprintf("%s:%s", web.Host, web.Port)))
+}

+ 0 - 80
webserver.go

@@ -2,18 +2,13 @@ package main
 
 import (
 	"bufio"
-	"flag"
 	"fmt"
 	"log"
 	"net/http"
 	"os"
-	"path/filepath"
-	"strconv"
 	"strings"
 
 	"github.com/labstack/echo/v4"
-	"github.com/labstack/echo/v4/middleware"
-	"github.com/sevlyar/go-daemon"
 )
 
 type WebServer struct {
@@ -280,78 +275,3 @@ func (w *WebServer) tvcmd(c echo.Context) error {
 	return c.Redirect(http.StatusTemporaryRedirect, "/")
 	//return c.Render(http.StatusOK, "index.html", data)
 }
-
-func main() {
-	// Find program's path/directory.
-	var pathExecutable string
-	pathExecutable, _ = os.Executable()
-	// Getting the directory path/name
-	var dirPathExecutable string = filepath.Dir(pathExecutable)
-	os.Chdir(dirPathExecutable)
-
-	var background bool
-	var port int
-	var bind string
-	flag.BoolVar(&background, "d", false, "Run in background detached")
-	flag.IntVar(&port, "p", 8000, "Port to listen on")
-	flag.StringVar(&bind, "b", "0.0.0.0", "Interface to bind to")
-	flag.Parse()
-
-	var cntxt *daemon.Context = &daemon.Context{
-		PidFileName: "roku.pid",
-		PidFilePerm: 0644,
-		LogFileName: "roku.log",
-		LogFilePerm: 0640,
-		WorkDir:     "./",
-		Umask:       027,
-		Args:        []string{},
-	}
-
-	var d *os.Process
-	var err error
-
-	if background {
-		d, err = cntxt.Reborn()
-
-		if err != nil {
-			log.Fatal("Unable to run:", err)
-		}
-		if d != nil {
-			return
-		}
-		defer cntxt.Release()
-
-		log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
-		log.Println("daemon started")
-		defer os.Remove("roku.pid")
-	} else {
-		// set up logging
-
-		// Write log.print ect into a file
-		var logfilename string = "roku.log"
-		var logf *os.File
-
-		logf, err = os.OpenFile(logfilename, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
-		if err != nil {
-			log.Panicf("Error creating log file %s: %v", logfilename, err)
-		}
-		log.SetOutput(logf)
-		log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
-	}
-
-	var web WebServer = WebServer{}
-	web.Init(bind, "", strconv.Itoa(port))
-
-	var e *echo.Echo = echo.New()
-	e.Use(middleware.Logger())
-	e.Use(middleware.Static("static"))
-	e.Renderer = NewRenderer("./templates/*.html", true)
-
-	e.GET("/", web.homepage)
-	e.GET("/tv/:cmd", web.tvcmd)
-	e.GET("/connect", web.connectpage)
-	e.GET("/con/:cmd", web.connector)
-	e.GET("/conn_refresh", web.con_refresh)
-
-	e.Logger.Fatal(e.Start(fmt.Sprintf("%s:%s", web.Host, web.Port)))
-}