示例#1
0
func InitJobServer(cfg config.Config, pigeonServer jobqueue.Server) error {
	mailer, err := mail.NewMailClient(cfg)
	if err != nil {
		return err
	}

	userCtx := map[string]interface{}{
		"cfg":    cfg,
		"mailer": mailer,
	}

	dl := cassandra_datalayer.NewDatalayer(cfg)
	conn, err := dl.Connect("canopy")
	if err != nil {
		return err
	}
	userCtx["db-conn"] = conn

	routes := map[string]jobqueue.HandlerFunc{
		"api/activate":                 rest.RestJobWrapper(rest.ApiActivateHandler),
		"api/create_devices":           rest.RestJobWrapper(rest.ApiCreateDevicesHandler),
		"api/create_user":              rest.RestJobWrapper(rest.ApiCreateUserHandler),
		"GET:api/device/id":            rest.RestJobWrapper(rest.GET__api__device__id),
		"POST:api/device/id":           rest.RestJobWrapper(rest.POST__api__device__id),
		"DELETE:api/device/id":         rest.RestJobWrapper(rest.DELETE__api__device__id),
		"api/device/id/var":            rest.RestJobWrapper(rest.GET__api__device__id__var),
		"api/devices":                  rest.RestJobWrapper(rest.GET__api__devices),
		"api/finish_share_transaction": rest.RestJobWrapper(rest.POST__api__finish_share_transaction),
		"api/info":                     rest.RestJobWrapper(rest.GET__api__info),
		"api/login":                    rest.RestJobWrapper(rest.POST__api__login),
		"api/logout":                   rest.RestJobWrapper(rest.GET_POST__api__logout),
		"GET:api/user/self":            rest.RestJobWrapper(rest.GET__api__user__self),
		"POST:api/user/self":           rest.RestJobWrapper(rest.POST__api__user__self),
		"DELETE:api/user/self":         rest.RestJobWrapper(rest.DELETE__api__user__self),
		"api/reset_password":           rest.RestJobWrapper(rest.POST__api__reset_password),
		"api/share":                    rest.RestJobWrapper(rest.POST__api__share),
	}

	// Register handlers
	for msgKey, handler := range routes {
		inbox, err := pigeonServer.CreateInbox(msgKey)
		if err != nil {
			return err
		}
		inbox.SetUserCtx(userCtx)
		inbox.SetHandlerFunc(handler)
	}

	return nil
}
示例#2
0
func main() {
	cfg := config.NewDefaultConfig("", "", "")
	err := cfg.LoadConfig()
	if err != nil {
		fmt.Printf("Error loading config")
	}

	err = canolog.Init(".canopy-ops.log")
	if err != nil {
		fmt.Println(err)
		return
	}
	flag.Parse()
	cmd := canopy_ops.FindCommand(cmds, flag.Arg(0))
	info := canopy_ops.CommandInfo{
		CmdList: cmds,
		Cfg:     cfg,
		Args:    flag.Args(),
	}
	if cmd != nil {
		cmd.Perform(info)
	} else if flag.Arg(0) == "create-account" {
		dl := cassandra_datalayer.NewDatalayer(cfg)
		conn, _ := dl.Connect("canopy")
		conn.CreateAccount(flag.Arg(1), flag.Arg(2), flag.Arg(3))
	} else if flag.Arg(0) == "delete-account" {
		dl := cassandra_datalayer.NewDatalayer(cfg)
		conn, _ := dl.Connect("canopy")
		conn.DeleteAccount(flag.Arg(1))
	} else if flag.Arg(0) == "create-device" {
		dl := cassandra_datalayer.NewDatalayer(cfg)
		conn, _ := dl.Connect("canopy")

		account, err := conn.LookupAccount(flag.Arg(1))
		if err != nil {
			fmt.Println("Unable to lookup account ", flag.Arg(1), ":", err)
			return
		}

		device, err := conn.CreateDevice(flag.Arg(2), nil, "", datalayer.NoAccess)
		if err != nil {
			fmt.Println("Unable to create device: ", err)
			return
		}

		err = device.SetAccountAccess(account, datalayer.ReadWriteAccess, datalayer.ShareRevokeAllowed)
		if err != nil {
			fmt.Println("Unable to grant account access to device: ", err)
			return
		}
	} else if flag.Arg(0) == "list-devices" {
		dl := cassandra_datalayer.NewDatalayer(cfg)
		conn, _ := dl.Connect("canopy")

		account, err := conn.LookupAccount(flag.Arg(1))
		if err != nil {
			fmt.Println("Unable to lookup account ", flag.Arg(1), ":", err)
			return
		}

		devices, err := account.Devices().DeviceList(0, -1)
		if err != nil {
			fmt.Println("Error reading devices: ", err)
			return
		}
		for _, device := range devices {
			fmt.Printf("%s %s\n", device.ID(), device.Name())
		}

	} else if flag.Arg(0) == "gen-fake-sensor-data" {
		dl := cassandra_datalayer.NewDatalayer(cfg)
		conn, _ := dl.Connect("canopy")
		deviceId, err := gocql.ParseUUID(flag.Arg(1))
		if err != nil {
			fmt.Println("Error parsing UUID: ", flag.Arg(1), ":", err)
			return
		}
		_, err = conn.LookupDevice(deviceId)
		if err != nil {
			fmt.Println("Device not found: ", flag.Arg(1), ":", err)
			return
		}
		for i := 0; i < 100; i++ {
			//val := float64(i % 16);
			//t := time.Now().Add(time.Duration(-i)*time.Second)
			//err = device.InsertSensorSample(flag.Arg(2), t, val)
			//if err != nil {
			fmt.Println("Error inserting sample: ", err)
			//}
		}
	} else if flag.Arg(0) == "clear-sensor-data" {
		dl := cassandra_datalayer.NewDatalayer(cfg)
		conn, _ := dl.Connect("canopy")
		conn.ClearSensorData()

	} else if flag.Arg(0) == "test-email" {
		mailer, err := mail.NewMailClient(cfg)
		if err != nil {
			fmt.Println("Error initializing mail client: ", err)
			return
		}
		mail := mailer.NewMail()
		err = mail.AddTo(flag.Arg(1), "Customer")
		if err != nil {
			fmt.Println("Invalid recipient: ", flag.Arg(1), err)
			return
		}
		mail.SetSubject("Test email from Canopy")
		mail.SetHTML("<b>Canopy Rulez</b>")
		mail.SetFrom("*****@*****.**", "The Canopy Team")
		err = mailer.Send(mail)
		if err != nil {
			fmt.Println("Error sending email:", err)
			return
		}
		fmt.Println("Email sent.")
	} else if flag.Arg(0) == "migrate-db" {
		startVersion := flag.Arg(1)
		if startVersion == "" {
			fmt.Println("<startVersion> required")
			return
		}
		endVersion := flag.Arg(2)
		if endVersion == "" {
			fmt.Println("<endVersion> required")
			return
		}
		dl := cassandra_datalayer.NewDatalayer(cfg)
		err := dl.MigrateDB("canopy", startVersion, endVersion)
		if err != nil {
			fmt.Println(err.Error())
		}
	} else if len(flag.Args()) == 0 {
		cmds[0].Perform(info)
	} else {
		fmt.Println("Unknown command '" + flag.Arg(0) + "'.  See 'canopy-ops help'.")
	}
}