2018-05-27 22:40:42 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2018-06-20 11:29:55 +08:00
|
|
|
func commenterTokenNew() (string, error) {
|
|
|
|
commenterToken, err := randomHex(32)
|
2018-05-27 22:40:42 +08:00
|
|
|
if err != nil {
|
2018-06-20 11:29:55 +08:00
|
|
|
logger.Errorf("cannot create commenterToken: %v", err)
|
2018-05-27 22:40:42 +08:00
|
|
|
return "", errorInternal
|
|
|
|
}
|
|
|
|
|
|
|
|
statement := `
|
|
|
|
INSERT INTO
|
2018-06-20 11:29:55 +08:00
|
|
|
commenterSessions (commenterToken, creationDate)
|
|
|
|
VALUES ($1, $2 );
|
2018-05-27 22:40:42 +08:00
|
|
|
`
|
2018-06-20 11:29:55 +08:00
|
|
|
_, err = db.Exec(statement, commenterToken, time.Now().UTC())
|
2018-05-27 22:40:42 +08:00
|
|
|
if err != nil {
|
2018-06-20 11:29:55 +08:00
|
|
|
logger.Errorf("cannot insert new commenterToken: %v", err)
|
2018-05-27 22:40:42 +08:00
|
|
|
return "", errorInternal
|
|
|
|
}
|
|
|
|
|
2018-06-20 11:29:55 +08:00
|
|
|
return commenterToken, nil
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|
|
|
|
|
2018-06-20 11:29:55 +08:00
|
|
|
func commenterTokenNewHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
commenterToken, err := commenterTokenNew()
|
2018-05-27 22:40:42 +08:00
|
|
|
if err != nil {
|
2018-07-24 14:58:43 +08:00
|
|
|
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
2018-05-27 22:40:42 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-24 14:58:43 +08:00
|
|
|
bodyMarshal(w, response{"success": true, "commenterToken": commenterToken})
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|