code on mac
This commit is contained in:
parent
0885214afc
commit
ce0508294c
4
.env
4
.env
|
|
@ -1,5 +1,5 @@
|
||||||
MONGO_URI="mongodb://adminLinh:linhporo1@10.10.0.216:27017/linhporo1?authSource=admin"
|
MONGO_URI="mongodb://adminLinh:linhporo1@localhost:27017"
|
||||||
API_TEST_PORT=":8080"
|
API_TEST_PORT=":8080"
|
||||||
REDIS_URI="redis://10.10.0.216:6379/0"
|
REDIS_URI="redis://localhost:6479/0"
|
||||||
STMP_PASS="btmp judz ebys pfxw"
|
STMP_PASS="btmp judz ebys pfxw"
|
||||||
EMAIL_VERIFY_SECRET="WDc&4+&vYP(n'}?LHNE#5M?IE|g(c812"
|
EMAIL_VERIFY_SECRET="WDc&4+&vYP(n'}?LHNE#5M?IE|g(c812"
|
||||||
|
|
@ -1,15 +1,9 @@
|
||||||
package rest_api
|
package rest_api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"linhdevtran99/rest-api/models"
|
|
||||||
"linhdevtran99/rest-api/rest-api/routes"
|
"linhdevtran99/rest-api/rest-api/routes"
|
||||||
"linhdevtran99/rest-api/rest-api/services"
|
|
||||||
"linhdevtran99/rest-api/utils"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
@ -18,30 +12,6 @@ type APIServer struct {
|
||||||
listenAddr string
|
listenAddr string
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// define function apiFn
|
|
||||||
type apiFunc func(http.ResponseWriter, *http.Request) error
|
|
||||||
|
|
||||||
// makeHTTPHandlerFn fn
|
|
||||||
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 NewAPIServer(listenAddr string) *APIServer {
|
func NewAPIServer(listenAddr string) *APIServer {
|
||||||
return &APIServer{
|
return &APIServer{
|
||||||
listenAddr: listenAddr,
|
listenAddr: listenAddr,
|
||||||
|
|
@ -59,21 +29,12 @@ func startMuxServer(s *APIServer, router *mux.Router) {
|
||||||
func (s *APIServer) Run() {
|
func (s *APIServer) Run() {
|
||||||
router := mux.NewRouter()
|
router := mux.NewRouter()
|
||||||
|
|
||||||
//authRouter := router.PathPrefix("/").Subrouter()
|
|
||||||
|
|
||||||
routes.AuthRouterSetup(router)
|
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) AuthRoute(w http.ResponseWriter, r *http.Request) error {
|
func (s *APIServer) AuthRoute(w http.ResponseWriter, r *http.Request) error {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
client := utils.MongoDB
|
|
||||||
|
|
||||||
if r.Method == http.MethodGet {
|
if r.Method == http.MethodGet {
|
||||||
fmt.Println("hit")
|
fmt.Println("hit")
|
||||||
|
|
@ -115,49 +76,5 @@ func (s *APIServer) AuthRoute(w http.ResponseWriter, r *http.Request) error {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.Method == http.MethodPost {
|
|
||||||
var registerInfo models.CreateUser
|
|
||||||
|
|
||||||
_ = json.NewDecoder(r.Body).Decode(®isterInfo)
|
|
||||||
//call function check info user type in
|
|
||||||
validRegisterInfo := services.CheckAndValidRegisterFiled(®isterInfo, w)
|
|
||||||
//call function check data user use in past or not
|
|
||||||
isValidData := services.CheckAccountExist(client, registerInfo.Username, registerInfo.Email, w)
|
|
||||||
|
|
||||||
if isValidData == false || validRegisterInfo == false {
|
|
||||||
return errors.New("USER DON'T HAVE VALID INFO FOR REGISTER ACCOUNT")
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *APIServer) handleAccount(w http.ResponseWriter, r *http.Request) error {
|
|
||||||
if r.Method == http.MethodGet {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
res, err := utils.Redis.Ping(ctx).Result()
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(res)
|
|
||||||
|
|
||||||
}
|
|
||||||
if r.Method == http.MethodPost {
|
|
||||||
fmt.Println("POST")
|
|
||||||
}
|
|
||||||
if r.Method == http.MethodDelete {
|
|
||||||
fmt.Println("DELETE")
|
|
||||||
}
|
|
||||||
if r.Method == http.MethodPut {
|
|
||||||
fmt.Println("PUT")
|
|
||||||
}
|
|
||||||
if r.Method == http.MethodPatch {
|
|
||||||
fmt.Println("PATCH")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,50 +2,34 @@ package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"errors"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
"linhdevtran99/rest-api/models"
|
||||||
|
"linhdevtran99/rest-api/rest-api/services"
|
||||||
|
"linhdevtran99/rest-api/utils"
|
||||||
"net/http"
|
"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 {
|
func RegisterNewAccount(w http.ResponseWriter, r *http.Request) error {
|
||||||
if r.Method == http.MethodGet {
|
client := utils.MongoDB
|
||||||
fmt.Println("API Route Healthy")
|
|
||||||
}
|
|
||||||
|
|
||||||
if r.Method == http.MethodPost {
|
if r.Method == http.MethodPost {
|
||||||
fmt.Println("method Post")
|
var registerInfo models.CreateUser
|
||||||
|
|
||||||
|
_ = json.NewDecoder(r.Body).Decode(®isterInfo)
|
||||||
|
//call function check info user type in
|
||||||
|
validRegisterInfo := services.CheckAndValidRegisterFiled(®isterInfo, w)
|
||||||
|
//call function check data user use in past or not
|
||||||
|
isValidData := services.CheckAccountExist(client, registerInfo.Username, registerInfo.Email, w)
|
||||||
|
|
||||||
|
if isValidData == false || validRegisterInfo == false {
|
||||||
|
return errors.New("USER DON'T HAVE VALID INFO FOR REGISTER ACCOUNT")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func AuthRouterSetup(router *mux.Router) {
|
func AuthRouterSetup(router *mux.Router) {
|
||||||
authRouter := router.PathPrefix("/account").Subrouter()
|
authRouter := router.PathPrefix("/account").Subrouter()
|
||||||
authRouter.Handle("/register", MakeHTTPHandlerFn(RegisterNewAccount)).Methods("GET")
|
authRouter.Handle("/register", utils.MakeHTTPHandlerFn(RegisterNewAccount)).Methods("POST")
|
||||||
linh := authRouter.Handle("/register", MakeHTTPHandlerFn(RegisterNewAccount)).GetError()
|
|
||||||
fmt.Println(linh)
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,31 @@
|
||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// define function apiFn
|
||||||
|
type apiFunc func(http.ResponseWriter, *http.Request) error
|
||||||
|
|
||||||
|
// makeHTTPHandlerFn fn
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue