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.

57 lines
1.5 KiB

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. domain = flag.String("domain", "http://localhost", "--domain=localhost")
  14. serverAddr = flag.String("serverAddr", "localhost", "--serverAddr=localhost")
  15. serverPort = flag.Int("serverPort", 8080, "--serverPort=8080")
  16. serverSecret = flag.String("serverSecret", "...", "--serverSecret=...")
  17. )
  18. func authorized(f http.HandlerFunc) http.HandlerFunc {
  19. return func(w http.ResponseWriter, r *http.Request) {
  20. }
  21. }
  22. func main() {
  23. // Parse the optional flags.
  24. flag.Parse()
  25. // Create the connection to the server.
  26. conn, err := grpc.Dial(fmt.Sprintf("%s:%d", *serverAddr, *serverPort))
  27. if err != nil {
  28. log.Fatal(err)
  29. }
  30. // Defer closing the connection.
  31. defer conn.Close()
  32. // Create the client.
  33. client := proto.NewUsersClient(conn)
  34. // Setup HTTP endpoints.
  35. http.HandleFunc("/register", gateway.Register(client))
  36. http.HandleFunc("/login", gateway.Login(client))
  37. http.HandleFunc("/reset_password", gateway.Authorize(client, serverSecret,
  38. gateway.ResetPassword(client, fmt.Sprintf("%s, %d", *domain, *port))))
  39. http.HandleFunc("/change_password", gateway.Authorize(client, serverSecret,
  40. gateway.ChangePassword(client)))
  41. // Listen for requests.
  42. log.Printf("Forwarding from :%d to %s:%d", *port, *serverAddr, *serverPort)
  43. if err := http.ListenAndServe(fmt.Sprintf(":%d", *port), nil); err != nil {
  44. log.Fatal(err)
  45. }
  46. }