member_gateway_rpc/main.go

89 lines
1.8 KiB
Go
Raw Normal View History

2018-04-06 13:01:28 +00:00
package main
import (
2018-04-09 13:37:03 +00:00
"context"
"flag"
2018-04-06 13:01:28 +00:00
"log"
"os"
"os/signal"
"syscall"
"time"
2018-04-09 13:37:03 +00:00
2018-04-09 12:55:27 +00:00
"git.loafle.net/commons/configuration-go"
2018-04-09 13:37:03 +00:00
"git.loafle.net/commons/logging-go"
"git.loafle.net/overflow/member_gateway_rpc/config"
"git.loafle.net/overflow/member_gateway_rpc/server"
2018-04-11 13:08:56 +00:00
"io/ioutil"
"github.com/dgrijalva/jwt-go"
"path"
2018-04-09 12:55:27 +00:00
)
var (
2018-04-09 13:37:03 +00:00
configDir *string
2018-04-06 13:01:28 +00:00
)
func init() {
2018-04-09 12:55:27 +00:00
configDir = flag.String("config-dir", "./", "Config directory")
logConfigPath := flag.String("log-config", "", "logging config path")
flag.Parse()
logging.InitializeLogger(*logConfigPath)
2018-04-06 13:01:28 +00:00
}
func main() {
2018-04-10 06:03:15 +00:00
_config := &config.Config{}
2018-04-09 12:55:27 +00:00
configuration.SetConfigPath(*configDir)
2018-04-10 06:03:15 +00:00
if err := configuration.Load(_config, "config.json"); nil != err {
2018-04-09 12:55:27 +00:00
logging.Logger().Panic(err)
}
2018-04-11 13:08:56 +00:00
loadKey(*configDir, _config)
2018-04-09 12:55:27 +00:00
2018-04-11 13:08:56 +00:00
s := server.New(_config)
2018-04-06 13:01:28 +00:00
go func() {
err := s.ListenAndServe()
if nil != err {
log.Printf("err: %v", err)
}
}()
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt,
syscall.SIGKILL,
syscall.SIGSTOP,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
<-interrupt
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := s.Shutdown(ctx); err != nil {
logging.Logger().Errorf("error: %v", err)
}
}
2018-04-11 13:08:56 +00:00
func loadKey(configDir string, _config *config.Config) {
signBytes, err := ioutil.ReadFile(path.Join(configDir, "overFlow-private.key"))
if nil != err {
logging.Logger().Panic(err)
}
_config.SignKey, err = jwt.ParseRSAPrivateKeyFromPEM(signBytes)
if nil != err {
logging.Logger().Panic(err)
}
verifyBytes, err := ioutil.ReadFile(path.Join(configDir, "overFlow-public.pem"))
if nil != err {
logging.Logger().Panic(err)
}
_config.VerifyKey, err = jwt.ParseRSAPublicKeyFromPEM(verifyBytes)
if nil != err {
logging.Logger().Panic(err)
}
}