func Worker(config *Config, requests chan *EngineRequest) { // create engine and start engine := bytengine.NewEngine() err := engine.Start(config.Bytengine) if err != nil { fmt.Println("Error: ", err) os.Exit(1) } for req := range requests { // check if script request or command request if req.Command != nil { r, err := engine.ExecuteCommand(req.Token, *req.Command) rep := EngineResponse{r, err} req.ResponseChan <- rep } else { r, err := engine.ExecuteScript(req.Token, req.Script) rep := EngineResponse{r, err} req.ResponseChan <- rep } } }
func main() { app := cli.NewApp() createadminCmd := cli.Command{ Name: "createadmin", Flags: []cli.Flag{ cli.StringFlag{Name: "u", Value: "", Usage: "username"}, cli.StringFlag{Name: "p", Value: "", Usage: "password"}, cli.StringFlag{Name: "c", Value: "config.json"}, }, Action: func(c *cli.Context) error { usr := c.String("u") pw := c.String("p") pth := c.String("c") err := readConfigFile(pth) if err != nil { return cli.NewExitError(err.Error(), 1) } // create and start bytengine engine := bytengine.NewEngine() err = engine.Start(Configuration.Bytengine) if err != nil { return cli.NewExitError(err.Error(), 1) } err = engine.CreateAdminUser(usr, pw) if err != nil { return cli.NewExitError(err.Error(), 1) } fmt.Println("...done") return nil }, } run := cli.Command{ Name: "run", Flags: []cli.Flag{ cli.StringFlag{Name: "c", Value: "config.json"}, }, Action: func(c *cli.Context) error { // get configuration file/info pth := c.String("c") err := readConfigFile(pth) if err != nil { return cli.NewExitError(err.Error(), 1) } addr := Configuration.Address port := Configuration.Port workers := Configuration.Workers // start worker pool EngineRequestChan = WorkerPool(workers, Configuration) // setup routes router := gin.Default() router.GET("/", welcomeHandler) router.POST("/bfs/query", runScriptHandler) router.POST("/bfs/token", getTokenHandler) router.POST("/bfs/uploadticket", getUploadTicketHandler) router.POST("/bfs/writebytes/:ticket", uploadFileHandler) router.POST("/bfs/readbytes", downloadFileHandler) router.GET("/bfs/direct/:layer/:database/*path", directaccessHandler) router.Run(fmt.Sprintf("%s:%d", addr, port)) return nil }, } app.Commands = []cli.Command{createadminCmd, run} app.Run(os.Args) }