From 4686a97c6ed12813d9cf7eea2aa5de142adfd630 Mon Sep 17 00:00:00 2001 From: Christopher James Hayward Date: Sat, 29 Jul 2023 14:45:24 -0400 Subject: [PATCH] Remove gateway --- cmd/gateway/main.go | 69 ---------------------- gateway/gateway.go | 136 -------------------------------------------- 2 files changed, 205 deletions(-) delete mode 100644 cmd/gateway/main.go delete mode 100644 gateway/gateway.go diff --git a/cmd/gateway/main.go b/cmd/gateway/main.go deleted file mode 100644 index 33737cc..0000000 --- a/cmd/gateway/main.go +++ /dev/null @@ -1,69 +0,0 @@ -package main - -import ( - "flag" - "fmt" - "log" - "net/http" - "os" - - "git.chrishayward.xyz/x/users/gateway" - "git.chrishayward.xyz/x/users/proto" - "google.golang.org/grpc" - "gopkg.in/yaml.v3" -) - -var configPath = flag.String("config", "./config/gateway.yaml", "--config=./config/gateway.yaml") - -type config struct { - Port int `yaml:"port"` - Domain string `yaml:"domain"` - Server struct { - Address string `yaml:"address"` - Port int `yaml:"port"` - Secret string `yaml:"secret"` - } `yaml:"server"` -} - -func main() { - // Parse the optional flags. - flag.Parse() - - // Read the config file. - config := &config{} - file, err := os.Open(*configPath) - if err != nil { - log.Fatal(err) - } - defer file.Close() - if err := yaml.NewDecoder(file).Decode(&config); err != nil { - log.Fatal(err) - } - - // Create the connection to the server. - conn, err := grpc.Dial( - fmt.Sprintf("%s:%d", config.Server.Address, config.Server.Port), - grpc.WithInsecure()) - if err != nil { - log.Fatal(err) - } - - // Defer closing the connection. - defer conn.Close() - - // Create the client. - client := proto.NewUsersClient(conn) - - // Setup HTTP endpoints. - http.HandleFunc("/register", gateway.Register(client)) - http.HandleFunc("/login", gateway.Login(client)) - http.HandleFunc("/logout", gateway.Authorize(client, config.Server.Secret, gateway.Logout(client))) - http.HandleFunc("/reset_password", gateway.ResetPassword(client, fmt.Sprintf("%s, %d", config.Domain, config.Port))) - http.HandleFunc("/change_password", gateway.ChangePassword(client)) - - // Listen for requests. - log.Printf("Forwarding from :%d to %s:%d", config.Port, config.Server.Address, config.Server.Port) - if err := http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil); err != nil { - log.Fatal(err) - } -} diff --git a/gateway/gateway.go b/gateway/gateway.go deleted file mode 100644 index 3366d66..0000000 --- a/gateway/gateway.go +++ /dev/null @@ -1,136 +0,0 @@ -package gateway - -import ( - "context" - "encoding/json" - "fmt" - "net/http" - - "git.chrishayward.xyz/x/users/proto" -) - -func Register(client proto.UsersClient) http.HandlerFunc { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - var req proto.RegisterRequest - if err := json.NewDecoder(r.Body).Decode(&req); err != nil { - w.WriteHeader(http.StatusBadRequest) - w.Write([]byte(err.Error())) - return - } - - if _, err := client.Register(r.Context(), &req); err != nil { - w.WriteHeader(http.StatusBadRequest) - w.Write([]byte(err.Error())) - return - } - - w.WriteHeader(http.StatusOK) - }) -} - -func Login(client proto.UsersClient) http.HandlerFunc { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - var req proto.LoginRequest - if err := json.NewDecoder(r.Body).Decode(&req); err != nil { - w.Write([]byte(err.Error())) - w.WriteHeader(http.StatusBadRequest) - return - } - - res, err := client.Login(r.Context(), &req) - if err != nil { - w.Write([]byte(err.Error())) - w.WriteHeader(http.StatusBadRequest) - return - } - - w.Write([]byte(res.Token.Token)) - w.WriteHeader(http.StatusOK) - }) -} - -func Logout(client proto.UsersClient) http.HandlerFunc { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if _, err := client.Logout(r.Context(), &proto.LogoutRequest{ - Token: r.Context().Value("token").(*proto.UserToken), - }); err != nil { - w.Write([]byte(err.Error())) - w.WriteHeader(http.StatusBadRequest) - return - } - - w.WriteHeader(http.StatusOK) - }) -} - -func Authorize(client proto.UsersClient, secret string, next http.HandlerFunc) http.HandlerFunc { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - var req proto.AuthorizeRequest - if err := json.NewDecoder(r.Body).Decode(&req); err != nil { - w.Write([]byte(err.Error())) - w.WriteHeader(http.StatusBadRequest) - return - } - - res, err := client.Authorize(r.Context(), &proto.AuthorizeRequest{ - Secret: secret, - Token: req.Token, - }) - - if err != nil { - w.Write([]byte(err.Error())) - w.WriteHeader(http.StatusBadRequest) - return - } - - ctx := context.WithValue(r.Context(), "user", res.User) - ctx = context.WithValue(ctx, "roles", res.Roles) - ctx = context.WithValue(ctx, "token", &proto.UserToken{ - Token: req.Token.Token, - }) - - next(w, r.WithContext(ctx)) - }) -} - -func ResetPassword(client proto.UsersClient, endpoint string) http.HandlerFunc { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - var req proto.ResetPasswordRequest - if err := json.NewDecoder(r.Body).Decode(&req); err != nil { - w.Write([]byte(err.Error())) - w.WriteHeader(http.StatusBadRequest) - return - } - - res, err := client.ResetPassword(r.Context(), &req) - if err != nil { - w.Write([]byte(err.Error())) - w.WriteHeader(http.StatusBadRequest) - return - } - - w.Write([]byte(fmt.Sprintf( - "Please follow this link to update your password: %s/change_password?token=%s\n", - endpoint, res.Token.Token))) - w.WriteHeader(http.StatusOK) - }) -} - -func ChangePassword(client proto.UsersClient) http.HandlerFunc { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - var req proto.ChangePasswordRequest - if err := json.NewDecoder(r.Body).Decode(&req); err != nil { - w.Write([]byte(err.Error())) - w.WriteHeader(http.StatusBadRequest) - return - } - - if _, err := client.ChangePassword(r.Context(), &req); err != nil { - w.Write([]byte(err.Error())) - w.WriteHeader(http.StatusBadRequest) - return - } - - w.WriteHeader(http.StatusOK) - }) -}