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.
121 lines
2.3 KiB
121 lines
2.3 KiB
package main
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"io/ioutil"
|
|
"log"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"sync"
|
|
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/credentials"
|
|
"src.whiteboxsystems.nl/decozo/okapi"
|
|
"src.whiteboxsystems.nl/decozo/okapidemo/cryptoutil"
|
|
)
|
|
|
|
var rpcPort = "9999"
|
|
var rpcAddr = "0.0.0.0:" + rpcPort
|
|
var extRpcAddr = "localhost:" + rpcPort
|
|
var uiAddr = "0.0.0.0:9095"
|
|
|
|
func loadCert() *tls.Certificate {
|
|
_, err := os.Stat("certs/client.crt")
|
|
|
|
if err != nil {
|
|
_, _, certPem, keyPem, err := cryptoutil.GenCert("dvza", "dvza")
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if err := ioutil.WriteFile("certs/client.crt", []byte(certPem), 0600); err != nil {
|
|
panic(err)
|
|
}
|
|
if err := ioutil.WriteFile("certs/client.key", []byte(keyPem), 0600); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
certificate, err := tls.LoadX509KeyPair("certs/client.crt", "certs/client.key")
|
|
|
|
if err != nil {
|
|
panic("Load client certification failed: " + err.Error())
|
|
}
|
|
|
|
return &certificate
|
|
}
|
|
|
|
func loadKeyPair() credentials.TransportCredentials {
|
|
certificate := loadCert()
|
|
|
|
tlsConfig := &tls.Config{
|
|
ClientAuth: tls.RequestClientCert,
|
|
Certificates: []tls.Certificate{*certificate},
|
|
}
|
|
return credentials.NewTLS(tlsConfig)
|
|
}
|
|
|
|
func main() {
|
|
stop := make(chan os.Signal, 1)
|
|
signal.Notify(stop, os.Interrupt)
|
|
wg := &sync.WaitGroup{}
|
|
|
|
openapisrv := NewServer()
|
|
openapisrv.LoadData("./data/data.db")
|
|
opts := []grpc.ServerOption{
|
|
grpc.Creds(loadKeyPair()),
|
|
}
|
|
|
|
grpcServer := grpc.NewServer(opts...)
|
|
|
|
if ext := os.Getenv("EXT_ADDR"); ext != "" {
|
|
extRpcAddr = ext + ":" + rpcPort
|
|
}
|
|
|
|
go func() {
|
|
lis, err := net.Listen("tcp", rpcAddr)
|
|
if err != nil {
|
|
log.Fatalf("failed to listen: %v", err)
|
|
}
|
|
|
|
okapi.RegisterOkAPIServer(grpcServer, openapisrv)
|
|
log.Printf("RPC Listening on %v", rpcAddr)
|
|
wg.Add(1)
|
|
grpcServer.Serve(lis)
|
|
}()
|
|
|
|
srv := NewUIServer(uiAddr)
|
|
srv.data = openapisrv.data
|
|
|
|
go func() {
|
|
wg.Add(1)
|
|
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
panic(err)
|
|
}
|
|
}()
|
|
|
|
<-stop
|
|
|
|
go func() {
|
|
grpcServer.GracefulStop()
|
|
wg.Done()
|
|
log.Println("Shutdown RPC server")
|
|
}()
|
|
|
|
go func() {
|
|
log.Println("Shutdown UI server...")
|
|
srv.Shutdown(context.Background())
|
|
wg.Done()
|
|
log.Println("UI Server shutdown...")
|
|
}()
|
|
|
|
wg.Wait()
|
|
}
|
|
|