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.

48 lines
1.3 KiB

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