main.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "log"
  6. "os"
  7. "path/filepath"
  8. "strconv"
  9. "github.com/labstack/echo/v4"
  10. "github.com/labstack/echo/v4/middleware"
  11. "github.com/sevlyar/go-daemon"
  12. )
  13. func main() {
  14. // Find program's path/directory.
  15. var pathExecutable string
  16. pathExecutable, _ = os.Executable()
  17. // Getting the directory path/name
  18. var dirPathExecutable string = filepath.Dir(pathExecutable)
  19. os.Chdir(dirPathExecutable)
  20. var background bool
  21. var port int
  22. var bind string
  23. flag.BoolVar(&background, "d", false, "Run in background detached")
  24. flag.IntVar(&port, "p", 8000, "Port to listen on")
  25. flag.StringVar(&bind, "b", "0.0.0.0", "Interface to bind to")
  26. flag.Parse()
  27. var cntxt *daemon.Context = &daemon.Context{
  28. PidFileName: "roku.pid",
  29. PidFilePerm: 0644,
  30. LogFileName: "roku.log",
  31. LogFilePerm: 0640,
  32. WorkDir: "./",
  33. Umask: 027,
  34. Args: []string{},
  35. }
  36. var d *os.Process
  37. var err error
  38. if background {
  39. d, err = cntxt.Reborn()
  40. if err != nil {
  41. log.Fatal("Unable to run:", err)
  42. }
  43. if d != nil {
  44. return
  45. }
  46. defer cntxt.Release()
  47. log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
  48. log.Println("daemon started")
  49. defer os.Remove("roku.pid")
  50. } else {
  51. // set up logging
  52. // Write log.print ect into a file
  53. var logfilename string = "roku.log"
  54. var logf *os.File
  55. logf, err = os.OpenFile(logfilename, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
  56. if err != nil {
  57. log.Panicf("Error creating log file %s: %v", logfilename, err)
  58. }
  59. log.SetOutput(logf)
  60. log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
  61. }
  62. var web WebServer = WebServer{}
  63. //web.Init(bind, "", strconv.Itoa(port))
  64. web.Init("192.168.254.75", bind, strconv.Itoa(port))
  65. var e *echo.Echo = echo.New()
  66. e.Use(middleware.Logger())
  67. e.Use(middleware.Static("static"))
  68. e.Renderer = NewRenderer("./templates/*.html", true)
  69. e.GET("/", web.homepage)
  70. e.GET("/tv/:cmd", web.tvcmd)
  71. e.GET("/connect", web.connectpage)
  72. e.GET("/con/:cmd", web.connector)
  73. e.GET("/conn_refresh", web.con_refresh)
  74. e.Logger.Fatal(e.Start(fmt.Sprintf("%s:%s", web.Host, web.Port)))
  75. }