Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions controller/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
)

func PingHandler(c *gin.Context) {
// [Good] ステータスコードにhttpパッケージを使っている
c.JSON(http.StatusOK, gin.H{
"message": "ping",
})
Expand All @@ -25,11 +26,16 @@ func SignupHandler(c *gin.Context) {
userId := c.PostForm("UserId")
password := c.PostForm("Password")

// [Good] パスワードのHash化が出来ている
/* [Mooto] この処理は一見何をやっているかわからないので別パッケージでラップしてあげる可読性があがる
hashpass := PasswordToHash(password)
*/
checkSum := sha256.Sum256([]byte(password))
hashpass := hex.EncodeToString(checkSum[:])

user := model.User{}

// [Good] SQL文を書いているのでなんのクエリを叩いているのかがわかる
db.GetDB().Where("user_id = ?", userId).First(&user)
// SELECT * FROM users WHERE user_id = '(valuable userId)' ORDER BY id LIMIT 1;

Expand Down Expand Up @@ -101,6 +107,9 @@ func SigninHandler(c *gin.Context) {
})
return
}

//[Good] Nullチェックをしている
//[Motto] 値がNullの時の処理が書かれているとなおよい
if !result.Valid {
if checkTokenExpiration(claims) {
// if the jwt expires, regenerate and reset cookie
Expand All @@ -124,6 +133,7 @@ func SigninHandler(c *gin.Context) {
})
}

// [Motto] Controllerではなく別のパッケージに切り分けたほうがよさそう!
func generateToken(userId string) (string, error) {
expirationTime := time.Now().Add(constants.EXPIRATION_TIME)

Expand Down
20 changes: 18 additions & 2 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,39 @@ func GetDB() *gorm.DB {
return db
}

func GormConnect() {
// ※ 動かないけどこうであって欲しいて言うの書いていくね
//[Motto] Void関数
func GormConnect() error {
// .envを取得、代入
err := godotenv.Load(".env")
if err != nil {
//[Good] errorハンドリング出来ている
fmt.Printf("Fail to read .env file : %v", err)
return err
}

//[Good] 環境変数から接続情報を取得している
/*[Motto] エラーハンドリングができると良い
if dialect := os.Getenv("DIALECT"); dialect == "" {
return fmt.Errorf("DIALECT value is empty")
}
*/
DBMS := os.Getenv("DIALECT")
USER := os.Getenv("USER_NAME")
PASS := os.Getenv("PASSWORD")
PROTOCOL := os.Getenv("PROTOCOL")
DBNAME := os.Getenv("DB_NAME")

/*[Motto] 文字列操作はSprintfを使うと可読性が上がる
CONNECT := fmt.Sprintf("%s:%s@%s/%s?parseTime=true", USER, PASS, PROTOCOL, DBNAME)
*/
CONNECT := USER + ":" + PASS + "@" + PROTOCOL + "/" + DBNAME + "?parseTime=true"
db, err = gorm.Open(DBMS, CONNECT)

if err != nil {
panic(err.Error())
//[Motto] panicはプログラムが終了してしまうため、呼ぶならmain関数で呼ぶ
return err
}

return nil
}