jouney continue
This commit is contained in:
parent
3869d24200
commit
0885214afc
|
|
@ -0,0 +1 @@
|
||||||
|
package controler
|
||||||
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"linhdevtran99/rest-api/models"
|
"linhdevtran99/rest-api/models"
|
||||||
|
"linhdevtran99/rest-api/rest-api/routes"
|
||||||
"linhdevtran99/rest-api/rest-api/services"
|
"linhdevtran99/rest-api/rest-api/services"
|
||||||
"linhdevtran99/rest-api/utils"
|
"linhdevtran99/rest-api/utils"
|
||||||
"log"
|
"log"
|
||||||
|
|
@ -31,7 +32,7 @@ type ApiError struct {
|
||||||
type apiFunc func(http.ResponseWriter, *http.Request) error
|
type apiFunc func(http.ResponseWriter, *http.Request) error
|
||||||
|
|
||||||
// makeHTTPHandlerFn fn
|
// makeHTTPHandlerFn fn
|
||||||
func makeHTTPHandlerFn(fn apiFunc) http.HandlerFunc {
|
func MakeHTTPHandlerFn(fn apiFunc) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
if err := fn(w, r); err != nil {
|
if err := fn(w, r); err != nil {
|
||||||
if err := WriteJSON(w, http.StatusInternalServerError, ApiError{Error: err.Error()}); err != nil {
|
if err := WriteJSON(w, http.StatusInternalServerError, ApiError{Error: err.Error()}); err != nil {
|
||||||
|
|
@ -58,22 +59,19 @@ func startMuxServer(s *APIServer, router *mux.Router) {
|
||||||
func (s *APIServer) Run() {
|
func (s *APIServer) Run() {
|
||||||
router := mux.NewRouter()
|
router := mux.NewRouter()
|
||||||
|
|
||||||
router.HandleFunc("/account/register", makeHTTPHandlerFn(s.registerNewAccount))
|
//authRouter := router.PathPrefix("/").Subrouter()
|
||||||
router.HandleFunc("/account", makeHTTPHandlerFn(s.handleAccount))
|
|
||||||
router.HandleFunc("/test", makeHTTPHandlerFn(s.testCheckUserAndPass))
|
routes.AuthRouterSetup(router)
|
||||||
|
|
||||||
|
//authRouter.HandleFunc("/account", MakeHTTPHandlerFn(s.AuthRoute))
|
||||||
|
|
||||||
|
//router.HandleFunc("/account/register", MakeHTTPHandlerFn(s.RegisterNewAccount))
|
||||||
|
//router.HandleFunc("/account/register", MakeHTTPHandlerFn(s.AuthRoute))
|
||||||
|
|
||||||
startMuxServer(s, router)
|
startMuxServer(s, router)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *APIServer) registerNewAccount(w http.ResponseWriter, r *http.Request) error {
|
func (s *APIServer) AuthRoute(w http.ResponseWriter, r *http.Request) error {
|
||||||
if r.Method == http.MethodGet {
|
|
||||||
fmt.Println("API Route Healthy")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *APIServer) testCheckUserAndPass(w http.ResponseWriter, r *http.Request) error {
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
client := utils.MongoDB
|
client := utils.MongoDB
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue