in online_boutique_demo/src/frontend/main.go [71:196]
func main() {
regAddr := os.Getenv("DUBBO_REGISTRY_ADDRESS")
if regAddr == "" {
regAddr = "127.0.0.1:2181"
}
ins, err := dubbo.NewInstance(
dubbo.WithName("frontendservice"),
dubbo.WithRegistry(
registry.WithZookeeper(),
registry.WithAddress(regAddr),
),
dubbo.WithProtocol(
protocol.WithTriple(),
protocol.WithPort(20010),
),
)
if err != nil {
panic(err)
}
//server
srv, err := ins.NewServer()
if err != nil {
panic(err)
}
//client
cli, err := ins.NewClient()
if err != nil {
panic(err)
}
log := logrus.New()
log.Level = logrus.DebugLevel
log.Formatter = &logrus.JSONFormatter{
FieldMap: logrus.FieldMap{
logrus.FieldKeyTime: "timestamp",
logrus.FieldKeyLevel: "severity",
logrus.FieldKeyMsg: "message",
},
TimestampFormat: time.RFC3339Nano,
}
log.Out = os.Stdout
//adservice
adService, err := pb.NewAdService(cli)
if err != nil {
panic(err)
}
//cartService
cartService, err := pb.NewCartService(cli)
if err != nil {
panic(err)
}
//checkoutService
checkoutService, err := pb.NewCheckoutService(cli)
if err != nil {
panic(err)
}
//currencyService
currencyService, err := pb.NewCurrencyService(cli)
if err != nil {
panic(err)
}
//productcatalog
productCatalogService, err := pb.NewProductCatalogService(cli)
if err != nil {
panic(err)
}
//recommendation
recommendationService, err := pb.NewRecommendationService(cli)
//shippingService
shippingService, err := pb.NewShippingService(cli)
if err != nil {
panic(err)
}
svc := frontendServer{
adService: adService,
cartService: cartService,
checkoutService: checkoutService,
currencyService: currencyService,
productCatalogService: productCatalogService,
shippingService: shippingService,
recommendationService: recommendationService,
}
r := mux.NewRouter()
r.HandleFunc("/", svc.homeHandler).Methods(http.MethodGet, http.MethodHead)
r.HandleFunc("/product/{id}", svc.productHandler).Methods(http.MethodGet, http.MethodHead)
r.HandleFunc("/cart", svc.viewCartHandler).Methods(http.MethodGet, http.MethodHead)
r.HandleFunc("/cart", svc.addToCartHandler).Methods(http.MethodPost)
r.HandleFunc("/cart/empty", svc.emptyCartHandler).Methods(http.MethodPost)
r.HandleFunc("/setCurrency", svc.setCurrencyHandler).Methods(http.MethodPost)
r.HandleFunc("/logout", svc.logoutHandler).Methods(http.MethodGet)
r.HandleFunc("/cart/checkout", svc.placeOrderHandler).Methods(http.MethodPost)
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
r.HandleFunc("/robots.txt", func(w http.ResponseWriter, _ *http.Request) { fmt.Fprint(w, "User-agent: *\nDisallow: /") })
r.HandleFunc("/_healthz", func(w http.ResponseWriter, _ *http.Request) { fmt.Fprint(w, "ok") })
var handler http.Handler = r
handler = &logHandler{log: log, next: handler} // add logging
handler = ensureSessionID(handler) // add session ID
// handler = tracing(handler) // add opentelemetry instrumentation
//r.Use(otelmux.Middleware(name))
//r.Use(tracingContextWrapper)
//TODO:REGISTER HANDLER
//if err := micro.RegisterHandler(srv.Server(), handler); err != nil {
// logger.Fatal(err)
//}
srv_http := &http.Server{
Addr: ":8090",
Handler: handler,
}
log.Fatal(srv_http.ListenAndServe())
logger.Infof("starting server on %s", config.Address())
if err := srv.Serve(); err != nil {
logger.Fatal(err)
}
}