예제 #1
0
func (s httpServer) Serve(exit <-chan struct{}) {
	r := mux.NewRouter()
	r.HandleFunc("/new", s.serveCreateEntry).Methods("GET", "POST")
	r.HandleFunc("/delete/{id}", s.withDoc(s.serveDeleteEntry)).Methods("POST")
	r.HandleFunc("/edit/{id}", s.withDoc(s.serveEditEntry)).Methods("GET", "POST")
	r.HandleFunc("/view/{id}", s.withDoc(s.serveViewEntry)).Methods("GET")

	r.HandleFunc("/search", s.serveSearchEntries).Methods("POST")

	r.HandleFunc("/alltags", s.serveAllTags).Methods("GET")
	r.HandleFunc("/tag/{tag}", s.serveTagDetails).Methods("GET")
	r.HandleFunc("/licenses", s.serveLicenses).Methods("GET")
	r.HandleFunc("/help", s.serveMarkdown("help")).Methods("GET")
	r.HandleFunc("/", s.serveIndex).Methods("GET")

	r.PathPrefix("/").Handler(http.FileServer(http.Dir(path.Join(config.ApplicationDir, "static")))).Methods("GET")

	ln, err := net.Listen("tcp", config.Current.HttpAddr)
	if err != nil {
		log.Fatal(err)
		return
	}

	srv := http.Server{Handler: r}
	go func() {
		log.Info("Starting Webserver")
		srv.Serve(tcpKeepAliveListener{ln.(*net.TCPListener)})
	}()
	select {
	case <-exit:
	}
	log.Info("Stopping Webserver")
	ln.Close()
}
예제 #2
0
파일: index.go 프로젝트: oblank/knowledgedb
func Init(allDocs GetAllDocsFn) {
	var err error

	idx_path := path.Join(config.Current.DataDir, index_name)

	index, err = bleve.Open(idx_path)
	if err == bleve.ErrorIndexPathDoesNotExist {
		log.Info("Creating new Index")
		indexMapping := bleve.NewIndexMapping()
		indexMapping.DefaultAnalyzer = config.Current.DefaultAnalyzer

		entryMapping := bleve.NewDocumentMapping()

		textField := bleve.NewTextFieldMapping()

		entryMapping.AddFieldMappingsAt("Body", textField)
		entryMapping.AddFieldMappingsAt("Title", textField)

		tagField := bleve.NewTextFieldMapping()
		tagField.Analyzer = tag_analyzer

		entryMapping.AddFieldMappingsAt("Tags", tagField)

		indexMapping.AddDocumentMapping("entry", entryMapping)

		index, err = bleve.New(idx_path, indexMapping)
		if err != nil {
			log.Fatal(err)
		}

		// reindex existing documents
		indexRebuildingLogged := false
		for itm := range allDocs() {
			if !indexRebuildingLogged {
				indexRebuildingLogged = true
				log.Info("Start rebuilding Search-Index")
			}

			index.Index(itm.Id(), itm.Content())
		}
		if indexRebuildingLogged {
			log.Info("Finished rebuilding Search-Index")
		}

	} else if err == nil {
		log.Info("Opening existing Index")
	} else {
		log.Fatal(err)
	}
}