質問編集履歴
3
コード修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -104,6 +104,7 @@
|
|
104
104
|
|
105
105
|
// ルーティング
|
106
106
|
e.GET("/progress", progress)
|
107
|
+
e.GET("/longtask", longtask)
|
107
108
|
|
108
109
|
// local サーバー
|
109
110
|
e.Logger.Fatal(e.Start(":8000"))
|
@@ -137,4 +138,5 @@
|
|
137
138
|
|
138
139
|
return nil
|
139
140
|
}
|
141
|
+
|
140
142
|
```
|
2
現状のコードを追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -19,6 +19,10 @@
|
|
19
19
|
|
20
20
|
<<追記>>
|
21
21
|
```App.js
|
22
|
+
import React from 'react';
|
23
|
+
import axios from 'axios';
|
24
|
+
import imgLogo from './images/logo.png';
|
25
|
+
import './css/style.css'
|
22
26
|
|
23
27
|
class App extends React.Component {
|
24
28
|
|
@@ -26,65 +30,111 @@
|
|
26
30
|
|
27
31
|
super();
|
28
32
|
|
29
|
-
this.
|
33
|
+
this.state = {
|
30
34
|
|
31
|
-
|
35
|
+
}
|
32
36
|
}
|
33
37
|
|
34
|
-
|
38
|
+
interval = async() => {
|
35
39
|
|
36
|
-
|
40
|
+
console.log("interval start")
|
37
|
-
.then((response) => {
|
38
41
|
|
39
|
-
|
42
|
+
let res = await fetch("/api/progress");
|
40
|
-
})
|
41
|
-
|
43
|
+
let data = await res.json();
|
44
|
+
let progress = document.getElementById("progress");
|
45
|
+
progress.value = data.progress;
|
46
|
+
progress.innerHTML = data.progress + "%";
|
42
47
|
}
|
43
48
|
|
44
|
-
|
49
|
+
start = async() => {
|
45
50
|
|
46
|
-
|
51
|
+
console.log("start start")
|
47
|
-
.then((response) => {
|
48
52
|
|
53
|
+
let progress = document.getElementById("progress");
|
54
|
+
progress.value = 0;
|
55
|
+
progress.innerHTML = "0%";
|
49
|
-
|
56
|
+
let id = setInterval(this.interval(), 1000);
|
50
|
-
|
57
|
+
await fetch("/api/longtask");
|
51
|
-
|
58
|
+
clearInterval(id);
|
59
|
+
window.alert("long task completed");
|
52
60
|
}
|
61
|
+
|
62
|
+
render() {
|
63
|
+
return (
|
64
|
+
|
65
|
+
<div>
|
66
|
+
<div className="container">
|
67
|
+
<button id="start" onClick={async () => {await this.start();} }>start</button>
|
68
|
+
<progress id="progress" max="100" value="0">0%</progress>
|
69
|
+
</div>
|
70
|
+
</div>
|
71
|
+
)
|
72
|
+
}
|
73
|
+
}
|
74
|
+
|
75
|
+
export default App;
|
53
76
|
```
|
54
77
|
|
55
78
|
```handler.go
|
79
|
+
package handler
|
80
|
+
|
81
|
+
import (
|
82
|
+
// "encoding/json"
|
83
|
+
"fmt"
|
84
|
+
"net/http"
|
85
|
+
"sync"
|
86
|
+
"time"
|
87
|
+
|
88
|
+
_ "github.com/jinzhu/gorm/dialects/postgres"
|
89
|
+
_ "github.com/lib/pq"
|
90
|
+
|
91
|
+
"github.com/labstack/echo/v4"
|
92
|
+
"github.com/labstack/echo/v4/middleware"
|
93
|
+
)
|
94
|
+
|
95
|
+
type Progress struct {
|
96
|
+
sync.RWMutex
|
56
|
-
|
97
|
+
Progress int `json:"progress"`
|
98
|
+
}
|
57
99
|
|
58
100
|
func Handler() {
|
59
101
|
|
60
|
-
e.
|
102
|
+
e := echo.New()
|
61
|
-
e.
|
103
|
+
e.Use(middleware.CORS())
|
62
104
|
|
105
|
+
// ルーティング
|
106
|
+
e.GET("/progress", progress)
|
107
|
+
|
108
|
+
// local サーバー
|
63
109
|
e.Logger.Fatal(e.Start(":8000"))
|
64
110
|
|
65
111
|
return
|
66
112
|
}
|
67
113
|
|
68
|
-
func
|
114
|
+
func progress(c echo.Context) (err error) {
|
69
115
|
|
70
|
-
situationTime = “10%”
|
71
|
-
situationTime = “20%”
|
72
|
-
situationTime = “30%”
|
73
|
-
situationTime = “40%”
|
74
|
-
situationTime = “50%”
|
75
|
-
situationTime = “60%”
|
76
|
-
situationTime = “70%”
|
77
|
-
situationTime = “80%”
|
78
|
-
situationTime = “90%”
|
79
|
-
|
116
|
+
progress := &Progress{}
|
80
117
|
|
118
|
+
fmt.Printf("progress start")
|
119
|
+
|
120
|
+
progress.RLock()
|
81
|
-
c.JSON(http.StatusOK,
|
121
|
+
c.JSON(http.StatusOK, progress)
|
122
|
+
progress.RUnlock()
|
82
123
|
|
83
124
|
return
|
84
125
|
}
|
85
126
|
|
86
|
-
func
|
127
|
+
func longtask(c echo.Context) error {
|
87
128
|
|
129
|
+
progress := &Progress{}
|
130
|
+
|
88
|
-
r
|
131
|
+
for i := 0; i <= 10; i++ {
|
132
|
+
progress.Lock()
|
133
|
+
progress.Progress = i * 10
|
134
|
+
progress.Unlock()
|
135
|
+
time.Sleep(time.Second)
|
136
|
+
}
|
137
|
+
|
138
|
+
return nil
|
89
139
|
}
|
90
140
|
```
|
1
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -16,3 +16,75 @@
|
|
16
16
|
バックエンドがGoです。
|
17
17
|
|
18
18
|
色々と初歩的な質問ですいません。
|
19
|
+
|
20
|
+
<<追記>>
|
21
|
+
```App.js
|
22
|
+
|
23
|
+
class App extends React.Component {
|
24
|
+
|
25
|
+
constructor() {
|
26
|
+
|
27
|
+
super();
|
28
|
+
|
29
|
+
this.longMethod();
|
30
|
+
|
31
|
+
setInterval(this.sendRequest(), 1000)
|
32
|
+
}
|
33
|
+
|
34
|
+
sendRequest = async() => {
|
35
|
+
|
36
|
+
axios.get('/api/situation)
|
37
|
+
.then((response) => {
|
38
|
+
|
39
|
+
console.log(‘now is ’ + response.data)
|
40
|
+
})
|
41
|
+
.catch(console.error);
|
42
|
+
}
|
43
|
+
|
44
|
+
longMethod = () => {
|
45
|
+
|
46
|
+
axios.get(‘/longMethod’)
|
47
|
+
.then((response) => {
|
48
|
+
|
49
|
+
this.setState({hoge: response.data})
|
50
|
+
})
|
51
|
+
.catch(console.error);
|
52
|
+
}
|
53
|
+
```
|
54
|
+
|
55
|
+
```handler.go
|
56
|
+
var situationTime = "0%"
|
57
|
+
|
58
|
+
func Handler() {
|
59
|
+
|
60
|
+
e.GET("/situation", situation)
|
61
|
+
e.GET("/longMethod", longMethod)
|
62
|
+
|
63
|
+
e.Logger.Fatal(e.Start(":8000"))
|
64
|
+
|
65
|
+
return
|
66
|
+
}
|
67
|
+
|
68
|
+
func longMethod(c echo.Context) (err error) {
|
69
|
+
|
70
|
+
situationTime = “10%”
|
71
|
+
situationTime = “20%”
|
72
|
+
situationTime = “30%”
|
73
|
+
situationTime = “40%”
|
74
|
+
situationTime = “50%”
|
75
|
+
situationTime = “60%”
|
76
|
+
situationTime = “70%”
|
77
|
+
situationTime = “80%”
|
78
|
+
situationTime = “90%”
|
79
|
+
situationTime = “100%”
|
80
|
+
|
81
|
+
c.JSON(http.StatusOK, “finish”)
|
82
|
+
|
83
|
+
return
|
84
|
+
}
|
85
|
+
|
86
|
+
func situation(c echo.Context) (err error) {
|
87
|
+
|
88
|
+
return c.JSON(http.StatusOK, situationTime)
|
89
|
+
}
|
90
|
+
```
|