commento/api/commenter_session_new.go
Adhityaa a090770b73 api: Add go files
I know this is a huge commit, but I can't be bothered to check
this in part by part.
2018-05-27 23:40:46 +05:30

38 lines
801 B
Go

package main
import (
"net/http"
"time"
)
func commenterSessionNew() (string, error) {
session, err := randomHex(32)
if err != nil {
logger.Errorf("cannot create session hex: %v", err)
return "", errorInternal
}
statement := `
INSERT INTO
commenterSessions (session, creationDate)
VALUES ($1, $2 );
`
_, err = db.Exec(statement, session, time.Now().UTC())
if err != nil {
logger.Errorf("cannot insert new session: %v", err)
return "", errorInternal
}
return session, nil
}
func commenterSessionNewHandler(w http.ResponseWriter, r *http.Request) {
session, err := commenterSessionNew()
if err != nil {
writeBody(w, response{"success": false, "message": err.Error()})
return
}
writeBody(w, response{"success": true, "session": session})
}