package main import ( "flag" "fmt" "log" "net/http" "os" "git.chrishayward.xyz/x/users/gateway" "git.chrishayward.xyz/x/users/proto" "google.golang.org/grpc" "gopkg.in/yaml.v3" ) var configPath = flag.String("config", "./config/gateway.yaml", "--config=./config/gateway.yaml") type config struct { Port int `yaml:"port"` Domain string `yaml:"domain"` Server struct { Address string `yaml:"address"` Port int `yaml:"port"` Secret string `yaml:"secret"` } `yaml:"server"` } func main() { // Parse the optional flags. flag.Parse() // Read the config file. config := &config{} file, err := os.Open(*configPath) if err != nil { log.Fatal(err) } defer file.Close() if err := yaml.NewDecoder(file).Decode(&config); err != nil { log.Fatal(err) } // Create the connection to the server. conn, err := grpc.Dial( fmt.Sprintf("%s:%d", config.Server.Address, config.Server.Port), grpc.WithInsecure()) if err != nil { log.Fatal(err) } // Defer closing the connection. defer conn.Close() // Create the client. client := proto.NewUsersClient(conn) // Setup HTTP endpoints. http.HandleFunc("/register", gateway.Register(client)) http.HandleFunc("/login", gateway.Login(client)) http.HandleFunc("/logout", gateway.Authorize(client, config.Server.Secret, gateway.Logout(client))) http.HandleFunc("/reset_password", gateway.ResetPassword(client, fmt.Sprintf("%s, %d", config.Domain, config.Port))) http.HandleFunc("/change_password", gateway.ChangePassword(client)) // Listen for requests. log.Printf("Forwarding from :%d to %s:%d", config.Port, config.Server.Address, config.Server.Port) if err := http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil); err != nil { log.Fatal(err) } }