-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add API endpoint for log querying (admin)
- Loading branch information
1 parent
f51d319
commit ff3761f
Showing
7 changed files
with
168 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package admin | ||
|
||
import ( | ||
"database/sql" | ||
"net/http" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/LucaBernstein/beancount-bot-tg/db" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type Log struct { | ||
Created string `json:"createdOn"` | ||
Chat string `json:"chat"` | ||
Level int `json:"level"` | ||
Message string `json:"message"` | ||
} | ||
|
||
func GetLogs(from, to string, minLevel int) ([]Log, error) { | ||
rows, err := db.Connection().Query(` | ||
SELECT "created", "chat", "level", "message" | ||
FROM "app::log" | ||
WHERE "created" >= $1 AND "created" < $2 AND "level" >= $3 | ||
ORDER BY "created" DESC | ||
`, from, to, minLevel) | ||
if err != nil { | ||
return nil, err | ||
} | ||
logs := []Log{} | ||
for rows.Next() { | ||
logEntry := &Log{} | ||
chatDb := sql.NullString{} | ||
err := rows.Scan(&logEntry.Created, &chatDb, &logEntry.Level, &logEntry.Message) | ||
if err != nil { | ||
return nil, err | ||
} | ||
logEntry.Chat = chatDb.String | ||
logs = append(logs, *logEntry) | ||
} | ||
return logs, nil | ||
} | ||
|
||
func (r *Router) Logs(c *gin.Context) { | ||
from := c.Query("from") | ||
if from == "" { | ||
from = time.Now().Add(-24 * time.Hour).Format(time.DateTime) | ||
} | ||
to := c.Query("to") | ||
if to == "" { | ||
to = time.Now().Format(time.DateTime) | ||
} | ||
minLevelQ := c.Query("minLevel") | ||
if minLevelQ == "" { | ||
minLevelQ = "0" | ||
} | ||
minLevel, err := strconv.Atoi(minLevelQ) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{ | ||
"error": err.Error(), | ||
}) | ||
return | ||
} | ||
logs, err := GetLogs(from, to, minLevel) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{ | ||
"error": err.Error(), | ||
}) | ||
return | ||
} | ||
c.JSON(http.StatusOK, logs) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package admin_test | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/LucaBernstein/beancount-bot-tg/api/admin" | ||
"github.com/LucaBernstein/beancount-bot-tg/api/helpers/apiTest" | ||
"github.com/LucaBernstein/beancount-bot-tg/bot/botTest" | ||
"github.com/gin-gonic/gin" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLogs(t *testing.T) { | ||
token, mockBc, msg := apiTest.MockBcApiUser(t, 919) | ||
r := gin.Default() | ||
g := r.Group("") | ||
admin.NewRouter(mockBc).Hook(g) | ||
|
||
// Should be forbidden without admin priviledges | ||
err := apiTest.PromoteAdmin(msg.Chat.ID, false) | ||
botTest.HandleErr(t, err) | ||
w := httptest.NewRecorder() | ||
req, _ := http.NewRequest("GET", "/logs", nil) | ||
req.Header.Add("Authorization", "Bearer "+token) | ||
r.ServeHTTP(w, req) | ||
assert.Equal(t, 403, w.Result().StatusCode) | ||
|
||
err = apiTest.PromoteAdmin(msg.Chat.ID, true) | ||
botTest.HandleErr(t, err) | ||
|
||
w = httptest.NewRecorder() | ||
req, _ = http.NewRequest("GET", "/logs", nil) | ||
req.Header.Add("Authorization", "Bearer "+token) | ||
r.ServeHTTP(w, req) | ||
assert.Equal(t, 200, w.Result().StatusCode) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package admin | ||
|
||
import ( | ||
"github.com/LucaBernstein/beancount-bot-tg/api/helpers" | ||
"github.com/LucaBernstein/beancount-bot-tg/bot" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type Router struct { | ||
bc *bot.BotController | ||
} | ||
|
||
func NewRouter(bc *bot.BotController) *Router { | ||
return &Router{ | ||
bc: bc, | ||
} | ||
} | ||
|
||
func (r *Router) Hook(g *gin.RouterGroup) { | ||
g.Use(helpers.AttachChatId(r.bc)) | ||
g.Use(helpers.EnsureAdmin(r.bc)) | ||
|
||
g.GET("/logs", r.Logs) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters