質問編集履歴
6
質問の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -148,6 +148,21 @@
|
|
148
148
|
}
|
149
149
|
```
|
150
150
|
|
151
|
+
```startmainserver.go
|
152
|
+
func StartMainServer() error {
|
153
|
+
// http.FileServerはハンドラーを返す。
|
154
|
+
// http.Dirは必ずFileServerの引数にする。
|
155
|
+
// Dirで静的ファイルを読み込む。
|
156
|
+
files := http.FileServer(http.Dir(config.Config.Static))
|
157
|
+
http.Handle("/static/", http.StripPrefix("/static/", files))
|
158
|
+
```
|
159
|
+
```config.go
|
160
|
+
Static: cfg.Section("web").Key("static").String(),
|
161
|
+
```
|
162
|
+
|
163
|
+
```config.ini
|
164
|
+
static = app/views
|
165
|
+
```
|
151
166
|
### 試したこと
|
152
167
|
|
153
168
|
- VScodeのGo Liveを試したときは、以下のコードで画像が表示されました。
|
5
質問の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -84,6 +84,70 @@
|
|
84
84
|
|
85
85
|
```
|
86
86
|
|
87
|
+
```signup_circle.go
|
88
|
+
func signup_circle(w http.ResponseWriter, r *http.Request) {
|
89
|
+
// GETメソッドのとき
|
90
|
+
if r.Method == "GET" {
|
91
|
+
_, err := session(w, r)
|
92
|
+
if err != nil {
|
93
|
+
generateHTML(w, nil, "layout", "public_navbar", "/Circles/signup")
|
94
|
+
} else {
|
95
|
+
http.Redirect(w, r, "/todos", 302)
|
96
|
+
}
|
97
|
+
} else if r.Method == "POST" {
|
98
|
+
//すべてのパラメータを取得する
|
99
|
+
err := r.ParseForm()
|
100
|
+
if err != nil {
|
101
|
+
log.Println(err)
|
102
|
+
}
|
103
|
+
|
104
|
+
// "upload" に入力された画像ファイルを取り出す
|
105
|
+
fileSrc, fileHeader, err := r.FormFile("upload")
|
106
|
+
if err != nil {
|
107
|
+
http.Error(w, err.Error(), http.StatusInternalServerError)
|
108
|
+
return
|
109
|
+
}
|
110
|
+
//ファイルをクローズしておく
|
111
|
+
defer fileSrc.Close()
|
112
|
+
|
113
|
+
//アップロードされたファイル名を取得
|
114
|
+
uploadedFileName := fileHeader.Filename
|
115
|
+
//アップロードされたファイルを置くパスを設定
|
116
|
+
imagePath := "images/" + uploadedFileName
|
117
|
+
|
118
|
+
//指定された場所に新しいファイルを作成する
|
119
|
+
fileDest, err := os.Create(imagePath)
|
120
|
+
if err != nil {
|
121
|
+
http.Error(w, err.Error(), http.StatusInternalServerError)
|
122
|
+
return
|
123
|
+
}
|
124
|
+
//ファイルをクローズしておく
|
125
|
+
defer fileDest.Close()
|
126
|
+
|
127
|
+
//ファイルにデータをコピーする
|
128
|
+
io.Copy(fileDest, fileSrc)
|
129
|
+
|
130
|
+
//構造体Userにデータを格納
|
131
|
+
circle := models.Circle{
|
132
|
+
Name: r.PostFormValue("name"),
|
133
|
+
Email: r.PostFormValue("email"),
|
134
|
+
PassWord: r.PostFormValue("password"),
|
135
|
+
Picture: uploadedFileName,
|
136
|
+
}
|
137
|
+
|
138
|
+
// //ユーザ入力確認
|
139
|
+
// generateHTML(w, user, "layout", "public_navbar", "check")
|
140
|
+
|
141
|
+
//ユーザ作成
|
142
|
+
if err := circle.CreateCircle(); err != nil {
|
143
|
+
log.Println(err)
|
144
|
+
}
|
145
|
+
|
146
|
+
http.Redirect(w, r, "/todos", 302)
|
147
|
+
}
|
148
|
+
}
|
149
|
+
```
|
150
|
+
|
87
151
|
### 試したこと
|
88
152
|
|
89
153
|
- VScodeのGo Liveを試したときは、以下のコードで画像が表示されました。
|
4
質問の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -5,12 +5,10 @@
|
|
5
5
|
|
6
6
|
├─app
|
7
7
|
||ーーviews
|
8
|
-
| |ーーtemplates
|
8
|
+
| |ーーtemplates <-カレントフォルダ
|
9
|
-
| |ーーーtop.html
|
9
|
+
| |ーーーtop.html
|
10
10
|
|
|
11
11
|
└─images
|
12
|
-
|
13
|
-
フレームワークは使用していません。
|
14
12
|
|
15
13
|
```go.mod
|
16
14
|
github.com/google/uuid v1.3.0 // indirect
|
@@ -62,6 +60,30 @@
|
|
62
60
|
{{end}}
|
63
61
|
```
|
64
62
|
|
63
|
+
```view.go
|
64
|
+
// 詳細ページの表示
|
65
|
+
func view(w http.ResponseWriter, r *http.Request, id int) {
|
66
|
+
|
67
|
+
c, err := models.GetCircle(id)
|
68
|
+
if err != nil {
|
69
|
+
log.Println(err)
|
70
|
+
}
|
71
|
+
generateHTML(w, c, "layout", "public_navbar", "circle/view")
|
72
|
+
}
|
73
|
+
```
|
74
|
+
|
75
|
+
```generateHTML.go
|
76
|
+
func generateHTML(w http.ResponseWriter, data interface{}, filenames ...string) {
|
77
|
+
var files []string
|
78
|
+
for _, file := range filenames {
|
79
|
+
files = append(files, fmt.Sprintf("app/views/templates/%s.html", file))
|
80
|
+
}
|
81
|
+
templates := template.Must(template.ParseFiles(files...))
|
82
|
+
templates.ExecuteTemplate(w, "layout", data)
|
83
|
+
}ateHTML.go
|
84
|
+
|
85
|
+
```
|
86
|
+
|
65
87
|
### 試したこと
|
66
88
|
|
67
89
|
- VScodeのGo Liveを試したときは、以下のコードで画像が表示されました。
|
3
コードの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -66,7 +66,7 @@
|
|
66
66
|
|
67
67
|
- VScodeのGo Liveを試したときは、以下のコードで画像が表示されました。
|
68
68
|
```
|
69
|
-
<img src="./
|
69
|
+
<img src="./pokemon.jpg" width="20%">
|
70
70
|
```
|
71
71
|
|
72
72
|
|
2
フレームワークの提示
test
CHANGED
File without changes
|
test
CHANGED
@@ -10,11 +10,20 @@
|
|
10
10
|
|
|
11
11
|
└─images
|
12
12
|
|
13
|
+
フレームワークは使用していません。
|
14
|
+
|
15
|
+
```go.mod
|
16
|
+
github.com/google/uuid v1.3.0 // indirect
|
17
|
+
github.com/mattn/go-sqlite3 v1.14.15 // indirect
|
18
|
+
gopkg.in/go-ini/ini.v1 v1.67.0 // indirect
|
19
|
+
```
|
20
|
+
|
13
21
|
### 実現したいこと
|
14
22
|
|
15
23
|
- 画像を表示させる
|
16
24
|
|
17
25
|
### 該当のソースコード
|
26
|
+
|
18
27
|
|
19
28
|
```HTML
|
20
29
|
{{define "content"}}
|
1
ファイル構成の要約
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,13 +2,17 @@
|
|
2
2
|
|
3
3
|
Goでユーザーが登録した画像を表示するプログラムを作成しようと考えております。
|
4
4
|
|
5
|
+
|
6
|
+
├─app
|
7
|
+
||ーーviews
|
8
|
+
| |ーーtemplates
|
9
|
+
| |ーーーtop.html
|
10
|
+
|
|
11
|
+
└─images
|
12
|
+
|
5
13
|
### 実現したいこと
|
6
14
|
|
7
15
|
- 画像を表示させる
|
8
|
-
|
9
|
-
### 発生している問題・エラーメッセージ
|
10
|
-
|
11
|
-
|
12
16
|
|
13
17
|
### 該当のソースコード
|
14
18
|
|
@@ -56,6 +60,4 @@
|
|
56
60
|
<img src="./images/pokemon.jpg" width="20%">
|
57
61
|
```
|
58
62
|
|
59
|
-
- パス指定は合っていると思います。
|
60
63
|
|
61
|
-
|