split checker from token-validator

This commit is contained in:
nemunaire 2020-03-27 14:57:14 +01:00
parent 685dc0b0ea
commit 0c661f36f6
20 changed files with 634 additions and 748 deletions

View file

@ -3,21 +3,22 @@ package main
import (
"encoding/json"
"errors"
"time"
"git.nemunai.re/lectures/adlin/libadlin"
)
var PongSecret = "felixfixit"
func init() {
router.GET("/api/students/:sid/ping", apiHandler(studentHandler(lastPing)))
router.GET("/api/students/:sid/pong", apiHandler(studentHandler(func(student Student, body []byte) (interface{}, error) {
return student.lastPongs()
router.GET("/api/students/:sid/pong", apiHandler(studentHandler(func(student adlin.Student, body []byte) (interface{}, error) {
return student.LastPongs()
})))
router.POST("/api/students/:sid/pong", apiHandler(studentHandler(stdPong), sslOnly))
}
func lastPing(student Student, body []byte) (interface{}, error) {
if pongs, err := student.lastPongs(); err != nil {
func lastPing(student adlin.Student, body []byte) (interface{}, error) {
if pongs, err := student.LastPongs(); err != nil {
return nil, err
} else if len(pongs) <= 0 {
return false, nil
@ -26,33 +27,7 @@ func lastPing(student Student, body []byte) (interface{}, error) {
}
}
type Pong struct {
Date time.Time
State bool
}
func (s Student) lastPongs() (pongs []Pong, err error) {
if rows, errr := DBQuery("SELECT time, state FROM student_pong WHERE id_student = ? ORDER BY time DESC", s.Id); errr != nil {
return nil, errr
} else {
defer rows.Close()
for rows.Next() {
var p Pong
if err = rows.Scan(&p.Date, &p.State); err != nil {
return
}
pongs = append(pongs, p)
}
if err = rows.Err(); err != nil {
return
}
return
}
}
func stdPong(student Student, body []byte) (interface{}, error) {
func stdPong(student adlin.Student, body []byte) (interface{}, error) {
var gt givenToken
if err := json.Unmarshal(body, &gt); err != nil {
return nil, err
@ -62,5 +37,5 @@ func stdPong(student Student, body []byte) (interface{}, error) {
return nil, errors.New("This is not the expected token.")
}
return true, student.onPong(gt.Challenge == 0)
return true, student.OnPong(gt.Challenge == 0)
}