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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
package testservices
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
)
var wsUpgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool { return true },
}
func Testservice1(c *gin.Context) {
if websocket.IsWebSocketUpgrade(c.Request) {
conn, err := wsUpgrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
return
}
defer conn.Close()
for {
msgType, msg, err := conn.ReadMessage()
if err != nil {
break
}
if err := conn.WriteMessage(msgType, msg); err != nil {
break
}
}
return
}
c.JSON(200, gin.H{
"message": "Testservice1",
"headers": c.Request.Header,
"ip": c.ClientIP(),
})
}
func Testservice2(c *gin.Context) {
c.JSON(200, gin.H{
"message": "Testservice2",
"headers": c.Request.Header,
"ip": c.ClientIP(),
})
}
func Testservice3(c *gin.Context) {
c.Header("Content-Type", "text/html; charset=utf-8")
c.String(200, testservice3HTML)
}
func Testservice3Info(c *gin.Context) {
c.JSON(200, gin.H{
"message": "Testservice3",
"headers": c.Request.Header,
"ip": c.ClientIP(),
"method": c.Request.Method,
"path": c.Request.URL.Path,
})
}
const testservice3HTML = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Noxway β Frontend Testservice</title>
<style>
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: system-ui, sans-serif; background: #0f1117; color: #e2e8f0; min-height: 100vh; padding: 2rem; }
h1 { font-size: 1.5rem; font-weight: 700; margin-bottom: 0.25rem; }
.badge { display: inline-block; background: #22c55e; color: #000; font-size: 0.7rem; font-weight: 700; padding: 0.15rem 0.5rem; border-radius: 999px; margin-left: 0.5rem; vertical-align: middle; }
.badge.ws { background: #3b82f6; color: #fff; }
.subtitle { color: #64748b; font-size: 0.875rem; margin-bottom: 2rem; }
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1.5rem; max-width: 900px; }
@media (max-width: 640px) { .grid { grid-template-columns: 1fr; } }
.card { background: #1e2330; border: 1px solid #2d3448; border-radius: 0.75rem; padding: 1.25rem; }
.card h2 { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.05em; color: #64748b; margin-bottom: 0.75rem; }
pre { font-size: 0.75rem; line-height: 1.6; white-space: pre-wrap; word-break: break-all; color: #a5f3fc; }
.ws-log { font-size: 0.75rem; line-height: 1.8; height: 120px; overflow-y: auto; }
.ws-log .msg { color: #86efac; }
.ws-log .err { color: #f87171; }
.ws-log .info { color: #94a3b8; }
button { margin-top: 0.75rem; padding: 0.4rem 1rem; border: none; border-radius: 0.4rem; font-size: 0.8rem; font-weight: 600; cursor: pointer; background: #3b82f6; color: #fff; }
button:disabled { opacity: 0.4; cursor: not-allowed; }
input { width: 100%; margin-top: 0.5rem; padding: 0.4rem 0.6rem; border-radius: 0.4rem; border: 1px solid #2d3448; background: #0f1117; color: #e2e8f0; font-size: 0.8rem; }
</style>
</head>
<body>
<h1>Noxway <span class="badge">testservice3</span> <span class="badge ws">frontend</span></h1>
<p class="subtitle">Gateway frontend test β headers, ping, WebSocket echo</p>
<div class="grid">
<div class="card">
<h2>Request Info</h2>
<pre id="info">loading...</pre>
</div>
<div class="card">
<h2>Gateway Ping</h2>
<pre id="ping">β</pre>
<button onclick="doPing()">Ping /v1/testservice1</button>
</div>
<div class="card" style="grid-column: span 2">
<h2>WebSocket Echo <span class="badge ws">ws</span></h2>
<div class="ws-log" id="wslog"><span class="info">not connected</span></div>
<input id="wsmsg" placeholder="Message to send..." />
<button id="wsconnect" onclick="wsConnect()">Connect ws://localhost:8080/v1/testservice1</button>
<button id="wssend" onclick="wsSend()" disabled>Send</button>
</div>
</div>
<script>
// ββ Request info ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
fetch('/testservice3/info', { headers: { 'X-Test': 'frontend' } })
.then(r => r.json())
.then(d => { document.getElementById('info').textContent = JSON.stringify(d, null, 2) })
.catch(e => { document.getElementById('info').textContent = 'error: ' + e })
// ββ Ping ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function doPing() {
const t0 = Date.now()
fetch('/v1/testservice1')
.then(r => r.json())
.then(d => {
document.getElementById('ping').textContent =
'HTTP ' + (Date.now()-t0) + 'ms\n' + JSON.stringify(d, null, 2)
})
.catch(e => { document.getElementById('ping').textContent = 'error: ' + e })
}
// ββ WebSocket βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
let ws = null
function log(cls, msg) {
const el = document.getElementById('wslog')
const line = document.createElement('div')
line.className = cls
line.textContent = new Date().toLocaleTimeString() + ' ' + msg
el.appendChild(line)
el.scrollTop = el.scrollHeight
}
function wsConnect() {
if (ws) { ws.close(); return }
const url = 'ws://' + location.host + '/v1/testservice1'
log('info', 'connecting to ' + url)
ws = new WebSocket(url)
ws.onopen = () => { log('msg', 'connected'); document.getElementById('wssend').disabled = false; document.getElementById('wsconnect').textContent = 'Disconnect' }
ws.onmessage = e => log('msg', 'β ' + e.data)
ws.onerror = e => log('err', 'error')
ws.onclose = () => { log('info', 'closed'); ws = null; document.getElementById('wssend').disabled = true; document.getElementById('wsconnect').textContent = 'Connect ws://localhost:8080/v1/testservice1' }
}
function wsSend() {
const inp = document.getElementById('wsmsg')
if (ws && inp.value) { ws.send(inp.value); log('msg', 'β ' + inp.value); inp.value = '' }
}
document.getElementById('wsmsg').addEventListener('keydown', e => { if (e.key === 'Enter') wsSend() })
</script>
</body>
</html>`
|