Пример #1
0
// Walk the supplied basePath directory and parse all files and templates into routes
// using the route prefix specified.
func (svr *Server) LoadTemplates(routePrefix, basePath string) error {

	wfn := func(path string, info os.FileInfo, err error) error {
		if info.IsDir() {
			return nil
		}
		svr.Log.Debug("Loading: %s", path)

		ext := filepath.Ext(path)
		switch ext {
		case ".hbs":
			route := routePrefix + strings.TrimSuffix(path[len(basePath)+1:], ".hbs")
			template, err := raymond.ParseFile(path)
			if err != nil {
				return err
			}
			svr.Root.Add(route, NewTemplateSegment(template))
		case ".raw":
			fallthrough
		default:
			route := routePrefix + strings.TrimSuffix(path[len(basePath)+1:], ".raw")
			fn, err := filepath.Abs(path)
			if err != nil {
				return err
			}
			svr.Root.Add(route, NewFileSegment(fn))
		}

		return nil
	}

	return filepath.Walk(basePath, wfn)
}
Пример #2
0
Файл: main.go Проект: siim-/siil
//Initialize the Siil API server
func StartAPIServer(c *cli.Context) {
	var (
		baseRouter *mux.Router
		port       int    = c.GlobalInt("port")
		tmplDir    string = fmt.Sprintf("%s/templates", c.GlobalString("wd"))
	)

	fmt.Printf("Starting API server on port %d...\n", port)
	if err := entity.CreateConnection(c.GlobalString("mysql")); err != nil {
		log.Fatal(err)
	}

	fmt.Println("Loading templates...")
	if t, err := ioutil.ReadDir(tmplDir); err != nil {
		log.Fatal(err)
	} else {
		for _, file := range t {
			name := fmt.Sprintf("%s/%s", tmplDir, file.Name())
			if template, err := raymond.ParseFile(name); err != nil {
				log.Panic(err)
			} else {
				templates[file.Name()] = template
			}
		}
	}

	site.SIIL_SITE_ID = c.GlobalString("sid")

	baseRouter = mux.NewRouter()

	//Root endpoint doesn't really do anything
	baseRouter.HandleFunc("/", handleRootRequest)
	baseRouter.HandleFunc("/success", handleSuccessRequest)
	baseRouter.HandleFunc("/fail", handleCancelRequest)

	//User primer & authentication handlers
	baseRouter.HandleFunc("/signin/{site:[a-zA-Z0-9]*}", handleSigninRequest)
	baseRouter.HandleFunc("/id/{site:[a-zA-Z0-9]*}", handleSessionCreation)

	//New site creation
	baseRouter.HandleFunc("/addsite", handleAddSiteForm)
	baseRouter.HandleFunc("/addsite/fail", handleAddSiteFormFailed)
	baseRouter.HandleFunc("/addsite/success", handleAddSiteSuccess)
	baseRouter.HandleFunc("/addsite/request", handleAddSiteRequest)

	//Edit site
	baseRouter.HandleFunc("/editsite/success", handleEditSiteSuccess)
	baseRouter.HandleFunc("/editsite/{site:[a-zA-Z0-9]*}", handleEditSiteForm)
	baseRouter.HandleFunc("/editsite/{site:[a-zA-Z0-9]*}/fail", handleEditSiteFormFailed)
	baseRouter.HandleFunc("/editsite/{site:[a-zA-Z0-9]*}/request", handleEditSiteRequest)

	//Invalidate sessions
	baseRouter.HandleFunc("/signout/{token:[a-zA-Z0-9]*}", handleSignoutRequest)

	baseRouter.HandleFunc("/api/session", handleAPISessionRequest)
	baseRouter.HandleFunc("/api/me", handleAPIMeRequest)

	http.ListenAndServe(fmt.Sprintf(":%d", port), baseRouter)
}