main.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package main
  2. import (
  3. "embed"
  4. "flag"
  5. "io/fs"
  6. "log"
  7. "net/http"
  8. "os"
  9. "os/signal"
  10. "syscall"
  11. "imuslab.com/remdeskvm/remdeskd/mod/remdeshid"
  12. )
  13. const development = true
  14. var (
  15. usbKVMDeviceName = flag.String("usbkvm", "COM4", "USB KVM device file path")
  16. usbKVMBaudRate = flag.Int("baudrate", 115200, "USB KVM baud rate")
  17. captureDeviceName = flag.String("capture", "/dev/video0", "Video capture device file path")
  18. usbKVM *remdeshid.Controller
  19. )
  20. /* Web Server Static Files */
  21. //go:embed www
  22. var embeddedFiles embed.FS
  23. var webfs http.FileSystem
  24. func init() {
  25. // Initiate the web server static files
  26. if development {
  27. webfs = http.Dir("./www")
  28. } else {
  29. // Embed the ./www folder and trim the prefix
  30. subFS, err := fs.Sub(embeddedFiles, "www")
  31. if err != nil {
  32. log.Fatal(err)
  33. }
  34. webfs = http.FS(subFS)
  35. }
  36. // Initiate the HID controller
  37. usbKVM = remdeshid.NewHIDController(&remdeshid.Config{
  38. PortName: *usbKVMDeviceName,
  39. BaudRate: *usbKVMBaudRate,
  40. })
  41. }
  42. func main() {
  43. //Start the HID controller
  44. err := usbKVM.Connect()
  45. if err != nil {
  46. log.Fatal(err)
  47. }
  48. // Handle program exit to close the HID controller
  49. c := make(chan os.Signal, 1)
  50. signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  51. go func() {
  52. <-c
  53. log.Println("Shutting down...")
  54. usbKVM.Close()
  55. os.Exit(0)
  56. }()
  57. // Start the web server
  58. http.HandleFunc("/hid", usbKVM.HIDWebSocketHandler)
  59. http.Handle("/", http.FileServer(webfs))
  60. addr := ":9000"
  61. log.Printf("Serving on http://localhost%s\n", addr)
  62. log.Fatal(http.ListenAndServe(addr, nil))
  63. }