Esempio n. 1
0
func (s *WalkhubServer) Start(addr string, certfile string, keyfile string) error {
	frontendPaths := []string{
		"/user/:uuid",
		"/connect",
		"/record",
		"/walkthrough/:uuid",
		"/search",
		"/embedcode",
		"/helpcenterlist",
	}
	for _, path := range append(frontendPaths, s.CustomPaths...) {
		s.AddFile(path, "assets/index.html")
	}

	s.Options("/*path", corsPreflightHandler(s.BaseURL, s.HTTPOrigin))

	s.Use(corsMiddleware(s.BaseURL, s.HTTPOrigin))

	UserDelegate.DB = s.GetDBConnection()

	gauth := google.NewGoogleAuthProvider(s.AuthCreds.Google, &GoogleUserDelegate{})
	authsvc := auth.NewService(s.BaseURL, UserDelegate, s.GetDBConnection(), gauth)
	s.RegisterService(authsvc)

	s.RegisterService(&UserService{})

	searchsvc := search.NewSearchService(s.GetDBConnection(), nil)
	searchsvc.AddDelegate("walkthrough", &walkhubSearchDelegate{
		db: s.GetDBConnection(),
	})
	s.RegisterService(searchsvc)

	s.RegisterService(&WalkthroughService{
		SearchService: searchsvc,
		BaseURL:       s.BaseURL,
	})

	s.RegisterService(&EmbedLogService{})

	s.RegisterService(&LogService{
		BaseURL: s.BaseURL,
	})

	s.Get("/metrics", stdprometheus.Handler(), ab.RestrictPrivateAddressMiddleware())

	if certfile != "" && keyfile != "" {
		s.setupHTTPS()
		if s.TLSConfig == nil {
			s.TLSConfig = &tls.Config{}
		}

		if s.TLSConfig.ServerName == "" {
			s.TLSConfig.ServerName = s.BaseURL
		}
	}

	return s.StartHTTPS(addr, certfile, keyfile)
}
func afterWalkthroughServiceRegister(s *WalkthroughService, h *hitch.Hitch) {
	reindexing := false
	var reindexingMutex sync.RWMutex
	h.Post("/api/reindexwalkthroughs", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		reindexingMutex.RLock()
		idxing := reindexing
		reindexingMutex.RUnlock()

		if idxing {
			ab.Fail(r, http.StatusServiceUnavailable, errors.New("reindexing is in progress"))
		}

		reindexingMutex.Lock()
		reindexing = true
		reindexingMutex.Unlock()

		db := ab.GetDB(r)

		go func() {
			defer func() {
				reindexingMutex.Lock()
				reindexing = false
				reindexingMutex.Unlock()
			}()
			err := s.SearchService.PurgeIndex()
			if err != nil {
				log.Println(err)
				return
			}

			wts, err := LoadAllActualWalkthroughs(db, 0, 0)
			if err != nil {
				log.Println(err)
				return
			}

			for _, wt := range wts {
				err = s.SearchService.IndexEntity("walkthrough", wt)
				if err != nil {
					log.Println(err)
					return
				}
			}
		}()

		ab.Render(r).SetCode(http.StatusAccepted)
	}), ab.RestrictPrivateAddressMiddleware())

}