2019-04-15 03:56:55 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-05-06 00:56:27 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2019-04-15 03:56:55 +00:00
|
|
|
"flag"
|
|
|
|
"io"
|
2019-05-06 00:56:27 +00:00
|
|
|
stdlog "log"
|
2019-04-15 03:56:55 +00:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2019-05-22 15:22:40 +00:00
|
|
|
"syscall"
|
2019-04-15 03:56:55 +00:00
|
|
|
|
|
|
|
"github.com/go-kit/kit/log"
|
2019-05-22 15:22:40 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials"
|
|
|
|
|
2019-07-11 02:33:22 +00:00
|
|
|
"github.com/mitchell/selfpass/services/credentials/middleware"
|
|
|
|
"github.com/mitchell/selfpass/services/credentials/protobuf"
|
|
|
|
"github.com/mitchell/selfpass/services/credentials/repositories"
|
|
|
|
"github.com/mitchell/selfpass/services/credentials/service"
|
|
|
|
"github.com/mitchell/selfpass/services/credentials/transport"
|
|
|
|
"github.com/mitchell/selfpass/services/credentials/types"
|
2019-04-15 03:56:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var logger log.Logger
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var (
|
2019-05-28 01:16:50 +00:00
|
|
|
stop = make(chan os.Signal, 1)
|
|
|
|
jsonLogs = flag.Bool("json-logs", false, "enables json logging")
|
|
|
|
port = flag.String("port", "8080", "specify the port to listen on")
|
|
|
|
verbose = flag.Bool("v", false, "be more verbose")
|
2019-04-15 03:56:55 +00:00
|
|
|
)
|
|
|
|
flag.Parse()
|
|
|
|
|
2019-05-22 15:22:40 +00:00
|
|
|
signal.Notify(stop, syscall.SIGINT)
|
|
|
|
signal.Notify(stop, syscall.SIGKILL)
|
|
|
|
signal.Notify(stop, syscall.SIGTERM)
|
|
|
|
|
2019-05-28 01:16:50 +00:00
|
|
|
logger = newLogger(os.Stdout, *jsonLogs)
|
2019-04-15 03:56:55 +00:00
|
|
|
|
2019-05-28 01:16:50 +00:00
|
|
|
keypair, err := tls.X509KeyPair([]byte(cert), []byte(key))
|
2019-05-06 00:56:27 +00:00
|
|
|
check(err)
|
|
|
|
|
|
|
|
caPool := x509.NewCertPool()
|
2019-05-28 01:16:50 +00:00
|
|
|
caPool.AppendCertsFromPEM([]byte(ca))
|
2019-05-06 00:56:27 +00:00
|
|
|
|
|
|
|
creds := credentials.NewTLS(&tls.Config{
|
2019-06-07 09:03:15 +00:00
|
|
|
Certificates: []tls.Certificate{keypair},
|
|
|
|
ClientCAs: caPool,
|
|
|
|
ClientAuth: tls.RequireAndVerifyClientCert,
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
PreferServerCipherSuites: true,
|
|
|
|
CurvePreferences: []tls.CurveID{
|
|
|
|
tls.CurveP256,
|
|
|
|
},
|
2019-05-06 00:56:27 +00:00
|
|
|
})
|
|
|
|
|
2019-05-22 15:22:40 +00:00
|
|
|
db, err := repositories.NewRedisConn("tcp", "redis:6379", 2)
|
2019-05-06 00:56:27 +00:00
|
|
|
check(err)
|
|
|
|
|
|
|
|
var svc types.Service
|
|
|
|
svc = service.NewCredentials(db)
|
|
|
|
if *verbose {
|
|
|
|
svc = middleware.NewServiceLogger(logger, svc)
|
|
|
|
}
|
|
|
|
|
|
|
|
gsrv := transport.NewGRPCServer(svc, logger)
|
|
|
|
srv := grpc.NewServer(grpc.Creds(creds))
|
2019-04-15 03:56:55 +00:00
|
|
|
protobuf.RegisterCredentialServiceServer(srv, gsrv)
|
|
|
|
|
2019-05-06 00:56:27 +00:00
|
|
|
addr := ":" + *port
|
2019-04-15 03:56:55 +00:00
|
|
|
lis, err := net.Listen("tcp", addr)
|
|
|
|
check(err)
|
|
|
|
|
2019-05-06 00:56:27 +00:00
|
|
|
_ = logger.Log(
|
|
|
|
"message", "serving",
|
|
|
|
"address", addr,
|
|
|
|
)
|
|
|
|
|
|
|
|
go func() { check(srv.Serve(lis)) }()
|
2019-04-15 03:56:55 +00:00
|
|
|
|
|
|
|
<-stop
|
2019-05-06 00:56:27 +00:00
|
|
|
_ = logger.Log("message", "gracefully stopping")
|
2019-04-15 03:56:55 +00:00
|
|
|
srv.GracefulStop()
|
|
|
|
}
|
|
|
|
|
2019-05-28 01:16:50 +00:00
|
|
|
func newLogger(writer io.Writer, jsonLogs bool) log.Logger {
|
2019-04-15 03:56:55 +00:00
|
|
|
writer = log.NewSyncWriter(writer)
|
2019-05-28 01:16:50 +00:00
|
|
|
l := log.NewLogfmtLogger(writer)
|
2019-04-15 03:56:55 +00:00
|
|
|
|
2019-05-28 01:16:50 +00:00
|
|
|
if jsonLogs {
|
2019-04-15 03:56:55 +00:00
|
|
|
l = log.NewJSONLogger(writer)
|
|
|
|
}
|
2019-05-06 00:56:27 +00:00
|
|
|
l = log.WithPrefix(l, "caller", log.Caller(5), "timestamp", log.DefaultTimestamp)
|
2019-04-15 03:56:55 +00:00
|
|
|
|
|
|
|
lfunc := log.LoggerFunc(func(in ...interface{}) error {
|
|
|
|
if err := l.Log(in...); err != nil {
|
2019-05-06 00:56:27 +00:00
|
|
|
stdlog.Println(err)
|
2019-04-15 03:56:55 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return lfunc
|
|
|
|
}
|
|
|
|
|
|
|
|
func check(err error) {
|
|
|
|
if err != nil {
|
2019-05-06 00:56:27 +00:00
|
|
|
_ = logger.Log("error", err)
|
2019-04-15 03:56:55 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|