move server auto keygen to app/main

This commit is contained in:
Dan Ballard 2018-05-03 15:45:50 -07:00
parent 4b27e95c26
commit aab5996971
2 changed files with 23 additions and 13 deletions

View File

@ -3,11 +3,33 @@ package main
import (
cwtchserver "git.mascherari.press/cwtch/server"
"log"
"os"
"github.com/s-rah/go-ricochet/utils"
"io/ioutil"
)
const privateKeyFile = "./private_key"
func checkAndGenPrivateKey(privateKeyFile string) {
if _, err := os.Stat(privateKeyFile); os.IsNotExist(err) {
log.Printf("no private key found!")
log.Printf("generating new private key...")
pk, pk_err := utils.GeneratePrivateKey()
if pk_err != nil {
log.Fatalf("error generating new private key: %v\n", err)
}
err := ioutil.WriteFile(privateKeyFile, []byte(utils.PrivateKeyToString(pk)), 0400)
if err != nil {
log.Fatalf("error writing new private key to file %s: %v\n", privateKeyFile, err)
}
}
}
func main() {
checkAndGenPrivateKey(privateKeyFile)
server := new(cwtchserver.Server)
log.Printf("starting cwtch server...")
server.Run("./private_key")
server.Run(privateKeyFile)
}

View File

@ -10,8 +10,6 @@ import (
"github.com/s-rah/go-ricochet/utils"
"log"
"os"
"io/ioutil"
)
type Server struct {
@ -20,16 +18,6 @@ type Server struct {
func (s *Server) Run(privateKeyFile string) {
cwtchserver := new(application.RicochetApplication)
if _, err := os.Stat(privateKeyFile); os.IsNotExist(err) {
log.Printf("no private key found!")
log.Printf("generating new private key...")
pk, pk_err := utils.GeneratePrivateKey()
if pk_err != nil {
log.Fatalf("error generating new private key: %v", err)
}
ioutil.WriteFile(privateKeyFile, []byte(utils.PrivateKeyToString(pk)), 0400)
}
pk, err := utils.LoadPrivateKeyFromFile(privateKeyFile)
if err != nil {