jouney continue

This commit is contained in:
linhdevtran99 committed 2023-12-05 23:57:15 +07:00
1 parent 3869d24200
commit 0885214afc
3 files changed
+63 -13

No files matched your search

+51
View File
@@ -0,0 +1,51 @@
package routes
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"net/http"
)
type AuthRouter struct{}
func WriteJSON(w http.ResponseWriter, status int, v any) error {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
return json.NewEncoder(w).Encode(v)
}
type ApiError struct {
Error string
}
type apiFunc func(http.ResponseWriter, *http.Request) error
func MakeHTTPHandlerFn(fn apiFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if err := fn(w, r); err != nil {
if err := WriteJSON(w, http.StatusInternalServerError, ApiError{Error: err.Error()}); err != nil {
fmt.Print(err)
}
}
}
}
func RegisterNewAccount(w http.ResponseWriter, r *http.Request) error {
if r.Method == http.MethodGet {
fmt.Println("API Route Healthy")
}
if r.Method == http.MethodPost {
fmt.Println("method Post")
}
return nil
}
func AuthRouterSetup(router *mux.Router) {
authRouter := router.PathPrefix("/account").Subrouter()
authRouter.Handle("/register", MakeHTTPHandlerFn(RegisterNewAccount)).Methods("GET")
linh := authRouter.Handle("/register", MakeHTTPHandlerFn(RegisterNewAccount)).GetError()
fmt.Println(linh)
}