Esempio n. 1
0
func init() {
	/* Initialise configuration instance, w/ GABS. */

	log.Debug("Initialise configuration instance")

	// This has to be a absolute path.
	configurationPath := os.Getenv("RESTAPI_CONFIG")

	// Ascertain if the configuration path is inputted.

	log.Debug("Testing if the configuration path is inputted.")
	if _, err := os.Stat(configurationPath); os.IsNotExist(err) {
		log.Crit("The configuration file does not exist!",
			log.Ctx{"File": configurationPath})
		os.Exit(1)
	}

	log.Debug("Reading configuration file into RAM.")
	configurationFile, err := ioutil.ReadFile(configurationPath)
	if err != nil {
		log.Error("The JSON config could not be loaded..",
			log.Ctx{"Error": err.Error()})
		os.Exit(1)
	}

	log.Debug("Parsing configuration file into a GABS instance.")
	config, err = gabs.ParseJSON(configurationFile)
	if err != nil {
		log.Error("The JSON config could not be parsed.",
			log.Ctx{"Error": err.Error()})
		os.Exit(1)
	}

	/* Finish initializing the configuration instance */
}
Esempio n. 2
0
func Server(srv *context.T) {
	log := srv.Log.New("cmp", "dht")
	ch := srv.Router.Sub("/dht/join")
	tch := srv.Router.Sub("/transaction/mem")
	och := srv.Router.Sub("/git/object")

	keyAuth, err := newKeyAuth()
	if err != nil {
		log.Crit("can't generate node key", "err", err)
		os.Exit(1)
	}

	id, err := wendy.NodeIDFromBytes(util.SHA256(keyAuth.Marshal()))
	log = log.New("own_node", id)

	if err != nil {
		log15.Crit("error preparing node id", "err", err)
		os.Exit(0)
	}

	hostname := strings.Split(srv.Config.Network.Hostname, ":")[0]
	node := wendy.NewNode(id, "127.0.0.1", hostname, "localhost", srv.Config.Network.Port)

	cluster := wendy.NewCluster(node, keyAuth)
	cluster.SetLogLevel(wendy.LogLevelError)
	cluster.RegisterCallback(&GitchainApp{cluster: cluster, log: log.New(), srv: srv})
	go cluster.Listen()
	defer cluster.Stop()

	log.Info("node started")

	for i := range srv.Config.Network.Join {
		log.Info("scheduling a connection", "addr", srv.Config.Network.Join[i])
		srv.Router.Pub(srv.Config.Network.Join[i], "/dht/join")
	}

loop:
	select {
	case addri := <-ch:
		if addr, ok := addri.(string); ok {
			log.Debug("received a request to join the cluster", "addr", addr)

			addr := strings.Split(addr, ":")
			port := 31000
			if len(addr) == 2 {
				port, err = strconv.Atoi(addr[1])
				if err != nil {
					log.Error("invalid port number", "addr", addr, "port", addr[1], "err", err)
					goto loop
				}
			}
			err = cluster.Join(addr[0], port)

			if err != nil {
				log.Error("can't join cluster", "addr", addr, "err", err)
				goto loop
			}
		}
	case txei := <-tch:
		if txe, ok := txei.(*transaction.Envelope); ok {
			log.Debug("received transaction", "txn", txe)
			if err = broadcast(cluster, txe, MSG_TRANSACTION); err != nil {
				log.Error("error broadcasting a transaction message", "txn", txe, "err", err)
			} else {
				log.Debug("broadcasted transaction", "txn", txe)
			}
		}
	case obji := <-och:
		if obj, ok := obji.(git.Object); ok {
			id, err := wendy.NodeIDFromBytes(util.SHA256(obj.Hash()))
			if err != nil {
				log15.Error("error preparing msg id for a git object", "obj", obj, "err", err)
			} else {
				msg := cluster.NewMessage(MSG_REGULAR|MSG_OBJECT, id, git.ObjectToBytes(obj))
				if err = cluster.Send(msg); err != nil {
					log.Error("error sending git object", "obj", obj, "err", err)
				}
			}
		}
	}
	goto loop
}
Esempio n. 3
0
func main() {
	// runtime.GOMAXPROCS(runtime.NumCPU() * 2)
	/*
		// init postgres DB connection
		dbhost := "localhost"
		dbuser := "******"
		dbpassword := "******"
		dbname := "tsingcloud"

		taskPgdb := new(postgres.PostgresDB)
		taskPgdb.InitDb(dbhost, dbuser, dbpassword, dbname)
		defer taskPgdb.Pool.Close()

		taskPgdb.ListTasks()
	*/

	// create channel to communicate over
	jobs := make(chan todos.Job)

	log.Crit("start process job")
	// start watching jobs channel for work
	go todos.ProcessJobs(jobs, Db)

	// create dependencies
	client := &todos.TodoClient{Jobs: jobs}
	handlers := &TodoHandlers{Client: client}

	//var staticHtmlPath string
	staticHtmlPath := "/Users/qinshen/git/web-project/redux-learning/redux-async-learning/dist"
	// configure routes
	router := gin.Default()
	router.Static("/static", staticHtmlPath)
	//*****************************************************************************
	//  jwt token handle
	//*****************************************************************************

	//router.Use( CommHeade)

	router.GET("/user/token", JwtGetToken)
	router.POST("/user/balance", JwtCheckToken)

	//*****************************************************************************
	//  action for todos
	//*****************************************************************************

	v1 := router.Group("/v1")
	{
		v1.POST("/todo", handlers.AddTodo)
		v1.GET("/todo", handlers.GetTodos)
		v1.GET("/todo/:id", handlers.GetTodo)
		v1.PUT("/todo/:id", handlers.SaveTodo)
		v1.DELETE("/todo/:id", handlers.DeleteTodo)
	}

	router.GET("/", func(c *gin.Context) {
		c.Redirect(301, "/github")
	})

	//*****************************************************************************
	// test only  end
	//*****************************************************************************

	// start web server
	router.Run(":8080")

	/*
		routerAdmin := gin.Default()
		// start web server
		// debug
		// automatically add routers for net/http/pprof
		// e.g. /debug/pprof, /debug/pprof/heap, etc.
		routerAdmin.GET("/test", func(c *gin.Context) {
			c.Writer.Header().Set("link", nextPageUrl)
			c.Writer.Header().Set("token", token)
			c.Writer.Header().Set("X-GitHub-Media-Type", "github.v3")

			c.Data(200, "application/json; charset=utf-8", jsonData)
		})

		ginpprof.Wrapper(routerAdmin)
		//routerAdmin.Run(":8091")

	*/
}