Skip to content

Commit 2743b14

Browse files
committed
Fix run command
1 parent 65f5990 commit 2743b14

File tree

6 files changed

+37
-11
lines changed

6 files changed

+37
-11
lines changed

.env.sample

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ LAB_DB_USER=sample
22
LAB_DB_PASSWORD=sample
33
LAB_DB_NAME=sample
44
LAB_SLACK_TOKEN=sample
5-
LAB_SLACK_CHANNEL=sample
5+
LAB_SLACK_CHANNEL=sample
6+
LAB_SLACK_COMING_CHANNEL=sample

cmd/secretary-lab/app/run.go

+31-9
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import (
77
"path/filepath"
88
"sync"
99

10-
"github.com/chez-shanpu/secretary/pkg/slack"
11-
1210
"github.com/chez-shanpu/secretary/constants"
11+
"github.com/chez-shanpu/secretary/pkg/slack"
1312

1413
"github.com/gin-gonic/gin"
14+
_ "github.com/go-sql-driver/mysql"
1515
"github.com/jmoiron/sqlx"
1616
"github.com/spf13/cobra"
1717
"github.com/spf13/viper"
@@ -47,15 +47,18 @@ func init() {
4747
_ = viper.BindEnv("LAB_DB_NAME")
4848
_ = viper.BindEnv("LAB_SLACK_TOKEN")
4949
_ = viper.BindEnv("LAB_SLACK_CHANNEL")
50+
_ = viper.BindEnv("LAB_SLACK_COMING_CHANNEL")
5051

5152
// required
5253
_ = runCmd.MarkFlagRequired("config-path")
5354
}
5455

5556
func runServer(cmd *cobra.Command, args []string) {
57+
var err error
58+
5659
// db
5760
dataSrcName := fmt.Sprintf("%s:%s@/%s", viper.Get("LAB_DB_USER"), viper.Get("LAB_DB_PASSWORD"), viper.Get("LAB_DB_NAME"))
58-
db, err := sqlx.Open("mysql", dataSrcName)
61+
db, err = sqlx.Open("mysql", dataSrcName)
5962
if err != nil {
6063
log.Fatalf("[ERROR] sqlx.open: %s", err)
6164
}
@@ -74,11 +77,16 @@ func runServer(cmd *cobra.Command, args []string) {
7477
r.GET("/status", getStatus)
7578
r.POST("/event", postEvent)
7679

77-
r.Run()
80+
r.Run(":8080")
7881
}
7982

8083
func getStatus(c *gin.Context) {
8184
username := c.Query("name")
85+
if username == "" {
86+
c.JSON(http.StatusBadRequest, gin.H{"message": "please input name parameter"})
87+
return
88+
}
89+
8290
status, err := getCurrentStatus(username)
8391
if err != nil {
8492
log.Printf("[ERROR] getCurrentStatus db GET: %s", err)
@@ -126,7 +134,10 @@ func postEvent(c *gin.Context) {
126134
wg.Done()
127135
}(req.Username, nowStatus)
128136
go func(u, s string) {
129-
sendMessage(u, s)
137+
err := sendMessage(u, s)
138+
if err != nil {
139+
log.Printf("[ERROR] sendMessage: %s", err)
140+
}
130141
wg.Done()
131142
}(req.Username, nowStatus)
132143
wg.Wait()
@@ -147,22 +158,33 @@ func sendMessage(username, status string) error {
147158
return err
148159
}
149160
if status == constants.LabEventCome {
150-
msgStr = fmt.Sprintf("おかえりなさいませ,%s様! \n今日も一日頑張りましょう!", slackUsername)
161+
msgStr = fmt.Sprintf("おかえりなさいませ,@%s様! \n今日も一日頑張りましょう!", slackUsername)
151162
} else {
152-
msgStr = fmt.Sprintf("お疲れ様でした,%s様! \n帰り道気をつけてくださいね!", slackUsername)
163+
msgStr = fmt.Sprintf("お疲れ様でした,@%s様! \n帰り道気をつけてくださいね!", slackUsername)
153164
}
154165

155166
token := viper.GetString("LAB_SLACK_TOKEN")
156167
ch := viper.GetString("LAB_SLACK_CHANNEL")
157168
mi := slack.NewSlackMessageInfo(token, ch, msgStr)
158169
err = mi.PostMessage()
170+
if err != nil {
171+
return err
172+
}
173+
174+
if status != constants.LabEventCome {
175+
return nil
176+
}
177+
msgStr = fmt.Sprintf("@%s様がいらっしゃいました.", slackUsername)
178+
ch = viper.GetString("LAB_SLACK_COMING_CHANNEL")
179+
mi = slack.NewSlackMessageInfo(token, ch, msgStr)
180+
err = mi.PostMessage()
159181
return err
160182
}
161183

162184
func getCurrentStatus(username string) (string, error) {
163185
var todayEventNum int
164186

165-
err := db.Get(&todayEventNum, "SELECT count(*) FROM lab_event WHERE username=? created_at > CURRENT_DATE", username)
187+
err := db.Get(&todayEventNum, "SELECT count(*) FROM lab_events WHERE username=? AND created_at > CURRENT_DATE", username)
166188
if err != nil {
167189
return "", err
168190
}
@@ -177,7 +199,7 @@ func getCurrentStatus(username string) (string, error) {
177199
func convertSlackUsername(name string) (string, error) {
178200
configPath := viper.Get("secretary.lab.config")
179201
path, fileName := filepath.Split(configPath.(string))
180-
fileNameExt := filepath.Ext(path)
202+
fileNameExt := filepath.Ext(fileName)
181203
fileName = fileName[0 : len(fileName)-len(fileNameExt)]
182204

183205
viper.SetConfigName(fileName)

db/0_shema.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ CREATE DATABASE secretary;
22

33
CREATE TABLE secretary.lab_events
44
(
5-
id INTEGER NOT NULL PRIMARY KEY,
5+
id INTEGER AUTO_INCREMENT NOT NULL PRIMARY KEY,
66
username VARCHAR(128) NOT NULL,
77
event_type VARCHAR(128) NOT NULL,
88
created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL

docker-compose.yml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ services:
99
LAB_DB_NAME: sample
1010
LAB_SLACK_TOKEN: sample
1111
LAB_SLACK_CHANNEL: sample
12+
LAB_SLACK_COMING_CHANNEL: sample
1213
ports:
1314
- "8080:8080"
1415

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.15
55
require (
66
github.com/gin-gonic/gin v1.6.3
77
github.com/go-playground/validator/v10 v10.4.0 // indirect
8+
github.com/go-sql-driver/mysql v1.4.0
89
github.com/golang/protobuf v1.4.2 // indirect
910
github.com/jmoiron/sqlx v1.2.0
1011
github.com/json-iterator/go v1.1.10 // indirect

go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1
6262
github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
6363
github.com/go-playground/validator/v10 v10.4.0 h1:72qIR/m8ybvL8L5TIyfgrigqkrw7kVYAvjEvpT85l70=
6464
github.com/go-playground/validator/v10 v10.4.0/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
65+
github.com/go-sql-driver/mysql v1.4.0 h1:7LxgVwFb2hIQtMm87NdgAVfXjnt4OePseqT1tKx+opk=
6566
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
6667
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
6768
github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=

0 commit comments

Comments
 (0)