Ejemplo n.º 1
0
// newApp creates a new application. function can only be called once
func newApp() *app {
	a := new(app)
	var err error

	// make sure env var CS_DSN is set and points to the appropriate database
	if a.dbc, err = csdb.Connect(); err != nil {
		log.Fatal("MySQL Connect", "err", err)
	}

	if err := config.TableCollection.Init(a.dbc.NewSession()); err != nil {
		log.Fatal("config.TableCollection.Init", "err", err)
	}
	if err := store.TableCollection.Init(a.dbc.NewSession()); err != nil {
		log.Fatal("store.TableCollection.Init", "err", err)
	}

	a.config = config.NewService(config.WithDBStorage(a.dbc.DB))

	// create JSON web token instance
	if a.jwtSrv, err = ctxjwt.NewService(); err != nil {
		log.Fatal("ctxjwt.NewService", "err", err)
	}
	a.jwtSrv.EnableJTI = true

	return a
}
Ejemplo n.º 2
0
func main() {
	a := newApp()
	defer a.close() // @todo check signal and close gracefully

	ctx := store.WithContextMustService(
		scope.Option{Website: scope.MockID(1)}, // run website ID 1, see database table, like Mage::run('code','store')
		store.MustNewStorage(store.WithDatabaseInit(a.dbc.NewSession())),
	)

	ctx = config.WithContextGetter(ctx, a.config)

	router := ctxrouter.New(ctx)
	a.routeLogin(router)
	a.setupStoreRoutes(router)

	router.Handle("GET", "/error", func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
		return store.ErrContextServiceNotFound
	})

	println("Starting server @ ", ServerAddress)

	log.Fatal("ListenAndServe", "err", http.ListenAndServe(ServerAddress, router))

}
func ExampleWithInitStoreByToken() {
	initStore()
	ctx := store.WithContextReader(context.Background(), testStoreService)

	jwtService, err := ctxjwt.NewService(ctxjwt.WithPassword([]byte(`GÒph3r`)))

	finalHandler := ctxhttp.Chain(
		func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
			_, haveReqStore, err := store.FromContextReader(ctx)
			if err != nil {
				return err
			}
			// now we know that the current request depends on the store view DE.
			fmt.Fprintf(w, "StoreCode: %s\n", haveReqStore.StoreCode())
			return nil
		}, //
		jwtService.WithParseAndValidate(),
		store.WithInitStoreByToken(),
	)

	ts := httptest.NewServer(ctxhttp.NewAdapter(ctx, finalHandler))
	defer ts.Close()

	// Setup GET request
	token, _, err := jwtService.GenerateToken(
		map[string]interface{}{
			// Despite default store for Website ID 1 is AT we are currently
			// in the store context of DE.
			store.ParamName: "de",
		},
	)
	if err != nil {
		log.Fatal("jwtService.GenerateToken", "err", err)
	}

	req, err := http.NewRequest("GET", ts.URL, nil)
	if err != nil {
		log.Fatal("http.Get", "err", err)
	}
	ctxjwt.SetHeaderAuthorization(req, token)

	res, err := http.DefaultClient.Do(req)
	if err != nil {
		log.Fatal("http.DefaultClient.Do", "err", err)
	}

	response, err := ioutil.ReadAll(res.Body)
	if errC := res.Body.Close(); errC != nil {
		log.Fatal("res.Body.Close", "err", errC)
	}

	if err != nil {
		log.Fatal("ioutil.ReadAll", "err", err)
	}

	fmt.Printf("Response: %s\n", response)
	fmt.Printf("Log: %s\n", testDebugLogBuf.String())
	// Output:
	// Response: StoreCode: de
	//
	// Log:
}
Ejemplo n.º 4
0
func (a *app) close() {
	if err := a.dbc.Close(); err != nil {
		log.Fatal("MySQL Close", "err", err)
	}
}