func loadTemplatesFromBundle() { templates = mandira.NewLoader("./templates/", false) for path, content := range _bundle { if mandira.IsTemplate(path) { path = strings.TrimPrefix(path, "templates/") templates.Add(path, MustParse(content)) } } templates.Preload = true templates.Loaded = true t = templates.MustGet }
func init() { var err error path := environ("GOWIKI_PATH", "./wiki.db") db, err = sqlx.Connect("sqlite3", path) if err != nil { log.Fatal("Error: ", err) } dbm = modl.NewDbMap(&db.DB, modl.SqliteDialect{}) dbm.AddTable(User{}, "user").SetKeys(true, "id") dbm.AddTable(Page{}, "page").SetKeys(false, "url") dbm.AddTable(Config{}, "config").SetKeys(false, "key") dbm.AddTable(File{}, "file").SetKeys(false, "path") dbm.AddTable(Crosslink{}, "crosslink").SetKeys(false, "from", "to") err = dbm.CreateTablesIfNotExists() if err != nil { log.Fatal("Database not creatable: ", err) } // load bundled data loadBundle() cfg = LoadConfig() bootstrap() // update bundled data with copies from the database updateBundle() cookies = sessions.NewCookieStore([]byte(cfg.Secret)) // if we're developing, use /static/ and /templates/ if MODE == DEVELOP { fmt.Println("Running in development mode without bundled resources.") http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static/")))) templates = mandira.NewLoader("./templates/", false) } else { fmt.Println("Running in deployment mode with bundled resources.") http.Handle("/static/", http.HandlerFunc(bundleStatic)) loadTemplatesFromBundle() } t = templates.MustGet }
func init() { r := &Router{} r.Get("/new", newTest) r.Get("/{id:[^/]+}/complete", complete) r.Post("/{id:[^/]+}/exec", exec) r.Get("/{id:[^/]+}", detail) r.Get("/", index) http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static/")))) handler := handlers.LoggingHandler(os.Stdout, r) http.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { now := time.Now().UTC() ts := now.Format(time.RFC1123) ts = strings.Replace(ts, "UTC", "GMT", 1) w.Header().Set("Server", "sql-test") w.Header().Set("Date", ts) w.Header().Set("Content-Type", "text/html; charset=utf-8") handler.ServeHTTP(w, req) })) templates = mandira.NewLoader("./templates", PRELOAD_TEMPLATES) }
func main() { flag.StringVar(&opts.port, "port", environ("GOWIKI_PORT", "2222"), "port to run on") flag.StringVar(&opts.db, "db", environ("GOWIKI_PATH", "./wiki.db"), "path for wiki db") flag.BoolVar(&opts.debug, "debug", len(os.Getenv("GOWIKI_DEVELOP")) > 0, "run with debug mode") flag.BoolVar(&opts.delstatic, "del-static", false, "delete db-cached static files") flag.BoolVar(&opts.loadstatic, "load-static", false, "reload db-cached static files") flag.Parse() if opts.debug && opts.delstatic { fmt.Printf("Error: cannot specify -debug and -del-static") return } initdb(opts.db) if opts.delstatic { var files []File err := db.Select(&files, "SELECT * FROM file;") if err != nil { fmt.Printf("Error reading files from db: %s\n", err) } if err == nil && len(files) > 0 { fmt.Printf("Deleting %d cached static files:\n", len(files)) for _, f := range files { fmt.Printf(" > %s\n", f.Path) } } db.MustExec("DELETE FROM file;") return } if opts.loadstatic { bootstrap() return } bootstrap() // update bundled data with copies from the database updateBundle() cookies = sessions.NewCookieStore([]byte(cfg.Secret)) if opts.debug { // if we're developing, use /static/ and /templates/ fmt.Println("Running in development mode without bundled resources.") http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static/")))) templates = mandira.NewLoader("./templates/", false) } else { fmt.Println("Running in deployment mode with bundled resources.") http.Handle("/static/", http.HandlerFunc(bundleStatic)) loadTemplatesFromBundle() } t = templates.MustGet // TODO: user/delete && page/delete r := pat.New() // user management r.Get("/users/create", http.HandlerFunc(createUser)) r.Post("/users/create", http.HandlerFunc(createUser)) r.Get("/users/login", http.HandlerFunc(login)) r.Post("/users/login", http.HandlerFunc(login)) r.Get("/users/logout", http.HandlerFunc(logout)) r.Get("/users/{id}", http.HandlerFunc(showUser)) r.Get("/users", http.HandlerFunc(listUsers)) // page management r.Get("/pages/edit{url:.+}", http.HandlerFunc(editPage)) r.Post("/pages/edit{url:.+}", http.HandlerFunc(editPage)) r.Get("/pages", http.HandlerFunc(listPages)) // config r.Get("/config/files/{path:.+}", http.HandlerFunc(editFile)) r.Post("/config/files/{path:.+}", http.HandlerFunc(editFile)) r.Get("/config/files", http.HandlerFunc(listFiles)) r.Get("/config", http.HandlerFunc(configWiki)) r.Post("/config", http.HandlerFunc(configWiki)) // wiki site r.Get("/{url:.*}", http.HandlerFunc(wikipage)) handler := handlers.LoggingHandler(os.Stdout, r) http.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { now := time.Now().UTC() ts := now.Format(time.RFC1123) ts = strings.Replace(ts, "UTC", "GMT", 1) w.Header().Set("Server", "gowiki") w.Header().Set("Date", ts) w.Header().Set("Content-Type", "text/html; charset=utf-8") handler.ServeHTTP(w, req) })) fmt.Println("Listening on :" + opts.port) log.Fatal(http.ListenAndServe(":"+opts.port, nil)) }