Skip to content
Snippets Groups Projects
main.go 2.19 KiB
Newer Older
Viktor Popov's avatar
Viktor Popov committed
/*
Copyright (c) 2018 Vereign AG [https://www.vereign.com]

This is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package main

import (
	"log"

	"code.vereign.com/code/key-storage-agent/server"
Olgun Cengiz's avatar
Olgun Cengiz committed
	"github.com/spf13/viper"
Viktor Popov's avatar
Viktor Popov committed
)

// main start a gRPC server and waits for connection
func main() {
Olgun Cengiz's avatar
Olgun Cengiz committed
	server.SetConfigValues()
Viktor Popov's avatar
Viktor Popov committed

	// TODO this should be done via configuration or even a certificate repository
Olgun Cengiz's avatar
Olgun Cengiz committed
	certDir := viper.GetString("certDir")
Viktor Popov's avatar
Viktor Popov committed
	if certDir == "" {
		log.Printf("cert-dir cannot be empty")
		return
	}

	grpcAddress := viper.GetString("grpcListenAddress")
	restAddress := viper.GetString("restListenAddress")
	dataStorageAddress := viper.GetString("dataStorageUrl")
Viktor Popov's avatar
Viktor Popov committed

Olgun Cengiz's avatar
Olgun Cengiz committed
	certFilePath := certDir + "/" + viper.GetString("certFile")
	privateKeyFilePath := certDir + "/" + viper.GetString("certKey")
	caCertFilePath := certDir + "/" + viper.GetString("caCertFile")
Olgun Cengiz's avatar
Olgun Cengiz committed
	vereignCertFilePath := certDir + "/" + viper.GetString("vereignCertFile")
	vereignPrivateKeyFilePath := certDir + "/" + viper.GetString("vereignCertKey")
Viktor Popov's avatar
Viktor Popov committed

Olgun Cengiz's avatar
Olgun Cengiz committed
	maxMessageSize := viper.GetInt("maxMessageSize")

Viktor Popov's avatar
Viktor Popov committed
	// fire the gRPC server in a goroutine
	go func() {
		err := server.StartGRPCServer(grpcAddress, certFilePath, privateKeyFilePath, caCertFilePath, vereignCertFilePath,
Olgun Cengiz's avatar
Olgun Cengiz committed
			vereignPrivateKeyFilePath, dataStorageAddress, maxMessageSize)
Viktor Popov's avatar
Viktor Popov committed
		if err != nil {
			log.Fatalf("failed to start gRPC server: %s", err)
		}
	}()

	// fire the REST server in a goroutine
	go func() {
		err := server.StartRESTServer(restAddress, grpcAddress, certFilePath)
Viktor Popov's avatar
Viktor Popov committed
		if err != nil {
			log.Fatalf("failed to start gRPC server: %s", err)
		}
	}()

	// infinite loop
	log.Printf("Entering infinite loop")
	select {}
}