func main() { flag.Parse() if *devMode { ast.EnableDeveloperFunctions() if *logKeys == "" { clog.ParseLogFlags(devModeDefaultLogKeys) clog.Log("Developer Mode Enabled (default developer logging)") } else { clog.ParseLogFlag(*logKeys) clog.Log("Developer Mode Enabled (custom command-line logging)") } } if *profileMode { clog.Log("Enabling HTTP Profiling on :6060") go func() { realhttp.ListenAndServe(":6060", nil) }() } go dumpOnSignalForPlatform() // create a QueryChannel queryChannel := make(network.QueryChannel) // create one or more network endpoints httpEndpoint := http.NewHttpEndpoint(*addr, *staticPath) httpEndpoint.SendQueriesTo(queryChannel) err := server.Server(VERSION, *couchbaseSite, *defaultPoolName, queryChannel, queryTimeout) if err != nil { clog.Fatalf("Unable to run server, err: %v", err) } }
func setup(cacheDir string, src string, srcF os.FileInfo, idx Index, err error) error { if strings.HasPrefix(srcF.Name(), ".") || strings.Contains(src, "/.") { return nil } dst := cacheDir + src if strings.HasSuffix(dst, ".md") { dst = strings.TrimSuffix(dst, ".md") + ".html" } dstF, dstE := os.Stat(dst) // if up to date, skip if !os.IsNotExist(dstE) && (srcF.IsDir() || dstF.ModTime().After(srcF.ModTime())) { return nil } // copy to cache if os.IsNotExist(dstE) { clog.Log("Copying %s", srcF.Name()) } else { clog.Log("Updating %s", srcF.Name()) } if srcF.IsDir() { if err := os.MkdirAll(dst, srcF.Mode()); err != nil { return err } return nil } sbuf, err := ioutil.ReadFile(src) if err != nil { return err } dbuf := sbuf if strings.HasSuffix(src, ".md") { dbuf = blackfriday.MarkdownCommon(sbuf) } if err := ioutil.WriteFile(dst, dbuf, srcF.Mode()); err != nil { return err } if strings.HasSuffix(src, ".md") { search := regexp.MustCompile("##[^\n]+\n") found := search.FindString(string(sbuf)) if len(found) > 0 { found = strings.TrimSpace(strings.TrimPrefix(found, "##")) nsearch := regexp.MustCompile("/slide-(\\d+).md") num := nsearch.FindStringSubmatch(src) if num != nil { n, _ := strconv.Atoi(num[1]) li := strconv.Itoa(n) + ". " + found idx[li] = n } } } return nil }
func Server(version, siteName, defaultPoolName string, queryChannel network.QueryChannel, timeout *time.Duration) error { site, err := Site(siteName) if err != nil { return fmt.Errorf("Unable to access site %s, err: %v", siteName, err) } systemProxySite, err := system.NewSite(site) if err != nil { return fmt.Errorf("Unable to instantiate system catalog: %v", err) } // create a StaticQueryPipeline we use to process queries comp := standardCompiler.NewCompiler(systemProxySite, defaultPoolName) exec := interpretedExecutor.NewExecutor(systemProxySite, defaultPoolName) clog.Log("tuqtng started...") clog.Log("version: %s", version) clog.Log("site: %s", siteName) // dispatch each query that comes in for query := range queryChannel { go Dispatch(query, comp, exec, timeout) } return nil }
func serve(cdir string, tld string) { tempDir, _ := ioutil.TempDir("", "tut") tempDir += string(os.PathSeparator) defer os.RemoveAll(tempDir) clog.Log("Workdir %s", tempDir) c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) go func() { <-c os.RemoveAll(tempDir) clog.Fatal("Stopped") }() var idx Index idx = make(map[string]int) walker := func(src string, f os.FileInfo, err error) error { return setup(tempDir, src, f, idx, err) } if err := filepath.Walk(cdir, walker); err != nil { clog.Fatalf("Filewalk %v", err) } getindex := func(w http.ResponseWriter, r *http.Request) { json, err := json.Marshal(idx) if err != nil { clog.Fatalf("During Index JSON Marshal %v", err) } w.Header().Set("Content-Type", "application/json") w.Write(json) } http.HandleFunc("/tutorial/index.json", getindex) url, _ := url.Parse("http://localhost:8093") rp := httputil.NewSingleHostReverseProxy(url) http.Handle("/query", rp) fs := http.FileServer(http.Dir(tempDir + "/" + tld + "/")) http.Handle("/tutorial/", http.StripPrefix("/tutorial/", fs)) http.Handle("/", http.RedirectHandler("/tutorial/index.html#1", 302)) clog.Log("Running at http://localhost:8000/") go func() { for { filepath.Walk(cdir, walker) time.Sleep(2 * time.Second) } }() // last step if err := http.ListenAndServe(":8000", nil); err != nil { clog.Fatalf("ListenAndServe %v", err) } }