package main

import (
	"database/sql"
	"fmt"
	"google.golang.org/grpc"
	"google.golang.org/grpc/reflection"
	client "iaun.cn/linux/services/client"
	"log"
	"net"
	"net/http"
)

const (
	port = ":30001"
)

var db *sql.DB

//type Greeter struct {
//	service.UnimplementedGreeterServer
//}
//
//func (s *Greeter) SayHello(ctx context.Context, in *service.HelloRequest) (*service.HelloReply, error) {
//	fmt.Println(in.GetName())
//	return &service.HelloReply{Message: "return message " + in.GetName()}, nil
//}

func rpcServer(s *grpc.Server, lis net.Listener) {
	if err := s.Serve(lis); err != nil {
		log.Fatalf("failed to serve: %v", err)
	}
}

func main() {
	var err error
	db, err = sqlOpen()
	if err != nil {
		return
	}

	lis, err := net.Listen("tcp", port)
	if err != nil {
		log.Fatalf("failed to listen: %v", err)
		return
	}
	s := grpc.NewServer()
	client.RegisterClientServer(s, &Client{})
	reflection.Register(s)
	go rpcServer(s, lis)
	httpHandle()
	fmt.Println("http server started")
	_ = http.ListenAndServe("0.0.0.0:808", nil)
}
