forked from eooce/Sing-box
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
58 lines (45 loc) · 1.12 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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
)
const (
httpPort = 3000
)
func main() {
go startHTTPServer()
shellCommand := "chmod +x start.sh && ./start.sh &"
cmd := exec.Command("bash", "-c", shellCommand)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fmt.Printf("error: %v\n", err)
} else {
fmt.Printf("Server is running on port: %d\n", httpPort)
}
select {}
}
func startHTTPServer() {
http.HandleFunc("/", rootHandler)
http.HandleFunc("/sub", subHandler)
err := http.ListenAndServe(fmt.Sprintf(":%d", httpPort), nil)
if err != nil {
fmt.Printf("HTTP server error: %v\n", err)
}
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello, World!")
}
func subHandler(w http.ResponseWriter, r *http.Request) {
content, err := ioutil.ReadFile("./temp/sub.txt")
if err != nil {
http.Error(w, fmt.Sprintf("Error reading file: %v", err), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Write(content)
}