예제 #1
0
func main() {
	routerConfig, err := conf.NewConfig("router")
	if err != nil {
		fmt.Println("Error getting config", err)
		return
	}

	k := kitewrapper.NewKiteWrapper(routerConfig)
	err = k.RegisterToKontrol()
	if err != nil {
		fmt.Println("Failed to register", err)
		return
	}

	var appKite, authKite *kite.Client

	go func() {
		var err error

		appKite, err = k.FindAndDial("app")
		if err != nil {
			fmt.Println("Failed to dial app service", err)
			// return
		}
	}()

	go func() {
		var err error

		authKite, err = k.FindAndDial("auth")
		if err != nil {
			fmt.Println("Failed to dial auth service", err)
			// return
		}
	}()

	k.HandleHTTPFunc("/login", func(w http.ResponseWriter, r *http.Request) {
		router.LoginHandler(authKite, w, r)
	})

	k.HandleHTTPFunc("/profile", func(w http.ResponseWriter, r *http.Request) {
		router.ProfileHandler(authKite, w, r)
	})

	k.HandleHTTPFunc("/todos", func(w http.ResponseWriter, r *http.Request) {
		router.TodosHandler(appKite, w, r)
	})

	k.Run()
}
예제 #2
0
func main() {
	appConfig, err := conf.NewConfig("app")
	if err != nil {
		fmt.Println("Error getting config", err)
		return
	}

	k := kitewrapper.NewKiteWrapper(appConfig)
	err = k.RegisterToKontrol()
	if err != nil {
		fmt.Println("Failed to register", err)
		return
	}

	var authKite, dbKite *kite.Client

	go func() {
		var err error
		authKite, err = k.FindAndDial("auth")
		if err != nil {
			fmt.Println("Failed to dial auth service", err)
		}
	}()

	go func() {
		var err error
		dbKite, err = k.FindAndDial("db_accessor")
		if err != nil {
			fmt.Println("Failed to dial db service", err)
		}
	}()

	// Add our handler method
	k.HandleFunc("todos", func(r *kite.Request) (interface{}, error) {
		return app.TodosHandler(authKite, dbKite, r)
	})

	k.Run()
}
예제 #3
0
func main() {
	dbConfig, err := conf.NewConfig("db")
	if err != nil {
		fmt.Println("Error getting config", err)
		return
	}

	k := kitewrapper.NewKiteWrapper(dbConfig)
	err = k.RegisterToKontrol()
	if err != nil {
		fmt.Println("Failed to register", err)
		return
	}

	db := dbaccessor.NewDBAccessor()

	// Add our handler method
	k.HandleFunc("query", func(r *kite.Request) (interface{}, error) {
		return db.QueryHandler(r)
	})

	k.Run()
}
예제 #4
0
func main() {
	authorizer := auth.NewAuthorizer()

	authConfig, err := conf.NewConfig("auth")
	if err != nil {
		fmt.Println("Error getting config", err)
		return
	}

	k := kitewrapper.NewKiteWrapper(authConfig)
	err = k.RegisterToKontrol()
	if err != nil {
		fmt.Println("Failed to register", err)
		return
	}

	var dbKite *kite.Client

	go func() {
		var err error

		dbKite, err = k.FindAndDial("db_accessor")
		if err != nil {
			fmt.Println("Failed to dial db service", err)
			// return
		}
	}()

	// Add our handler method
	k.HandleFunc("login", func(r *kite.Request) (interface{}, error) {
		var (
			params, _ = r.Args.One().Map()
			user, _   = params["user"].String()
			pass, _   = params["pass"].String()
		)

		if user == User && pass == Pass {
			return authorizer.Token, nil
		}

		return nil, BadCredentials
	})

	k.HandleFunc("profile", func(r *kite.Request) (interface{}, error) {
		token, err := r.Args.One().String()
		if err != nil {
			return nil, BadCredentials
		}

		if !authorizer.Validate(token) {
			return nil, BadCredentials
		}

		result, err := dbKite.Tell("query", "profile")
		if err != nil {
			return nil, err
		}

		var (
			profileMap, _ = result.Map()
			profile       = make(map[string]string)
		)
		for name, value := range profileMap {
			val, _ := value.String()
			profile[name] = val
		}

		return profile, nil
	})

	k.HandleFunc("validateToken", func(r *kite.Request) (interface{}, error) {
		token, err := r.Args.One().String()

		if err != nil {
			return false, BadCredentials
		}

		return authorizer.Validate(token), nil
	})

	k.Run()
}