Exemplo n.º 1
0
func addConnection(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	// read the connection from the body
	connection, err := store.ConnectionFromJson(r.Body)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	// gentlemen, start your engines!
	engine, err := engine.NewConnectionEngine(connection)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	// ok, things look good, let's start our connection
	err = connection.Save()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	// start our engines!
	engine.Start()
	engines[connection.Uuid] = engine

	// write our config to the response
	w.Header().Set("Content-Type", "application/json")

	// serialize to json
	js, err := json.Marshal(connection)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	w.Header().Set("Content-Type", "application/json")
	w.Write(js)
}
Exemplo n.º 2
0
func main() {
	settings := flag.String("settings", "", "Our settings file")
	procs := flag.Int("procs", 4, "Max number of processors to use")
	flag.Parse()

	// they didn't pass in settings parameter, print some help
	if *settings == "" {
		fmt.Println("\nUsage: junebug --settings=junebug.conf")
		fmt.Println()
		fmt.Println("Example configuration file:")
		fmt.Println()
		fmt.Println(cfg.GetSampleConfig())
		fmt.Println()
		os.Exit(1)
	}

	config, err := cfg.ReadConfig(*settings)
	if err != nil {
		fmt.Println("Error reading Junebug settings:")
		fmt.Println(err.Error())
		os.Exit(1)
	}

	runtime.GOMAXPROCS(*procs)

	// Open our Database
	store.OpenDB(config.Db.Filename)

	// load our connection configurations
	connections, err := store.LoadAllConnections()
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("")

	// for each one, create a real connection
	engines := make(map[string]*engine.ConnectionEngine)
	for i := 0; i < len(*connections); i++ {
		connection := (*connections)[i]

		// warm our cache by getting our status
		_, err = connection.GetStatus()
		if err != nil {
			log.Fatal(err)
		}

		// and create our actual connection
		engine, err := engine.NewConnectionEngine(&connection)
		if err != nil {
			log.Fatal(err)
		}

		engine.Start()
		incoming, outgoing, err := engine.AddPendingMsgsFromDB()
		if err != nil {
			log.Fatal(err)
		}

		log.Printf("[%.8s] Started with %d queued outgoing, %d queued incoming\n",
			connection.Uuid, outgoing, incoming)

		// stash it
		engines[connection.Uuid] = engine
	}

	// start our server
	http.StartServer(&engines)
}