func startServer() { log.Println("starting server") var redisErr error r, redisErr = redis.NewClient(*redisHost, *redisDB) util.CheckError(redisErr, shutdown) sExists, sErr := r.KeyExists("secret") util.CheckError(sErr, shutdown) if !sExists { log.Println("running server for the first time") setErr := r.GenerateGlobalSecret() util.CheckError(setErr, shutdown) } getErr := r.RetrieveGlobalSecret() util.CheckError(getErr, shutdown) var lErr error l, lErr = tcp.NewListener(*tcpPort, nil) util.CheckError(lErr, shutdown) srv = server.New("/register", "/validate", "/file", *httpPort, r, l) startErr := srv.Start() util.CheckError(startErr, shutdown) l.Start() }
func startDaemon() { log.Println("starting daemon") configPath := os.Getenv("HOME") + "/.togepi/data" configStat, configStatErr := os.Stat(configPath) md.ConfPath = configPath switch { case os.IsNotExist(configStatErr): log.Println("first start, generating configuration") resp, respErr := http.Get("http://" + *httpServerAddress + "/register") util.CheckError(respErr, shutdown) body, bodyErr := ioutil.ReadAll(resp.Body) util.CheckError(bodyErr, shutdown) resp.Body.Close() var respStruct server.RegResp jsonRespErr := json.Unmarshal(body, &respStruct) util.CheckError(jsonRespErr, shutdown) md.SetUserData(respStruct.UserID, respStruct.UserKey) md.SetServerData(*tcpServerAddress, *httpServerAddress) dataErr := md.CreateDataFile(configPath) util.CheckError(dataErr, shutdown) case configStat.IsDir(): util.CheckError(errors.New(configPath+" is a directory"), shutdown) default: readDataErr := md.ReadDataFile(configPath) util.CheckError(readDataErr, shutdown) *httpServerAddress = md.HTTPServer *tcpServerAddress = md.TCPServer resp, respErr := http.Get("http://" + *httpServerAddress + "/validate?uid=" + md.UserID + "&ukey=" + md.UserKey) util.CheckError(respErr, shutdown) if resp.StatusCode != http.StatusOK { util.CheckError(errors.New("invalid user"), shutdown) } } var clErr error cl, clErr = tcp.NewClient(md, *tcpServerAddress, *socketPort, *providerPort) util.CheckError(clErr, shutdown) cl.HandleServerCommands() var lErr error l, lErr = tcp.NewListener(*socketPort, md) util.CheckError(lErr, shutdown) p = server.NewProvider("/provide", *providerPort, md) pErr := p.Start() util.CheckError(pErr, shutdown) l.AcceptConnections(*httpServerAddress, md.UserID, md.UserKey) }