Addr flag for debug server, logging
This commit is contained in:
parent
03178f59e6
commit
d767921e0e
11 changed files with 54 additions and 13 deletions
|
@ -8,7 +8,7 @@ import (
|
|||
|
||||
webutils "git.mstar.dev/mstar/goutils/http"
|
||||
"git.mstar.dev/mstar/goutils/sliceutils"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/rs/zerolog/hlog"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"git.mstar.dev/mstar/linstrom/storage-new"
|
||||
|
@ -22,6 +22,7 @@ func postAs(w http.ResponseWriter, r *http.Request) {
|
|||
Username string `json:"username"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
log := hlog.FromRequest(r)
|
||||
dec := json.NewDecoder(r.Body)
|
||||
data := Inbound{}
|
||||
err := dec.Decode(&data)
|
||||
|
@ -76,6 +77,7 @@ func postAs(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func notesFrom(w http.ResponseWriter, r *http.Request) {
|
||||
log := hlog.FromRequest(r)
|
||||
username := r.FormValue("username")
|
||||
user, err := dbgen.User.GetByUsername(username)
|
||||
if err != nil {
|
||||
|
|
|
@ -12,15 +12,15 @@ package webdebug
|
|||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const DebugAddr = "127.0.0.1:3305"
|
||||
webutils "git.mstar.dev/mstar/goutils/http"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
server *http.Server
|
||||
}
|
||||
|
||||
func New() *Server {
|
||||
func New(addr string) *Server {
|
||||
handler := http.NewServeMux()
|
||||
handler.HandleFunc("GET /non-deleted", getNonDeletedUsers)
|
||||
handler.HandleFunc("POST /local-user", createLocalUser)
|
||||
|
@ -28,8 +28,11 @@ func New() *Server {
|
|||
handler.HandleFunc("POST /post-as", postAs)
|
||||
handler.HandleFunc("GET /notes-for", notesFrom)
|
||||
web := http.Server{
|
||||
Addr: DebugAddr,
|
||||
Handler: handler,
|
||||
Addr: addr,
|
||||
Handler: webutils.ChainMiddlewares(
|
||||
handler,
|
||||
webutils.BuildLoggingMiddleware(map[string]string{"server": "debug"}),
|
||||
),
|
||||
}
|
||||
return &Server{&web}
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ import (
|
|||
webutils "git.mstar.dev/mstar/goutils/http"
|
||||
"git.mstar.dev/mstar/goutils/other"
|
||||
"git.mstar.dev/mstar/goutils/sliceutils"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/rs/zerolog/hlog"
|
||||
|
||||
"git.mstar.dev/mstar/linstrom/shared"
|
||||
"git.mstar.dev/mstar/linstrom/storage-new/dbgen"
|
||||
|
@ -20,18 +20,38 @@ import (
|
|||
)
|
||||
|
||||
func getNonDeletedUsers(w http.ResponseWriter, r *http.Request) {
|
||||
log := hlog.FromRequest(r)
|
||||
pageStr := r.FormValue("page")
|
||||
page := 0
|
||||
if pageStr != "" {
|
||||
var err error
|
||||
page, err = strconv.Atoi(pageStr)
|
||||
if err != nil {
|
||||
webutils.HttpErr(w, 0, "page is not a number", http.StatusBadRequest)
|
||||
webutils.ProblemDetails(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
"/errors/bad-page",
|
||||
"bad page number",
|
||||
other.IntoPointer("page number must be an uint"),
|
||||
nil,
|
||||
)
|
||||
return
|
||||
}
|
||||
if page < 0 {
|
||||
webutils.ProblemDetails(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
"/errors/bad-page",
|
||||
"bad page number",
|
||||
other.IntoPointer("page number must be >= 0"),
|
||||
nil,
|
||||
)
|
||||
return
|
||||
}
|
||||
}
|
||||
users, err := dbgen.User.GetPagedAllNonDeleted(uint(page))
|
||||
if err != nil {
|
||||
log.Error().Err(err).Int("page", page).Msg("Failed to get non-deleted users")
|
||||
webutils.ProblemDetails(
|
||||
w,
|
||||
http.StatusInternalServerError,
|
||||
|
@ -58,6 +78,7 @@ func createLocalUser(w http.ResponseWriter, r *http.Request) {
|
|||
Location *string `json:"location"`
|
||||
IsBot bool `json:"is_bot"`
|
||||
}
|
||||
log := hlog.FromRequest(r)
|
||||
jsonDecoder := json.NewDecoder(r.Body)
|
||||
data := Inbound{}
|
||||
err := jsonDecoder.Decode(&data)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue