Files
Lawliet-Chan e3b451c641 feat(prover-stats-api): add prover stats API (#635)
Co-authored-by: xinran chen <lawliet@xinran-m1x.local>
Co-authored-by: georgehao <haohongfan@gmail.com>
Co-authored-by: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com>
2023-07-20 15:33:29 +08:00

57 lines
1.4 KiB
Go

package controller
import (
"errors"
"fmt"
"strings"
"time"
"github.com/gin-gonic/gin"
"scroll-tech/prover-stats-api/internal/types"
)
// AuthController is auth API
type AuthController struct {
}
// NewAuthController returns an AuthController instance
func NewAuthController() *AuthController {
return &AuthController{}
}
// Login godoc
// @Summary login with prover public key
// @Description login with prover public key
// @Tags prover_task
// @Accept plain
// @Produce plain
// @Param pubkey query string true "prover public key"
// @Success 200 {array} types.LoginSchema
// @Router /api/prover_task/v1/request_token [get]
func (a *AuthController) Login(c *gin.Context) (interface{}, error) {
var login types.LoginParameter
if err := c.ShouldBindQuery(&login); err != nil {
return "", fmt.Errorf("missing the public_key, err:%w", err)
}
if a.checkValidPublicKey(login.PublicKey) {
return types.LoginParameter{PublicKey: login.PublicKey}, nil
}
return nil, errors.New("incorrect public_key")
}
func (a *AuthController) checkValidPublicKey(pubkey string) bool {
return strings.TrimSpace(pubkey) != ""
}
// LoginResponse response login api
func (a *AuthController) LoginResponse(c *gin.Context, code int, message string, time time.Time) {
resp := types.LoginSchema{
Time: time,
Token: message,
}
types.RenderJSON(c, code, nil, resp)
}