docker + go

This commit is contained in:
Shiro-Nek0
2026-02-25 23:54:30 -03:00
parent 66ec27672f
commit bc02d4c3a8
5 changed files with 94 additions and 1 deletions

7
ScannerGO/go.mod Normal file
View File

@@ -0,0 +1,7 @@
module ScannerGO
go 1.25.7
require github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07
require golang.org/x/sys v0.41.0 // indirect

4
ScannerGO/go.sum Normal file
View File

@@ -0,0 +1,4 @@
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07 h1:UyzmZLoiDWMRywV4DUYb9Fbt8uiOSooupjTq10vpvnU=
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=
golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k=
golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=

72
ScannerGO/main.go Normal file
View File

@@ -0,0 +1,72 @@
package main
import (
"flag"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/tarm/serial"
)
func main() {
portName := flag.String("port", "/dev/ttyACM0", "Serial port name")
endpoint := flag.String("url", "https://scanner.sekidesu.xyz/scan", "Target URL endpoint")
baudRate := flag.Int("baud", 115200, "Baud rate")
flag.Parse()
config := &serial.Config{
Name: *portName,
Baud: *baudRate,
ReadTimeout: time.Second * 2,
}
port, err := serial.OpenPort(config)
if err != nil {
fmt.Printf("Error opening port %s: %v\n", *portName, err)
os.Exit(1)
}
defer port.Close()
fmt.Printf("Listening on %s (Baud: %d)...\n", *portName, *baudRate)
fmt.Printf("Sending data to: %s\n", *endpoint)
buf := make([]byte, 128)
for {
n, err := port.Read(buf)
if err != nil {
if err != io.EOF {
fmt.Printf("Read error: %v\n", err)
}
continue
}
if n > 0 {
content := strings.TrimSpace(string(buf[:n]))
if content != "" {
sendToEndpoint(*endpoint, content)
}
}
}
}
func sendToEndpoint(baseURL, content string) {
client := &http.Client{
Timeout: 5 * time.Second,
}
fullURL := fmt.Sprintf("%s?content=%s", baseURL, url.QueryEscape(content))
resp, err := client.Get(fullURL)
if err != nil {
fmt.Printf("Network Error: %v\n", err)
return
}
defer resp.Body.Close()
fmt.Printf("Data: [%s] | Status: %s\n", content, resp.Status)
}