Skip to content

Commit c195d5a

Browse files
committed
[websocket.js] Websockets to refresh page
Page will be refreshed after a validation will be done
1 parent e1fe81b commit c195d5a

File tree

11 files changed

+82
-9
lines changed

11 files changed

+82
-9
lines changed

assets/svgload.svg

Lines changed: 13 additions & 0 deletions
Loading

assets/websocket.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
window.addEventListener("load", function(evt) {
2+
var ws;
3+
var loc = window.location, new_uri;
4+
if (loc.protocol === "https:") {
5+
new_uri = "wss:";
6+
} else {
7+
new_uri = "ws:";
8+
}
9+
new_uri += "//" + loc.host + "/ws";
10+
ws = new WebSocket(new_uri);
11+
ws.onmessage = function(evt) {
12+
// there should be a check for the message
13+
// content, but it is not needed for now
14+
location.reload();
15+
}
16+
});

cmd/ginvalid/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func registerRoutes(r *mux.Router) {
4646
r.HandleFunc("/results/{validator}/{user}/{repo}", web.Results).Methods("GET")
4747
r.HandleFunc("/results/{validator}/{user}/{repo}/{id}", web.Results).Methods("GET")
4848
r.HandleFunc("/login", web.LoginGet).Methods("GET")
49+
r.HandleFunc("/ws", web.Socket)
4950
r.HandleFunc("/login", web.LoginPost).Methods("POST")
5051
r.HandleFunc("/logout", web.Logout).Methods("GET")
5152
r.HandleFunc("/repos", web.ListRepos).Methods("GET")

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/google/uuid v1.1.1
1111
github.com/gorilla/handlers v1.4.2
1212
github.com/gorilla/mux v1.7.3
13+
github.com/gorilla/websocket v1.5.0 // indirect
1314
github.com/magiconair/properties v1.8.1 // indirect
1415
github.com/mattn/go-colorable v0.1.4 // indirect
1516
github.com/mattn/go-isatty v0.0.10 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ github.com/gorilla/handlers v1.4.2/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/
6060
github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
6161
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
6262
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
63+
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
64+
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
6365
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
6466
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
6567
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=

internal/resources/templates/generic_results.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ const GenericResults = `
3737
{{ end }}
3838
</div>
3939
<div class="column" style="width:80%">
40+
{{ if .LoadingSVG }}
41+
<div class="center" style="width: 100px; height: 100px; margin: auto;">
42+
{{ .LoadingSVG }}
43+
</div>
44+
{{ end }}
4045
<hr>
4146
<div>
4247
<pre style="white-space: pre-wrap">{{.Content}}</pre>

internal/resources/templates/layout.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var Layout = `
1515
<link rel="stylesheet" href="/assets/semantic-2.3.1.min.css">
1616
<link rel="stylesheet" href="/assets/gogs.css">
1717
<link rel="stylesheet" href="/assets/custom.css">
18+
<script src="/assets/websocket.js"></script>
1819
<title>GIN Valid</title>
1920
<meta name="twitter:card" content="summary" />
2021
<meta name="twitter:site" content="@gnode" />

internal/web/pkg.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ package web
77

88
const (
99
serveralias = "gin"
10-
progressmsg = "A validation job for this repository is currently in progress, please do not leave this page and refresh the page after a while."
10+
progressmsg = "A validation job for this repository is currently in progress. Please do not leave this page. When it is done, it will refresh automatically. You can also refresh this page manually as many times as you wish"
1111
notvalidatedyet = "This repository has not been validated yet. To see the results, update the repository."
1212
)

internal/web/results.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ func renderInProgress(w http.ResponseWriter, r *http.Request, badge []byte, vali
241241
resHistory := resultsHistory(validator, user, repo)
242242
loggedUsername := getLoggedUserName(r)
243243
year, _, _ := time.Now().Date()
244+
svgload, err2 := ioutil.ReadFile("/assets/svgload.svg")
245+
if err2 != nil {
246+
svgload = []byte("<svg></svg>")
247+
}
244248
info := struct {
245249
Badge template.HTML
246250
Header string
@@ -249,7 +253,8 @@ func renderInProgress(w http.ResponseWriter, r *http.Request, badge []byte, vali
249253
CurrentYear int
250254
UserName string
251255
*ResultsHistoryStruct
252-
}{template.HTML(badge), head, string(progressmsg), srvcfg.GINAddresses.WebURL, year, loggedUsername, &resHistory}
256+
LoadingSVG template.HTML
257+
}{template.HTML(badge), head, string(progressmsg), srvcfg.GINAddresses.WebURL, year, loggedUsername, &resHistory, template.HTML(svgload)}
253258

254259
err = tmpl.ExecuteTemplate(w, "layout", info)
255260
if err != nil {

internal/web/user.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ func Logout(w http.ResponseWriter, r *http.Request) {
228228
Expires: time.Time{},
229229
Secure: false, // TODO: Switch when we go live
230230
}
231+
if conn, ok := websockets[cookie.Value]; ok {
232+
conn.Close()
233+
delete(websockets, cookie.Value)
234+
}
231235
http.SetCookie(w, &cookie)
232236
http.Redirect(w, r, "/login", http.StatusFound)
233237
}

0 commit comments

Comments
 (0)