WIP ping support

This commit is contained in:
nemunaire 2019-02-27 00:19:17 +01:00
parent 82f83e3d89
commit 792f6bbe25
2 changed files with 29 additions and 1 deletions

View File

@ -88,11 +88,20 @@ CREATE TABLE IF NOT EXISTS student_challenges(
id_st INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
id_student INTEGER NOT NULL,
challenge INTEGER NOT NULL,
time TIMESTAMP NOT NULL,
time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
value VARCHAR(255) NOT NULL,
CONSTRAINT token_found UNIQUE (id_student,challenge),
FOREIGN KEY(id_student) REFERENCES students(id_student)
) DEFAULT CHARACTER SET = utf8 COLLATE = utf8_bin;
`); err != nil {
return err
}
if _, err := db.Exec(`
CREATE TABLE IF NOT EXISTS student_pong(
id_student INTEGER NOT NULL,
time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY(id_student) REFERENCES students(id_student)
) DEFAULT CHARACTER SET = utf8 COLLATE = utf8_bin;
`); err != nil {
return err
}

19
token-validator/ping.go Normal file
View File

@ -0,0 +1,19 @@
package main
import (
"time"
)
func init() {
router.POST("/api/students/:sid/pong", apiHandler(studentHandler(stdPong), sslOnly))
}
func stdPong(student Student, body []byte) (interface{}, error) {
if res, err := DBExec("INSERT INTO student_pong (id_student, time) VALUES (?, ?)", student.Id, time.Now()); err != nil {
return false, err
} else if _, err := res.LastInsertId(); err != nil {
return false, err
} else {
return true, err
}
}