// This is called if an error occured in a front hook. func DErr(uni *context.Uni, err error) { if _, isjson := uni.Req.Form["json"]; isjson { putJSON(uni) return } uni.Put(err) }
// Prints errors during query running to http response. (Kinda nonsense now as it is.) func queryErr(uni *context.Uni) { r := recover() if r != nil { uni.Put("There was an error running the queries: ", r) debug.PrintStack() } }
// Prints errors during template file display to http response. (Kinda nonsense now as it is.) func displErr(uni *context.Uni) { r := recover() if r != nil { uni.Put("There was an error executing the template.") fmt.Println("problem with template: ", r) debug.PrintStack() } }
// Prints all available data to http response as a JSON. func putJSON(uni *context.Uni) { var v []byte if _, format := uni.Req.Form["fmt"]; format { v, _ = json.MarshalIndent(uni.Dat, "", " ") } else { v, _ = json.Marshal(uni.Dat) } uni.W.Header().Set("Content-Type", "application/json; charset=utf-8") uni.Put(string(v)) }
// Tries to dislay a template file. func DisplayTemplate(uni *context.Uni, filep string) error { _, src := uni.Req.Form["src"] file, err := require.R("", filep+".tpl", func(root, fi string) ([]byte, error) { return GetFileAndConvert(uni.Root, fi, uni.Opt, uni.Req.Host, nil) }) if err != nil { return fmt.Errorf("Cant find template file %v.", filep) } if src { uni.Put(string(file)) return nil } uni.Dat["_tpl"] = "/templates/" + scut.TemplateType(uni.Opt) + "/" + scut.TemplateName(uni.Opt) + "/" prepareAndExec(uni, string(file)) return nil }
// Displays a display point. func D(uni *context.Uni) { points, points_exist := uni.Dat["_points"] var point string if points_exist { point = points.([]string)[0] } else { p := uni.Req.URL.Path if p == "/" { point = "index" } else { point = p } } queries, queries_exists := jsonp.Get(uni.Opt, "Display-points."+point+".queries") if queries_exists { qmap, ok := queries.(map[string]interface{}) if ok { runQueries(uni, qmap) } } BeforeDisplay(uni) // While it is not the cheapest solution to convert bson.ObjectIds to strings here, where we have to iterate trough all values, // it is still better than remembering (and forgetting) to convert it at every specific place. scut.IdsToStrings(uni.Dat) langs, _ := jsonp.Get(uni.Dat, "_user.languages") // _user always has language member langs_s := toStringSlice(langs) loc, _ := display_model.LoadLocStrings(uni.Dat, langs_s, uni.Root, scut.GetTPath(uni.Opt, uni.Req.Host), nil) // TODO: think about errors here. if loc != nil { uni.Dat["loc"] = loc } if _, isjson := uni.Req.Form["json"]; isjson { putJSON(uni) return } else { err := DisplayFile(uni, point) if err != nil { uni.Dat["missing_file"] = point err_404 := DisplayFile(uni, "404") if err_404 != nil { uni.Put("Cant find file: ", point) } } } }
// After running a background operation this either redirects with data in url paramters or prints out the json encoded result. func actionResponse(uni *context.Uni, err error, action_name string) { if DEBUG { fmt.Println(uni.Req.Referer()) fmt.Println(" ", err) } _, is_json := uni.Req.Form["json"] redir := uni.Req.Referer() if red, ok := uni.Dat["redirect"]; ok { redir = red.(string) } else if post_red, okr := uni.Req.Form["redirect"]; okr && len(post_red) == 1 { redir = post_red[1] } var cont map[string]interface{} cont_i, has := uni.Dat["_cont"] if has { cont = cont_i.(map[string]interface{}) } else { cont = map[string]interface{}{} } redir = appendParams(redir, action_name, err, cont) if is_json { cont["redirect"] = redir if err == nil { cont["ok"] = true } else { cont["error"] = err.Error() } var v []byte if _, fmt := uni.Req.Form["fmt"]; fmt { v, _ = json.MarshalIndent(cont, "", " ") } else { v, _ = json.Marshal(cont) } uni.Put(string(v)) } else { http.Redirect(uni.W, uni.Req, redir, 303) } }
// Tries to display a module file. func DisplayFallback(uni *context.Uni, filep string) error { _, src := uni.Req.Form["src"] if strings.Index(filep, "/") != -1 { return fmt.Errorf("Nothing to fall back to.") // No slash in fallback path means no modulename to fall back to. } if scut.PossibleModPath(filep) { return fmt.Errorf("Not a possible fallback path.") } file, err := require.R("", filep+".tpl", // Tricky, care. func(root, fi string) ([]byte, error) { return GetFileAndConvert(uni.Root, fi, uni.Opt, uni.Req.Host, nil) }) if err != nil { fmt.Errorf("Cant find fallback file %v.", filep) } if src { uni.Put(string(file)) return nil } uni.Dat["_tpl"] = "/modules/" + strings.Split(filep, "/")[0] + "/tpl/" prepareAndExec(uni, string(file)) return nil }
// Helper function to hotregister a guest user, log him in and build his user data into uni.Dat["_user"]. func RegLoginBuild(uni *context.Uni, solved_puzzle bool) error { db := uni.Db ev := uni.Ev guest_rules := guestRules(uni) inp := uni.Req.Form http_header := uni.Req.Header dat := uni.Dat w := uni.W block_key := []byte(uni.Secret()) guest_id, err := user_model.RegisterGuest(db, ev, guest_rules, inp, solved_puzzle) if err != nil { return err } err = user_model.Login(w, guest_id, block_key) if err != nil { return err } user, err := user_model.BuildUser(db, ev, guest_id, http_header) if err != nil { return err } dat["_user"] = user return nil }
func showTimer(uni *context.Uni, puzzle_opt map[string]interface{}) (string, error) { return user_model.ShowTimer(uni.Secret(), puzzle_opt) }
func solveTimer(uni *context.Uni, puzzle_opts map[string]interface{}) error { return user_model.SolveTimer(uni.Secret(), uni.Req.Form, puzzle_opts) }
func adErr(uni *context.Uni) { if r := recover(); r != nil { uni.Put("There was an error running the admin module.\n", r) debug.PrintStack() } }