Exporting this struct is a step towards breaking up the Start func into
a New func and Run func, enabling calling code to use:
api := webapi.New()
api.Run()
WebAPI is a more suitable name than server because it matches the
package name, and also it helps to distinguish WebAPI from the HTTP
server it uses internally (ie. webapi.server rather than server.server).
63 lines
1.8 KiB
Go
63 lines
1.8 KiB
Go
// Copyright (c) 2020-2022 The Decred developers
|
|
// Use of this source code is governed by an ISC
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package webapi
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/decred/vspd/database"
|
|
"github.com/decred/vspd/types/v2"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gin-gonic/gin/binding"
|
|
)
|
|
|
|
// ticketStatus is the handler for "POST /api/v3/ticketstatus".
|
|
func (w *WebAPI) ticketStatus(c *gin.Context) {
|
|
const funcName = "ticketStatus"
|
|
|
|
// Get values which have been added to context by middleware.
|
|
ticket := c.MustGet(ticketKey).(database.Ticket)
|
|
knownTicket := c.MustGet(knownTicketKey).(bool)
|
|
reqBytes := c.MustGet(requestBytesKey).([]byte)
|
|
|
|
if !knownTicket {
|
|
w.log.Warnf("%s: Unknown ticket (clientIP=%s)", funcName, c.ClientIP())
|
|
w.sendError(types.ErrUnknownTicket, c)
|
|
return
|
|
}
|
|
|
|
var request types.TicketStatusRequest
|
|
if err := binding.JSON.BindBody(reqBytes, &request); err != nil {
|
|
w.log.Warnf("%s: Bad request (clientIP=%s): %v", funcName, c.ClientIP(), err)
|
|
w.sendErrorWithMsg(err.Error(), types.ErrBadRequest, c)
|
|
return
|
|
}
|
|
|
|
// Get altSignAddress from database
|
|
altSignAddrData, err := w.db.AltSignAddrData(ticket.Hash)
|
|
if err != nil {
|
|
w.log.Errorf("%s: db.AltSignAddrData error (ticketHash=%s): %v", funcName, ticket.Hash, err)
|
|
w.sendError(types.ErrInternalError, c)
|
|
return
|
|
}
|
|
|
|
altSignAddr := ""
|
|
if altSignAddrData != nil {
|
|
altSignAddr = altSignAddrData.AltSignAddr
|
|
}
|
|
|
|
w.sendJSONResponse(types.TicketStatusResponse{
|
|
Timestamp: time.Now().Unix(),
|
|
Request: reqBytes,
|
|
TicketConfirmed: ticket.Confirmed,
|
|
FeeTxStatus: string(ticket.FeeTxStatus),
|
|
FeeTxHash: ticket.FeeTxHash,
|
|
AltSignAddress: altSignAddr,
|
|
VoteChoices: ticket.VoteChoices,
|
|
TreasuryPolicy: ticket.TreasuryPolicy,
|
|
TSpendPolicy: ticket.TSpendPolicy,
|
|
}, c)
|
|
}
|