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.

69 lines
1.7 KiB

1 year ago
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(
  38. fmt.Sprintf("%s:%d", config.Server.Address, config.Server.Port),
  39. grpc.WithInsecure())
  40. if err != nil {
  41. log.Fatal(err)
  42. }
  43. // Defer closing the connection.
  44. defer conn.Close()
  45. // Create the client.
  46. client := proto.NewUsersClient(conn)
  47. // Setup HTTP endpoints.
  48. http.HandleFunc("/register", gateway.Register(client))
  49. http.HandleFunc("/login", gateway.Login(client))
  50. http.HandleFunc("/logout", gateway.Authorize(client, config.Server.Secret, gateway.Logout(client)))
  51. http.HandleFunc("/reset_password", gateway.ResetPassword(client, fmt.Sprintf("%s, %d", config.Domain, config.Port)))
  52. http.HandleFunc("/change_password", gateway.ChangePassword(client))
  53. // Listen for requests.
  54. log.Printf("Forwarding from :%d to %s:%d", config.Port, config.Server.Address, config.Server.Port)
  55. if err := http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil); err != nil {
  56. log.Fatal(err)
  57. }
  58. }