Skip to content

Commit

Permalink
Merge pull request #7 from kayac/debug-log
Browse files Browse the repository at this point in the history
add debug logs
  • Loading branch information
fujiwara authored Oct 18, 2024
2 parents 9ed7919 + f393ce6 commit 4ce262d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
2 changes: 2 additions & 0 deletions msgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ func msgrPostHandler(w http.ResponseWriter, req *http.Request, chs []MessageChan
channel := req.FormValue("channel")
msg := req.FormValue("msg")
if channel == "" || msg == "" || req.Method != "POST" {
log.Printf("[warn] invalid request: method=%s channel=%s, msg=%s", req.Method, channel, msg)
code := http.StatusBadRequest
http.Error(w, http.StatusText(code), code)
return
}
log.Printf("[debug] post to channel=%s, msg=%s", channel, msg)
for _, ch := range chs {
ch.PostMsgr(req)
}
Expand Down
3 changes: 3 additions & 0 deletions nopaste.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const Root = "/np"

var config *Config

var Debug = false

type nopasteContent struct {
Text string
Channel string
Expand Down Expand Up @@ -125,6 +127,7 @@ func serveHandler(w http.ResponseWriter, req *http.Request, chs []MessageChan) {

func saveContent(np nopasteContent, chs []MessageChan) (string, int) {
if np.Text == "" {
log.Println("[warn] empty text")
return Root, http.StatusFound
}
data := []byte(np.Text)
Expand Down
6 changes: 4 additions & 2 deletions slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package nopaste
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"log"
"math"
"net/http"
Expand Down Expand Up @@ -47,6 +47,7 @@ func (ch SlackMessageChan) PostNopaste(np nopasteContent, url string) {
IconURL: np.IconURL,
LinkNames: np.LinkNames,
}
log.Printf("[debug] post to slack: %#v", msg)
select {
case ch <- msg:
default:
Expand All @@ -70,6 +71,7 @@ func (ch SlackMessageChan) PostMsgr(req *http.Request) {
if _notice := req.FormValue("notice"); _notice == "0" {
msg.LinkNames = 1
}
log.Printf("[debug] post to slack: %#v", msg)
select {
case ch <- msg:
default:
Expand All @@ -95,7 +97,7 @@ func (a *SlackAgent) Post(m SlackMessage) error {
if resp.StatusCode == http.StatusOK {
return nil
}
if body, err := ioutil.ReadAll(resp.Body); err == nil {
if body, err := io.ReadAll(resp.Body); err == nil {
return fmt.Errorf("failed post to slack:%s", body)
} else {
return err
Expand Down

0 comments on commit 4ce262d

Please sign in to comment.