Skip to content

Commit

Permalink
add logo to toolbar
Browse files Browse the repository at this point in the history
add test for logo handler

lint: unused param
  • Loading branch information
umputun committed Dec 28, 2023
1 parent 19b9650 commit badf19b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/webapi/assets/components/navbar.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<!-- Navbar -->
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container-fluid">
<a class="navbar-brand" href="/">TG-Spam</a>
<a class="navbar-brand" href="/">
<img src="/logo.png" alt="Logo" style="height: 30px;"> TG-Spam
</a>
<div class="collapse navbar-collapse">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
Expand Down
Binary file added app/webapi/assets/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions app/webapi/webapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ func (s *Server) routes(router *chi.Mux) *chi.Mux {
webUI.Get("/manage_samples", s.htmlManageSamplesHandler) // serve manage samples page
webUI.Get("/manage_users", s.htmlManageUsersHandler) // serve manage users page
webUI.Get("/styles.css", s.stylesHandler) // serve styles.css
webUI.Get("/logo.png", s.logoHandler) // serve logo.png

})

return router
Expand Down Expand Up @@ -486,6 +488,18 @@ func (s *Server) stylesHandler(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write(body)
}

// logoHandler handles GET /logo.png request. It returns assets/logo.png file.
func (s *Server) logoHandler(w http.ResponseWriter, _ *http.Request) {
img, err := templateFS.ReadFile("assets/logo.png")
if err != nil {
http.Error(w, "Logo not found", http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "image/png")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(img)
}

func (s *Server) renderSamples(w http.ResponseWriter) {
spam, ham, err := s.SpamFilter.DynamicSamples()
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions app/webapi/webapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,19 @@ func TestServer_stylesHandler(t *testing.T) {
assert.Contains(t, rr.Body.String(), "body", "handler should return CSS content")
}

func TestServer_logoHandler(t *testing.T) {
server := NewServer(Config{Version: "1.0"})
rr := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/logo.png", http.NoBody)
require.NoError(t, err)

handler := http.HandlerFunc(server.logoHandler)
handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusOK, rr.Code, "handler should return status OK")
assert.Equal(t, "image/png", rr.Header().Get("Content-Type"), "handler should return CSS content type")
}

func TestServer_getDynamicSamplesHandler(t *testing.T) {
mockSpamFilter := &mocks.SpamFilterMock{
DynamicSamplesFunc: func() ([]string, []string, error) {
Expand Down

0 comments on commit badf19b

Please sign in to comment.