You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.3 KiB

1 year ago
1 year ago
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. "git.chrishayward.xyz/x/users/gateway"
  8. "git.chrishayward.xyz/x/users/proto"
  9. "google.golang.org/grpc"
  10. )
  11. var (
  12. port = flag.Int("port", 8081, "--port=8081")
  13. serverAddr = flag.String("serverAddr", "localhost", "--serverAddr=localhost")
  14. serverPort = flag.Int("serverPort", 8080, "--serverPort=8080")
  15. serverSecret = flag.String("serverSecret", "...", "--serverSecret=...")
  16. )
  17. func main() {
  18. // Parse the optional flags.
  19. flag.Parse()
  20. // Create the connection to the server.
  21. conn, err := grpc.Dial(fmt.Sprintf("%s:%d", *serverAddr, *serverPort))
  22. if err != nil {
  23. log.Fatal(err)
  24. }
  25. // Defer closing the connection.
  26. defer conn.Close()
  27. // Create the client.
  28. client := proto.NewUsersClient(conn)
  29. // Setup HTTP endpoints.
  30. http.HandleFunc("/register", gateway.Register(client))
  31. http.HandleFunc("/login", gateway.Login(client))
  32. http.HandleFunc("/authorize", gateway.Authorize(client, serverSecret))
  33. http.HandleFunc("/reset_password", gateway.ResetPassword(client, port))
  34. http.HandleFunc("/change_password", gateway.ChangePassword(client))
  35. // Listen for requests.
  36. log.Printf("Forwarding from :%d to %s:%d", *port, *serverAddr, *serverPort)
  37. if err := http.ListenAndServe(fmt.Sprintf(":%d", *port), nil); err != nil {
  38. log.Fatal(err)
  39. }
  40. }