Skip to content

Commit

Permalink
Query ticket detail
Browse files Browse the repository at this point in the history
  • Loading branch information
taoso committed Apr 11, 2024
1 parent 4a80c2b commit ba20bc0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
15 changes: 9 additions & 6 deletions cmd/zns/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,21 @@ func main() {
repo := zns.NewTicketRepo(dbPath)
repo.New("foo", 2048, "pay-1")

pay := zns.NewPay(
os.Getenv("ALIPAY_APP_ID"),
os.Getenv("ALIPAY_PRIVATE_KEY"),
os.Getenv("ALIPAY_PUBLIC_KEY"),
)
var pay zns.Pay
if id := os.Getenv("ALIPAY_APP_ID"); id != "" {
pay = zns.NewPay(
id,
os.Getenv("ALIPAY_PRIVATE_KEY"),
os.Getenv("ALIPAY_PUBLIC_KEY"),
)
}

h := zns.Handler{Upstream: upstream, Repo: repo}
th := zns.TicketHandler{Pay: pay, Repo: repo}

mux := http.NewServeMux()
mux.Handle("/dns/{token}", h)
mux.Handle("/ticket/", th)
mux.Handle("/ticket/{token}", th)

if err = http.Serve(lnTLS, mux); err != nil {
log.Fatal(err)
Expand Down
30 changes: 21 additions & 9 deletions ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import (
)

type Ticket struct {
ID int `db:"id"`
Token string `db:"token"`
Bytes int `db:"bytes"`
TotalBytes int `db:"total_bytes"`
PayOrder string `db:"pay_order"`

Created time.Time `db:"created"`
Updated time.Time `db:"updated"`
Expires time.Time `db:"expires"`
ID int `db:"id" json:"-"`
Token string `db:"token" json:"token"`
Bytes int `db:"bytes" json:"bytes"`
TotalBytes int `db:"total_bytes" json:"total_bytes"`
PayOrder string `db:"pay_order" json:"pay_order"`

Created time.Time `db:"created" json:"created"`
Updated time.Time `db:"updated" json:"updated"`
Expires time.Time `db:"expires" json:"expires"`
}

func (_ *Ticket) KeyName() string { return "id" }
Expand Down Expand Up @@ -166,6 +166,18 @@ type TicketHandler struct {
}

func (h TicketHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
token := r.PathValue("token")
ts, err := h.Repo.List(token, 10)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Add("content-type", "application/json")
json.NewEncoder(w).Encode(ts)
return
}

if r.URL.Query().Get("buy") != "" {
req := struct {
Token string `json:"token"`
Expand Down

0 comments on commit ba20bc0

Please sign in to comment.