-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
115 lines (92 loc) · 2.1 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"fmt"
"log"
"net/http"
"os"
"os/signal"
"time"
"github.com/ss497254/keyboard-auto-typer/public"
"github.com/ss497254/keyboard-auto-typer/sendkeys"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/filesystem"
)
func parseArgs() map[string]string {
args := map[string]string{
"port": "51212",
"host": "0.0.0.0",
}
for i, arg := range os.Args {
if arg == "--host" {
args["host"] = os.Args[i+1]
} else if arg == "--port" {
args["port"] = os.Args[i+1]
}
}
return args
}
func main() {
args := parseArgs()
isRunning := false
serverAddress := fmt.Sprintf("%s:%s", args["host"], args["port"])
app := fiber.New(fiber.Config{
AppName: "send-keys-server",
})
app.Get("/", func(c *fiber.Ctx) error {
return c.JSON(&fiber.Map{
"success": true,
"message": "Hi",
})
})
app.Use("/dashboard", filesystem.New(filesystem.Config{
Root: http.FS(public.WebsiteSource),
}))
app.Post("/send", func(c *fiber.Ctx) error {
type Data struct {
Text []string `json:"text"`
StartAfter time.Duration `json:"startAfter"`
Delay time.Duration `json:"delay"`
}
data := &Data{
StartAfter: 5000,
Delay: 10,
}
if err := c.BodyParser(data); err != nil {
return c.Status(400).JSON(fiber.Map{
"success": false,
"message": "invalid data received",
"error": err.Error(),
})
}
if isRunning {
return c.Status(400).JSON(fiber.Map{
"success": false,
"message": "data is being sent, wait",
"error": "INUSE",
})
}
if data.StartAfter > 0 {
time.Sleep(data.StartAfter * time.Millisecond)
}
go func() {
isRunning = true
err := sendkeys.SendStrings(data.Text, data.Delay*time.Millisecond)
if err != nil {
fmt.Println("-- Unable to send --", err)
}
isRunning = false
}()
return c.Status(200).JSON(fiber.Map{
"success": true,
"message": "text send successfully",
})
})
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
fmt.Println("Gracefully shutting down")
app.Shutdown()
}()
log.Fatal(app.Listen(serverAddress))
}