Skip to content

Commit 3d9f96b

Browse files
committed
unix domain
1 parent 6cc6d71 commit 3d9f96b

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

app/webapp/go/main.go

+25-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"io"
1111
"io/ioutil"
12+
"net"
1213
"net/http"
1314
"os"
1415
"os/exec"
@@ -276,8 +277,30 @@ func main() {
276277
go trendUpdater()
277278
go insertIsuCondition()
278279

279-
serverPort := fmt.Sprintf(":%v", getEnv("SERVER_APP_PORT", "3000"))
280-
e.Logger.Fatal(e.Start(serverPort))
280+
if (getEnv("USE_SOCKET", "0") == "1") {
281+
// ここからソケット接続設定 ---
282+
socket_file := "/var/run/app.sock"
283+
os.Remove(socket_file)
284+
285+
l, err := net.Listen("unix", socket_file)
286+
if err != nil {
287+
e.Logger.Fatal(err)
288+
}
289+
290+
// go runユーザとnginxのユーザ(グループ)を同じにすれば777じゃなくてok
291+
err = os.Chmod(socket_file, 0777)
292+
if err != nil {
293+
e.Logger.Fatal(err)
294+
}
295+
296+
e.Listener = l
297+
e.Logger.Fatal(e.Start(""))
298+
// ここまで ---
299+
} else {
300+
serverPort := fmt.Sprintf(":%v", getEnv("SERVER_APP_PORT", "3000"))
301+
e.Logger.Fatal(e.Start(serverPort))
302+
}
303+
281304
}
282305

283306
func selectDBIndex(id string) int {

s1/deploy.sh

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
sudo cp -f etc/mysql/mariadb.conf.d/50-server.cnf /etc/mysql/mariadb.conf.d/50-server.cnf
44
sudo cp -f etc/nginx/nginx.conf /etc/nginx/nginx.conf
55
sudo cp -f etc/nginx/sites-available/isucondition.conf /etc/nginx/sites-available/isucondition.conf
6+
sudo cp -f home/isucon/env.sh /home/isucon/env.sh
67

78
#cp -f env /home/isucon/env
89

s1/etc/nginx/sites-available/isucondition.conf

+7-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ upstream app {
55
server isucondition-3.t.isucon.dev:3000;
66
}
77

8+
upstream unix_domain_server {
9+
server unix:/var/run/app.sock;
10+
}
11+
812
server {
913
listen 443 ssl http2;
1014

@@ -64,7 +68,7 @@ server {
6468
limit_except GET {
6569
proxy_pass http://app;
6670
}
67-
proxy_pass http://127.0.0.1:3000;
71+
proxy_pass http://unix_domain_server;
6872
}
6973

7074
location ~ ^/api/isu/.*/graph$ {
@@ -75,14 +79,14 @@ server {
7579
limit_except GET {
7680
proxy_pass http://app;
7781
}
78-
proxy_pass http://127.0.0.1:3000;
82+
proxy_pass http://unix_domain_server;
7983
}
8084

8185
location @webapp {
8286
proxy_http_version 1.1;
8387
proxy_set_header Connection "";
8488
proxy_set_header Host $http_host;
8589

86-
proxy_pass http://127.0.0.1:3000;
90+
proxy_pass http://unix_domain_server;
8791
}
8892
}

s1/home/isucon/env.sh

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
MYSQL_HOST="127.0.0.1"
2+
MYSQL_PORT=3306
3+
MYSQL_USER=isucon
4+
MYSQL_DBNAME=isucondition
5+
MYSQL_PASS=isucon
6+
POST_ISUCONDITION_TARGET_BASE_URL="https://isucondition-1.t.isucon.dev"
7+
USE_SOCKERT=1

0 commit comments

Comments
 (0)