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.

67 lines
1.7 KiB

1 year ago
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. "os"
  8. "git.chrishayward.xyz/x/users/gateway"
  9. "git.chrishayward.xyz/x/users/proto"
  10. "google.golang.org/grpc"
  11. "gopkg.in/yaml.v3"
  12. )
  13. var configPath = flag.String("config", "./config/gateway.yaml", "--config=./config/gateway.yaml")
  14. type config struct {
  15. Port int `yaml:"port"`
  16. Domain string `yaml:"domain"`
  17. Server struct {
  18. Address string `yaml:"address"`
  19. Port int `yaml:"port"`
  20. Secret string `yaml:"secret"`
  21. } `yaml:"server"`
  22. }
  23. func main() {
  24. // Parse the optional flags.
  25. flag.Parse()
  26. // Read the config file.
  27. config := &config{}
  28. file, err := os.Open(*configPath)
  29. if err != nil {
  30. log.Fatal(err)
  31. }
  32. defer file.Close()
  33. if err := yaml.NewDecoder(file).Decode(&config); err != nil {
  34. log.Fatal(err)
  35. }
  36. // Create the connection to the server.
  37. conn, err := grpc.Dial(fmt.Sprintf("%s:%d", config.Server.Address, config.Server.Port))
  38. if err != nil {
  39. log.Fatal(err)
  40. }
  41. // Defer closing the connection.
  42. defer conn.Close()
  43. // Create the client.
  44. client := proto.NewUsersClient(conn)
  45. // Setup HTTP endpoints.
  46. http.HandleFunc("/register", gateway.Register(client))
  47. http.HandleFunc("/login", gateway.Login(client))
  48. http.HandleFunc("/logout", gateway.Authorize(client, &config.Server.Secret, gateway.Logout(client)))
  49. http.HandleFunc("/reset_password", gateway.ResetPassword(client, fmt.Sprintf("%s, %d", config.Domain, config.Port)))
  50. http.HandleFunc("/change_password", gateway.ChangePassword(client))
  51. // Listen for requests.
  52. log.Printf("Forwarding from :%d to %s:%d", config.Port, config.Server.Address, config.Server.Port)
  53. if err := http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil); err != nil {
  54. log.Fatal(err)
  55. }
  56. }