Skip to content

Commit e1a1352

Browse files
committed
fix: route static react resources
1 parent 102db9e commit e1a1352

File tree

6 files changed

+56
-14
lines changed

6 files changed

+56
-14
lines changed

punch-server/api.go

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package main
22

33
import (
4+
"github.com/gorilla/mux"
45
"github.com/gorilla/websocket"
6+
"io/ioutil"
57
"log"
68
"net/http"
79
"os"
10+
"strings"
811
)
912

1013
func showList(m *Manager) http.HandlerFunc {
@@ -14,14 +17,42 @@ func showList(m *Manager) http.HandlerFunc {
1417
}
1518
}
1619

20+
type ReactRouter struct {
21+
fs http.Handler
22+
other http.Handler
23+
}
24+
25+
func (rr ReactRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
26+
p := r.URL.Path
27+
28+
if strings.HasPrefix(r.URL.Path, "/ws") {
29+
rr.other.ServeHTTP(w, r)
30+
return
31+
}
32+
33+
if _, err := os.Stat("./ui/build/" + p); err != nil {
34+
bs, _ := ioutil.ReadFile("./ui/build/index.html")
35+
w.WriteHeader(200)
36+
w.Write(bs)
37+
return
38+
} else {
39+
rr.fs.ServeHTTP(w, r)
40+
}
41+
}
42+
1743
func UIServer(addr string, m *Manager) {
1844
// See https://github.com/codegangsta/gin for get to known PORT environment.
1945
if p := os.Getenv("PORT"); p != "" {
2046
addr = ":" + p
2147
}
22-
http.Handle("/list", showList(m))
23-
http.Handle("/connect", connectByWS(m))
24-
http.ListenAndServe(addr, nil)
48+
49+
r := mux.NewRouter()
50+
r.HandleFunc("/ws", connectByWS(m))
51+
52+
http.ListenAndServe(addr, ReactRouter{
53+
fs: http.FileServer(http.Dir("./ui/build/")),
54+
other: r,
55+
})
2556
}
2657

2758
var upgrader = websocket.Upgrader{

punch-server/lib.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func writeJSON(w http.ResponseWriter, code int, v interface{}) {
6868

6969
func openUrl(url string) {
7070
bin, err := exec.LookPath("xdg-open")
71-
if err != nil || true {
71+
if err != nil {
7272
log.Printf("Please open %q to see more informations\n", url)
7373
} else {
7474
exec.Command(bin, url).Run()

punch-server/ui/src/App.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import XTerm from './react-xterm.js';
99

1010
import UserView from './UserView.js';
1111

12-
const API_SERVER = "localhost:2207"
12+
const API_SERVER = `${window.location.hostname}:2207`
1313

1414
class MagicLinkWithEnsure extends Component {
1515
state = {
@@ -62,7 +62,7 @@ class MagicLink extends Component {
6262
}
6363

6464
const id = this.props.magicKey;
65-
const backend = new WebSocket(`ws://${API_SERVER}/connect?uuid=${id}`)
65+
const backend = new WebSocket(`ws://${API_SERVER}/ws?uuid=${id}`)
6666
backend.onclose = this.handleError.bind(this)
6767
backend.onopen = this.handleOpen.bind(this, backend)
6868
}
@@ -131,9 +131,8 @@ class ListMagicLink extends Component {
131131
<div>
132132
<Header> HackIt 管理后台 <Link to="/">Home</Link> </Header>
133133
<Divider/>
134-
<Header> 当前有 {ids.length} 个连接 </Header>
135-
<ul>{ids}</ul>
136-
<Link to="/mysys/8080"> 本地系统状态 </Link>
134+
<Header> 当前有 {ids.length} 个有效连接 </Header>
135+
<Link to="/mysys/7777"> 本地系统状态(localhost:7777) </Link>
137136
</div>
138137
);
139138
}

punch-server/ui/src/UserView.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class ListConnection extends Component {
123123
tryGetDetailWidget() {
124124
const uuid = this.state.detail
125125
if (uuid) {
126-
return <DetailView uuid={uuid} />
126+
return <DetailView localServer={this.props.localServer} uuid={uuid} />
127127
}
128128
return null
129129
}
@@ -224,7 +224,7 @@ class DetailView extends Component {
224224
this.state = {
225225
backend: undefined
226226
}
227-
const backend = new WebSocket(`ws://localhost:8080/tty/${props.uuid}`)
227+
const backend = new WebSocket(`ws://${props.localServer}/tty/${props.uuid}`)
228228
backend.onopen = this.setBackend.bind(this, backend)
229229
backend.onclose = this.setBackend.bind(this, undefined)
230230
}

server/lib.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func writeJSON(w http.ResponseWriter, code int, v interface{}) {
6868

6969
func openUrl(url string) {
7070
bin, err := exec.LookPath("xdg-open")
71-
if err != nil || true {
71+
if err != nil {
7272
log.Printf("Please open %q to see more informations\n", url)
7373
} else {
7474
exec.Command(bin, url).Run()

server/server.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,29 @@ import (
77
"io"
88
"net/http"
99
"os"
10+
"time"
1011
)
1112

1213
var (
13-
PunchServerAddr = "localhost:2200"
14-
LocalServerAddr = "localhost:8080"
14+
PunchServerAddr = "hackit.snyh.org:2200"
15+
LocalServerAddr = "localhost:7777"
1516
)
1617

18+
func OpenBrowser() {
19+
go func() {
20+
time.Sleep(time.Millisecond * 500)
21+
openUrl("http://hackit.snyh.org/mysys/7777")
22+
}()
23+
}
24+
1725
func main() {
1826
if p := os.Getenv("PORT"); p != "" {
1927
LocalServerAddr = ":" + p
2028
}
29+
30+
fmt.Printf("开启本地监听%q,并连接到%q\n", LocalServerAddr, PunchServerAddr)
31+
OpenBrowser()
32+
2133
m, err := NewManager(PunchServerAddr, LocalServerAddr)
2234
if err != nil {
2335
fmt.Println("ERR:", err)

0 commit comments

Comments
 (0)