server: /whoami
This commit is contained in:
@@ -13,5 +13,9 @@ func Start() {
|
|||||||
w.Write([]byte("hello world"))
|
w.Write([]byte("hello world"))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
r.Route("/whoami", func(r chi.Router) {
|
||||||
|
r.Get("/", Whoami)
|
||||||
|
})
|
||||||
|
|
||||||
http.ListenAndServe(":3000", r)
|
http.ListenAndServe(":3000", r)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log/slog"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Whoami(w http.ResponseWriter, r *http.Request) {
|
||||||
|
slog.Debug("user: entering Whoami handler")
|
||||||
|
user, ok := r.Context().Value(userKey{}).(*User)
|
||||||
|
if !ok || user == nil {
|
||||||
|
slog.Debug("user: anonymous user")
|
||||||
|
w.Write([]byte("anonymous"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
ID uuid.UUID `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Password string `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type userKey struct{}
|
||||||
|
|
||||||
|
type UserPayload struct {
|
||||||
|
*User
|
||||||
|
}
|
||||||
+4
-1
@@ -2,4 +2,7 @@ module git.dubyatp.xyz/dubyatp/scannerbot/server
|
|||||||
|
|
||||||
go 1.26.3
|
go 1.26.3
|
||||||
|
|
||||||
require github.com/go-chi/chi/v5 v5.2.5 // indirect
|
require (
|
||||||
|
github.com/go-chi/chi/v5 v5.2.5 // indirect
|
||||||
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,2 +1,4 @@
|
|||||||
github.com/go-chi/chi/v5 v5.2.5 h1:Eg4myHZBjyvJmAFjFvWgrqDTXFyOzjj7YIm3L3mu6Ug=
|
github.com/go-chi/chi/v5 v5.2.5 h1:Eg4myHZBjyvJmAFjFvWgrqDTXFyOzjj7YIm3L3mu6Ug=
|
||||||
github.com/go-chi/chi/v5 v5.2.5/go.mod h1:X7Gx4mteadT3eDOMTsXzmI4/rwUpOwBHLpAfupzFJP0=
|
github.com/go-chi/chi/v5 v5.2.5/go.mod h1:X7Gx4mteadT3eDOMTsXzmI4/rwUpOwBHLpAfupzFJP0=
|
||||||
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
|||||||
Reference in New Issue
Block a user