-
Notifications
You must be signed in to change notification settings - Fork 25
Conversation
maiicy/db_handle/db_handle.go
Outdated
} | ||
|
||
if isNotExist { | ||
createTodoSQL := `CREATE TABLE todos ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
嵌入的大段SQL改为以下之一:
- 独立的
.sql
文件 +go:embed
- gorm AutoMigration
maiicy/db_handle/db_handle.go
Outdated
|
||
func InsertTodo(userID int, title string, dueDate string) error { | ||
// dueDate 以文本格式(YYYY-MM-DD)表示 | ||
sqlStatement := ` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
使用ORM: gorm
,来不及就算了
maiicy/db_handle/db_handle.go
Outdated
} | ||
err2 := file.Close() | ||
if err2 != nil { | ||
return err2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
所有的error最好能返回上下文,否则这个error传到最顶层就一个错误信息根本找不到代码的问题在哪。
fmt.Errorf("close: %w", err)
- https://github.com/pkg/errors
WithMessage
或Wrap
或其他类似的库
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
err2不需要新建,复用上面的err就好。
if err = file.Close(); err!=nil{}
maiicy/handlers/handlers.go
Outdated
return | ||
} | ||
|
||
c.JSON(http.StatusCreated, gin.H{"message": "用户注册成功"}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
201确实是“成功”的状态码之一,不过大多数前端的拦截器会只做200 OK的。所以一般“成功”就直接返回200好了
maiicy/handlers/handlers.go
Outdated
|
||
user, err := db_handle.GetUserByUsername(loginData.Username) | ||
if err != nil { | ||
c.JSON(http.StatusOK, gin.H{"message": "用户名不存在"}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这类有问题的响应一般不考虑返回200了
maiicy/main.go
Outdated
|
||
func main() { | ||
|
||
databasePath := "data.db" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
考虑使用 flag 包或更重的 https://github.com/spf13/cobra 等其他CLI包把这些参数可以在命令行传入,给个默认值就好
|
||
// 检查是否提供了 JWT | ||
if tokenString == "" { | ||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里的error意义不大,在状态码已经体现了,直接 c.AbortWithStatus
) | ||
|
||
var ( | ||
SecretKey = []byte("") // 用于签名和验证 JWT 的密钥 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
考虑环境变量或CLI flag传入
maiicy/handlers/handlers.go
Outdated
return | ||
} | ||
|
||
if !utils.IsValidUsername(RegisterData.Username) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个校验可以用 binding 做替代:binding:"required,alphanum"
等类似的操作。
gin底层用的 https://github.com/go-playground/validator ,具体规则可以在README 和https://github.com/go-playground/validator/blob/master/validator_test.go 中找
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM👏提了一些细节上的建议,整体代码风格和工程能力很不错
你叫什么名字哇 ❤️ @Mai-icy |
No description provided.