func InitAction(ctx *cli.Context) { t := time.Now() // check config, // is install time > 0, show installed message // otherwise, set install time and write new config file if app.Config.AppInstallTime > 0 { log.Info("Blog|Installed|%s", time.Unix(app.Config.AppInstallTime, 0).Format(time.RFC3339)) return } app.Config.AppInstallTime = time.Now().Unix() app.Config.Write() // make directories os.Mkdir(app.Config.UserDirectory, os.ModePerm) os.Mkdir(filepath.Join(app.Config.UserDirectory, app.Config.UserThemeDirectory), os.ModePerm) os.Mkdir(filepath.Join(app.Config.UserDirectory, app.Config.UserUploadDirectory), os.ModePerm) // init database schema app.Db = core.NewDatabase(filepath.Join(app.Config.UserDirectory, app.Config.UserDataFile)) action.Call(InitDbSchema, nil) action.Call(InitDbDefault, nil) log.Info("Blog|Install|Success|%.1fms", time.Since(t).Seconds()*1000) }
func ServAction(ctx *cli.Context) { t := time.Now() address := fmt.Sprintf("%s:%s", app.Config.HttpHost, app.Config.HttpAddress) log.Info("Serv|Begin|%s", address) // init global vars app.Db = core.NewDatabase(filepath.Join(app.Config.UserDirectory, app.Config.UserDataFile)) app.Server = core.NewServer(address) // read settings model.ReadSettingsToGlobal() // set other global vars with setting app.Theme = core.NewTheme(filepath.Join(app.Config.UserDirectory, app.Config.UserThemeDirectory), model.Settings["theme"].GetString()) // init server action.Call(InitServer, nil) // init router action.Call(InitRoute, nil) // start server core.Start(app.Server) log.Info("Serv|Close|%.1fms", time.Since(t).Seconds()*1000) }
// auth handler func AuthHandler() tango.HandlerFunc { return func(ctx *tango.Context) { auth, ok := ctx.Action().(AuthRoute) if !ok { ctx.Next() return } // read token token := auth.GetAuthToken(ctx) if token != "" { result := action.Call(action.UserAuth, token) if result.Status { auth.SetAuthUser(result.Data["user"].(*model.User)) ctx.Next() return } } // fail redirect if url := auth.GetAuthFailRedirect(); url != "" { ctx.Redirect(url, 302) return } // auth fail , no redirect, to show 401 ctx.WriteHeader(401) } }
func (l *Login) Post() { l.Assign("Title", "Login") // validate form form := &action.LoginForm{} if err := l.BindAndValidate(form); err != nil { l.Assign("Error", err.Error()) l.MustRenderTheme(200, "login.tmpl") return } // call UserLogin action result := action.Call(action.UserLogin, form) if !result.Status { l.Assign("Error", result.Error) l.MustRenderTheme(200, "login.tmpl") return } // set cookie token := result.Data["token"].(*model.Token) l.Cookies().Set(&http.Cookie{ Name: "x-token", Value: token.Value, Path: "/", Expires: time.Unix(token.ExpireTime, 0), MaxAge: int(token.ExpireTime - time.Now().Unix()), HttpOnly: true, }) // success, redirect l.Redirect("/admin/") }
// update password post route func (p *Password) Post() { form := &action.PasswordForm{} if err := p.BindAndValidate(form); err != nil { p.Redirect("/admin/profile?password="******"/admin/profile?password="******"/admin/profile?password=true") }
func (w *Write) Post() { form := &action.ArticleForm{} if err := w.BindAndValidate(form); err != nil { w.Assign("SaveError", err.Error()) w.Get() return } form.UserId = w.AuthUser.Id result := action.Call(action.ArticleSave, form) if !result.Status { w.Assign("SaveError", result.Error) w.Get() return } redirect := fmt.Sprintf("/admin/write/id/%d", result.Data["article"].(*model.Article).Id) w.Redirect(redirect) }
func (l *Logout) Get() { // remove token if token := l.Cookie("x-token"); token != "" { action.Call(action.UserLogout, token) } // remove cookie if exist l.Cookies().Set(&http.Cookie{ Name: "x-token", Value: "", Path: "/", HttpOnly: true, MaxAge: 0, }) // redirect go login l.Redirect("/admin/login") }
// update post page func (p *Profile) Post() { // bind form form := &action.ProfileForm{} if err := p.BindAndValidate(form); err != nil { p.Assign("ProfileError", err.Error()) p.Get() return } // call update action res := action.Call(action.UserUpdateProfile, form) if !res.Status { p.Assign("ProfileError", res.Error) p.Get() return } // update auth user data p.SetAuthUser(res.Data["user"].(*model.User)) p.Assign("ProfileSuccess", true) p.Get() }