This commit is contained in:
vipg
2026-02-09 16:38:59 +08:00
parent 2efc23cac7
commit 875feb37a5
2 changed files with 23 additions and 24 deletions

View File

@@ -3,4 +3,5 @@ package types
type Response struct { type Response struct {
Status bool `json:"status"` Status bool `json:"status"`
Message string `json:"message"` Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
} }

View File

@@ -67,12 +67,10 @@ func main() {
func routes() http.Handler { func routes() http.Handler {
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) writeJSON(w, http.StatusOK, true, "ok", nil)
w.Write([]byte("ok"))
}) })
mux.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) writeJSON(w, http.StatusOK, true, "ok", map[string]string{"version": "user-service v0.1.0"})
w.Write([]byte("user-service v0.1.0"))
}) })
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hello from user-service") fmt.Fprintf(w, "hello from user-service")
@@ -96,26 +94,26 @@ type loginResp struct {
func registerHandler(w http.ResponseWriter, r *http.Request) { func registerHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost { if r.Method != http.MethodPost {
writeJSON(w, http.StatusMethodNotAllowed, false, "method not allowed") writeJSON(w, http.StatusMethodNotAllowed, false, "method not allowed", nil)
return return
} }
var req registerReq var req registerReq
if err := json.NewDecoder(r.Body).Decode(&req); err != nil { if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeJSON(w, http.StatusBadRequest, false, "invalid json") writeJSON(w, http.StatusBadRequest, false, "invalid json", nil)
return return
} }
if !validAccount(req.Account) || !validPassword(req.Password) { if !validAccount(req.Account) || !validPassword(req.Password) {
writeJSON(w, http.StatusBadRequest, false, "invalid account or password") writeJSON(w, http.StatusBadRequest, false, "invalid account or password", nil)
return return
} }
hashed, err := bcrypt.GenerateFromPassword([]byte(req.Password), 12) hashed, err := bcrypt.GenerateFromPassword([]byte(req.Password), 12)
if err != nil { if err != nil {
writeJSON(w, http.StatusInternalServerError, false, "internal error") writeJSON(w, http.StatusInternalServerError, false, "internal error", nil)
return return
} }
tx, err := pg.Begin() tx, err := pg.Begin()
if err != nil { if err != nil {
writeJSON(w, http.StatusInternalServerError, false, "internal error") writeJSON(w, http.StatusInternalServerError, false, "internal error", nil)
return return
} }
defer func() { defer func() {
@@ -123,57 +121,57 @@ func registerHandler(w http.ResponseWriter, r *http.Request) {
}() }()
var userID string var userID string
if err := tx.QueryRow(`INSERT INTO users DEFAULT VALUES RETURNING user_id`).Scan(&userID); err != nil { if err := tx.QueryRow(`INSERT INTO users DEFAULT VALUES RETURNING user_id`).Scan(&userID); err != nil {
writeJSON(w, http.StatusInternalServerError, false, "internal error") writeJSON(w, http.StatusInternalServerError, false, "internal error", nil)
return return
} }
if _, err := tx.Exec(`INSERT INTO user_login_accounts (user_id, value, deleted) VALUES ($1, $2, false)`, userID, req.Account); err != nil { if _, err := tx.Exec(`INSERT INTO user_login_accounts (user_id, value, deleted) VALUES ($1, $2, false)`, userID, req.Account); err != nil {
if isUniqueViolation(err) { if isUniqueViolation(err) {
writeJSON(w, http.StatusConflict, false, "account exists") writeJSON(w, http.StatusConflict, false, "account exists", nil)
return return
} }
writeJSON(w, http.StatusInternalServerError, false, "internal error") writeJSON(w, http.StatusInternalServerError, false, "internal error", nil)
return return
} }
if _, err := tx.Exec(`INSERT INTO user_login_passwords (user_id, value, deleted) VALUES ($1, $2, false)`, userID, string(hashed)); err != nil { if _, err := tx.Exec(`INSERT INTO user_login_passwords (user_id, value, deleted) VALUES ($1, $2, false)`, userID, string(hashed)); err != nil {
writeJSON(w, http.StatusInternalServerError, false, "internal error") writeJSON(w, http.StatusInternalServerError, false, "internal error", nil)
return return
} }
if err := tx.Commit(); err != nil { if err := tx.Commit(); err != nil {
writeJSON(w, http.StatusInternalServerError, false, "internal error") writeJSON(w, http.StatusInternalServerError, false, "internal error", nil)
return return
} }
writeJSON(w, http.StatusCreated, true, userID) writeJSON(w, http.StatusCreated, true, "ok", map[string]string{"user_id": userID})
} }
func loginHandler(w http.ResponseWriter, r *http.Request) { func loginHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost { if r.Method != http.MethodPost {
writeJSON(w, http.StatusMethodNotAllowed, false, "method not allowed") writeJSON(w, http.StatusMethodNotAllowed, false, "method not allowed", nil)
return return
} }
var req loginReq var req loginReq
if err := json.NewDecoder(r.Body).Decode(&req); err != nil { if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeJSON(w, http.StatusBadRequest, false, "invalid json") writeJSON(w, http.StatusBadRequest, false, "invalid json", nil)
return return
} }
if !validAccount(req.Account) || !validPassword(req.Password) { if !validAccount(req.Account) || !validPassword(req.Password) {
writeJSON(w, http.StatusBadRequest, false, "invalid account or password") writeJSON(w, http.StatusBadRequest, false, "invalid account or password", nil)
return return
} }
var userID string var userID string
if err := pg.QueryRow(`SELECT user_id FROM user_login_accounts WHERE value = $1 AND deleted = false`, req.Account).Scan(&userID); err != nil { if err := pg.QueryRow(`SELECT user_id FROM user_login_accounts WHERE value = $1 AND deleted = false`, req.Account).Scan(&userID); err != nil {
writeJSON(w, http.StatusUnauthorized, false, "unauthorized") writeJSON(w, http.StatusUnauthorized, false, "unauthorized", nil)
return return
} }
var hashed string var hashed string
if err := pg.QueryRow(`SELECT value FROM user_login_passwords WHERE user_id = $1 AND deleted = false`, userID).Scan(&hashed); err != nil { if err := pg.QueryRow(`SELECT value FROM user_login_passwords WHERE user_id = $1 AND deleted = false`, userID).Scan(&hashed); err != nil {
writeJSON(w, http.StatusUnauthorized, false, "unauthorized") writeJSON(w, http.StatusUnauthorized, false, "unauthorized", nil)
return return
} }
if bcrypt.CompareHashAndPassword([]byte(hashed), []byte(req.Password)) != nil { if bcrypt.CompareHashAndPassword([]byte(hashed), []byte(req.Password)) != nil {
writeJSON(w, http.StatusUnauthorized, false, "unauthorized") writeJSON(w, http.StatusUnauthorized, false, "unauthorized", nil)
return return
} }
writeJSON(w, http.StatusOK, true, userID) writeJSON(w, http.StatusOK, true, "ok", map[string]string{"user_id": userID})
} }
func validAccount(a string) bool { func validAccount(a string) bool {
@@ -205,8 +203,8 @@ func applySchema(path string) error {
return err return err
} }
func writeJSON(w http.ResponseWriter, code int, status bool, msg string) { func writeJSON(w http.ResponseWriter, code int, status bool, msg string, data interface{}) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code) w.WriteHeader(code)
json.NewEncoder(w).Encode(types.Response{Status: status, Message: msg}) json.NewEncoder(w).Encode(types.Response{Status: status, Message: msg, Data: data})
} }