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
67 lines
1.7 KiB
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.yaml", "--config=config.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))
|
|
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)
|
|
}
|
|
}
|