write cache headers when applicable
This commit is contained in:
parent
d7de6491ea
commit
9adf74b5c2
2 changed files with 35 additions and 22 deletions
|
@ -229,6 +229,13 @@ func (s *Server) routeLogs(w http.ResponseWriter, r *http.Request) bool {
|
|||
// Disable content type sniffing for log output
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
|
||||
currentYear := fmt.Sprintf("%d", int(time.Now().Year()))
|
||||
currentMonth := fmt.Sprintf("%d", int(time.Now().Month()))
|
||||
|
||||
if request.time.year < currentYear || (request.time.year == currentYear && request.time.month < currentMonth) {
|
||||
writeCacheControl(w, r, time.Hour*8760)
|
||||
}
|
||||
|
||||
if request.responseType == responseTypeJSON {
|
||||
writeJSON(logs, http.StatusOK, w, r)
|
||||
return true
|
||||
|
@ -280,14 +287,14 @@ func reverseSlice(input []string) []string {
|
|||
//
|
||||
// List currently logged channels
|
||||
//
|
||||
// Produces:
|
||||
// - application/json
|
||||
// - text/plain
|
||||
// Produces:
|
||||
// - application/json
|
||||
// - text/plain
|
||||
//
|
||||
// Schemes: https
|
||||
// Schemes: https
|
||||
//
|
||||
// Responses:
|
||||
// 200: AllChannelsJSON
|
||||
// Responses:
|
||||
// 200: AllChannelsJSON
|
||||
func (s *Server) writeAllChannels(w http.ResponseWriter, r *http.Request) {
|
||||
response := new(AllChannelsJSON)
|
||||
response.Channels = []channel{}
|
||||
|
@ -318,6 +325,10 @@ func writeJSON(data interface{}, code int, w http.ResponseWriter, r *http.Reques
|
|||
w.Write(js)
|
||||
}
|
||||
|
||||
func writeCacheControl(w http.ResponseWriter, r *http.Request, cacheDuration time.Duration) {
|
||||
w.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%.0f", cacheDuration.Seconds()))
|
||||
}
|
||||
|
||||
func writeRaw(cLog *chatLog, code int, w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
w.WriteHeader(code)
|
||||
|
|
34
api/user.go
34
api/user.go
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/gempir/go-twitch-irc/v3"
|
||||
)
|
||||
|
@ -54,12 +55,12 @@ type RandomQuoteJSON struct {
|
|||
//
|
||||
// Get a random line from a user in a given channel
|
||||
//
|
||||
// Produces:
|
||||
// - application/json
|
||||
// - text/plain
|
||||
// Produces:
|
||||
// - application/json
|
||||
// - text/plain
|
||||
//
|
||||
// Responses:
|
||||
// 200: chatLog
|
||||
// Responses:
|
||||
// 200: chatLog
|
||||
func (s *Server) getRandomQuote(request logRequest) (*chatLog, error) {
|
||||
rawMessage, err := s.fileLogger.ReadRandomMessageForUser(request.channelid, request.userid)
|
||||
if err != nil {
|
||||
|
@ -76,14 +77,14 @@ func (s *Server) getRandomQuote(request logRequest) (*chatLog, error) {
|
|||
//
|
||||
// Lists available logs of a user
|
||||
//
|
||||
// Produces:
|
||||
// - application/json
|
||||
// - text/plain
|
||||
// Produces:
|
||||
// - application/json
|
||||
// - text/plain
|
||||
//
|
||||
// Schemes: https
|
||||
// Schemes: https
|
||||
//
|
||||
// Responses:
|
||||
// 200: logList
|
||||
// Responses:
|
||||
// 200: logList
|
||||
func (s *Server) writeAvailableLogs(w http.ResponseWriter, r *http.Request, q url.Values) {
|
||||
logs, err := s.fileLogger.GetAvailableLogsForUser(q.Get("channelid"), q.Get("userid"))
|
||||
if err != nil {
|
||||
|
@ -92,6 +93,7 @@ func (s *Server) writeAvailableLogs(w http.ResponseWriter, r *http.Request, q ur
|
|||
}
|
||||
|
||||
writeJSON(&logList{logs}, http.StatusOK, w, r)
|
||||
writeCacheControl(w, r, time.Hour)
|
||||
}
|
||||
|
||||
// swagger:route GET /channel/{channel}/user/{username} logs channelUserLogs
|
||||
|
@ -175,12 +177,12 @@ func (s *Server) writeAvailableLogs(w http.ResponseWriter, r *http.Request, q ur
|
|||
//
|
||||
// Get user logs in channel of given year month
|
||||
//
|
||||
// Produces:
|
||||
// - application/json
|
||||
// - text/plain
|
||||
// Produces:
|
||||
// - application/json
|
||||
// - text/plain
|
||||
//
|
||||
// Responses:
|
||||
// 200: chatLog
|
||||
// Responses:
|
||||
// 200: chatLog
|
||||
func (s *Server) getUserLogs(request logRequest) (*chatLog, error) {
|
||||
logMessages, err := s.fileLogger.ReadLogForUser(request.channelid, request.userid, request.time.year, request.time.month)
|
||||
if err != nil {
|
||||
|
|
Loading…
Add table
Reference in a new issue