implement basic chat history endpoint
This commit is contained in:
parent
eaccf41563
commit
bec64827d1
@ -12,6 +12,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/mediocregopher/blog.mediocregopher.com/srv/cfg"
|
"github.com/mediocregopher/blog.mediocregopher.com/srv/cfg"
|
||||||
|
"github.com/mediocregopher/blog.mediocregopher.com/srv/chat"
|
||||||
"github.com/mediocregopher/blog.mediocregopher.com/srv/mailinglist"
|
"github.com/mediocregopher/blog.mediocregopher.com/srv/mailinglist"
|
||||||
"github.com/mediocregopher/blog.mediocregopher.com/srv/pow"
|
"github.com/mediocregopher/blog.mediocregopher.com/srv/pow"
|
||||||
"github.com/mediocregopher/mediocre-go-lib/v2/mctx"
|
"github.com/mediocregopher/mediocre-go-lib/v2/mctx"
|
||||||
@ -24,6 +25,7 @@ type Params struct {
|
|||||||
Logger *mlog.Logger
|
Logger *mlog.Logger
|
||||||
PowManager pow.Manager
|
PowManager pow.Manager
|
||||||
MailingList mailinglist.MailingList
|
MailingList mailinglist.MailingList
|
||||||
|
GlobalRoom chat.Room
|
||||||
|
|
||||||
// ListenProto and ListenAddr are passed into net.Listen to create the
|
// ListenProto and ListenAddr are passed into net.Listen to create the
|
||||||
// API's listener. Both "tcp" and "unix" protocols are explicitly
|
// API's listener. Both "tcp" and "unix" protocols are explicitly
|
||||||
|
41
srv/api/chat.go
Normal file
41
srv/api/chat.go
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/mediocregopher/blog.mediocregopher.com/srv/chat"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (a *api) chatHistoryHandler() http.Handler {
|
||||||
|
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||||
|
limit, err := strToInt(r.FormValue("limit"), 0)
|
||||||
|
if err != nil {
|
||||||
|
badRequest(rw, r, fmt.Errorf("invalid limit parameter: %w", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cursor := r.FormValue("cursor")
|
||||||
|
|
||||||
|
cursor, msgs, err := a.params.GlobalRoom.History(r.Context(), chat.HistoryOpts{
|
||||||
|
Limit: limit,
|
||||||
|
Cursor: cursor,
|
||||||
|
})
|
||||||
|
|
||||||
|
if argErr := (chat.ErrInvalidArg{}); errors.As(err, &argErr) {
|
||||||
|
badRequest(rw, r, argErr.Err)
|
||||||
|
return
|
||||||
|
} else if err != nil {
|
||||||
|
internalServerError(rw, r, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonResult(rw, r, struct {
|
||||||
|
Cursor string `json:"cursor"`
|
||||||
|
Messages []chat.Message `json:"messages"`
|
||||||
|
}{
|
||||||
|
Cursor: cursor,
|
||||||
|
Messages: msgs,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/mediocregopher/mediocre-go-lib/v2/mlog"
|
"github.com/mediocregopher/mediocre-go-lib/v2/mlog"
|
||||||
)
|
)
|
||||||
@ -58,3 +59,10 @@ func internalServerError(rw http.ResponseWriter, r *http.Request, err error) {
|
|||||||
Error: "internal server error",
|
Error: "internal server error",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func strToInt(str string, defaultVal int) (int, error) {
|
||||||
|
if str == "" {
|
||||||
|
return defaultVal, nil
|
||||||
|
}
|
||||||
|
return strconv.Atoi(str)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user