logging cleanup

This commit is contained in:
Roland Osborne 2022-01-19 11:36:53 -08:00
parent 7930a10c42
commit 694b811a19
5 changed files with 66 additions and 43 deletions

View File

@ -8,22 +8,22 @@ import (
func SetProfile(w http.ResponseWriter, r *http.Request) { func SetProfile(w http.ResponseWriter, r *http.Request) {
account, res := BearerAppToken(r, true); account, err := BearerAppToken(r, true);
if res != nil { if err != nil {
w.WriteHeader(http.StatusUnauthorized) ErrResponse(w, http.StatusUnauthorized, err)
return return
} }
if account.Disabled { if account.Disabled {
w.WriteHeader(http.StatusGone); ErrResponse(w, http.StatusGone, nil)
return return
} }
detail := account.AccountDetail detail := account.AccountDetail
// extract profile data from body // extract profile data from body
var profileData ProfileData; var profileData ProfileData;
err := ParseRequest(r, w, &profileData) err = ParseRequest(r, w, &profileData)
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) ErrResponse(w, http.StatusBadRequest, err)
return return
} }
@ -42,9 +42,7 @@ func SetProfile(w http.ResponseWriter, r *http.Request) {
return nil return nil
}) })
if err != nil { if err != nil {
PrintMsg(err) ErrResponse(w, http.StatusInternalServerError, err)
LogMsg("failed to store profile")
w.WriteHeader(http.StatusInternalServerError)
return return
} }

View File

@ -1,7 +1,7 @@
package databag package databag
import ( import (
"log" "errors"
"sync" "sync"
"net/http" "net/http"
"encoding/json" "encoding/json"
@ -30,27 +30,31 @@ func Status(w http.ResponseWriter, r *http.Request) {
// accept websocket connection // accept websocket connection
conn, err := upgrader.Upgrade(w, r, nil) conn, err := upgrader.Upgrade(w, r, nil)
if err != nil { if err != nil {
log.Println("Status: failed upgrade connection") ErrMsg(err)
return return
} }
defer conn.Close() defer conn.Close()
// receive announce // receive announce
t, m, err := conn.ReadMessage() t, m, res := conn.ReadMessage()
if t != websocket.TextMessage || err != nil { if res != nil {
LogMsg("failed to receive announce") ErrMsg(res)
return
}
if t != websocket.TextMessage {
ErrMsg(errors.New("invalid websocket message type"))
return return
} }
var announce Announce var a Announce
if json.Unmarshal(m, &announce) != nil { if err := json.Unmarshal(m, &a); err != nil {
LogMsg("invalid announce") ErrMsg(err)
return return
} }
// retrieve reference account // retrieve reference account
var app store.App var app store.App
if store.DB.Preload("Account").Where("token = ?", announce.AppToken).First(&app).Error != nil { if err := store.DB.Preload("Account").Where("token = ?", a.AppToken).First(&app).Error; err != nil {
LogMsg("invalid app token") ErrMsg(err)
return return
} }
@ -59,12 +63,11 @@ func Status(w http.ResponseWriter, r *http.Request) {
var msg []byte var msg []byte
msg, err = json.Marshal(rev) msg, err = json.Marshal(rev)
if err != nil { if err != nil {
log.Println("Status - failed to marshal revision") ErrMsg(err)
return return
} }
err = conn.WriteMessage(websocket.TextMessage, msg) if err := conn.WriteMessage(websocket.TextMessage, msg); err != nil {
if err != nil { ErrMsg(err)
log.Println("Status - failed to send initial revision")
return return
} }
@ -80,13 +83,12 @@ func Status(w http.ResponseWriter, r *http.Request) {
for { for {
select { select {
case msg := <-c: case msg := <-c:
err = conn.WriteMessage(websocket.TextMessage, msg) if err := conn.WriteMessage(websocket.TextMessage, msg); err != nil {
if err != nil { ErrMsg(err)
log.Println("Status - failed to send revision, closing")
return return
} }
case <-wsExit: case <-wsExit:
log.Println("Status - server exit") LogMsg("exiting server")
wsExit<-true wsExit<-true
return return
} }
@ -115,7 +117,7 @@ func SetStatus(account store.Account) {
rev := getRevision(account); rev := getRevision(account);
msg, err := json.Marshal(rev) msg, err := json.Marshal(rev)
if err != nil { if err != nil {
log.Println("SetStatus - failed to marshal revision") ErrMsg(err)
return return
} }

View File

@ -37,6 +37,20 @@ func Logger(inner http.Handler, name string) http.Handler {
}) })
} }
func ErrResponse(w http.ResponseWriter, code int, err error) {
ErrMsg(err)
w.WriteHeader(code)
}
func ErrMsg(err error) {
if !hideLog && err != nil {
_, file, line, _ := runtime.Caller(1)
p, _ := os.Getwd()
log.Printf("%s:%d %s", strings.TrimPrefix(file, p), line, err.Error())
pretty.Println(err)
}
}
func LogMsg(msg string) { func LogMsg(msg string) {
if !hideLog { if !hideLog {
_, file, line, _ := runtime.Caller(1) _, file, line, _ := runtime.Caller(1)

View File

@ -0,0 +1,21 @@
package databag
import (
"net/url"
"net/http"
"net/http/httptest"
)
type StatusHandler struct {}
func (h *StatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Status(w, r)
}
func StartTestWebsocketServer() string {
h := StatusHandler{}
s := httptest.NewServer(&h)
wsUrl, _ := url.Parse(s.URL)
wsUrl.Scheme = "ws"
return wsUrl.String()
}

View File

@ -9,26 +9,14 @@ import (
"crypto/rsa" "crypto/rsa"
"crypto" "crypto"
"time" "time"
"net/url"
"net/http"
"net/http/httptest"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
) )
type StatusHandler struct {}
func (h *StatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Status(w, r)
}
func TestAttachAccount(t *testing.T) { func TestAttachAccount(t *testing.T) {
// setup websocket server // setup websocket server
h := StatusHandler{} wsUrl := StartTestWebsocketServer()
s := httptest.NewServer(&h)
wsUrl, _ := url.Parse(s.URL)
wsUrl.Scheme = "ws"
// get account token // get account token
r, w, _ := NewRequest("POST", "/admin/accounts", nil) r, w, _ := NewRequest("POST", "/admin/accounts", nil)
@ -96,7 +84,7 @@ func TestAttachAccount(t *testing.T) {
assert.Less(t, cur - 60, auth.Timestamp) assert.Less(t, cur - 60, auth.Timestamp)
// app connects websocket // app connects websocket
ws, _, _ := websocket.DefaultDialer.Dial(wsUrl.String(), nil) ws, _, _ := websocket.DefaultDialer.Dial(wsUrl, nil)
announce := Announce{ AppToken: access } announce := Announce{ AppToken: access }
msg, _ := json.Marshal(&announce) msg, _ := json.Marshal(&announce)
ws.WriteMessage(websocket.TextMessage, msg) ws.WriteMessage(websocket.TextMessage, msg)