main.go 1.3 KB

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