Initial checkin
This commit is contained in:
36
pathvalues/1-basic/main.go
Normal file
36
pathvalues/1-basic/main.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go-mux/common/db"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/user/{userId}/view", viewUser)
|
||||
|
||||
slog.Info("starting web service on :8080")
|
||||
panic(http.ListenAndServe(":8080", nil))
|
||||
}
|
||||
|
||||
func viewUser(w http.ResponseWriter, r *http.Request) {
|
||||
userID := r.PathValue("userId")
|
||||
userIDint, err := strconv.Atoi(userID)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte("bad user id"))
|
||||
return
|
||||
}
|
||||
user, err := db.LoadUser(userIDint)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte(fmt.Sprintf("could not load user: %s", err.Error())))
|
||||
return
|
||||
}
|
||||
|
||||
slog.Info("loaded user")
|
||||
// show the user id
|
||||
w.Write([]byte(fmt.Sprintf("loaded user %s", user)))
|
||||
}
|
||||
42
pathvalues/2-slightly-less-basic/main.go
Normal file
42
pathvalues/2-slightly-less-basic/main.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go-mux/common/db"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/user/{userId}/view", viewUser)
|
||||
|
||||
slog.Info("starting web service on :8080")
|
||||
panic(http.ListenAndServe(":8080", nil))
|
||||
}
|
||||
|
||||
func viewUser(w http.ResponseWriter, r *http.Request) {
|
||||
user, err := loadUserFromRequest(r)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte(fmt.Sprintf("could not load user: %s", err.Error())))
|
||||
return
|
||||
}
|
||||
|
||||
slog.Info("loaded user")
|
||||
// show the user id
|
||||
w.Write([]byte(fmt.Sprintf("loaded user %s", user)))
|
||||
}
|
||||
|
||||
func loadUserFromRequest(r *http.Request) (db.User, error) {
|
||||
userID := r.PathValue("userId")
|
||||
userIDint, err := strconv.Atoi(userID)
|
||||
if err != nil {
|
||||
return db.User{}, err
|
||||
}
|
||||
user, err := db.LoadUser(userIDint)
|
||||
if err != nil {
|
||||
return db.User{}, err
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
48
pathvalues/3-middleware/main.go
Normal file
48
pathvalues/3-middleware/main.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"go-mux/common/db"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var userContextKey = "user"
|
||||
|
||||
func main() {
|
||||
http.Handle("/user/{userId}/view", getUserFromPath(http.HandlerFunc(viewUser)))
|
||||
|
||||
slog.Info("starting web service on :8080")
|
||||
panic(http.ListenAndServe(":8080", nil))
|
||||
}
|
||||
|
||||
func viewUser(w http.ResponseWriter, r *http.Request) {
|
||||
user := r.Context().Value(userContextKey).(db.User)
|
||||
|
||||
slog.Info("loaded user")
|
||||
// show the user id
|
||||
w.Write([]byte(fmt.Sprintf("loaded user %s", user.String())))
|
||||
}
|
||||
|
||||
func getUserFromPath(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
userID := r.PathValue("userId")
|
||||
userIDint, err := strconv.Atoi(userID)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte("bad user id"))
|
||||
return
|
||||
}
|
||||
user, err := db.LoadUser(userIDint)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte(fmt.Sprintf("could not load user: %s", err.Error())))
|
||||
return
|
||||
}
|
||||
newContext := context.WithValue(r.Context(), userContextKey, user)
|
||||
r = r.Clone(newContext)
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user