2020-05-24 14:41:19 +03:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
2020-05-24 17:34:32 +03:00
|
|
|
"fmt"
|
2020-05-24 14:41:19 +03:00
|
|
|
"github.com/gorilla/schema"
|
2020-10-09 22:37:16 +03:00
|
|
|
conf "github.com/muety/wakapi/config"
|
2020-05-24 14:41:19 +03:00
|
|
|
"github.com/muety/wakapi/models"
|
2020-11-06 23:19:54 +03:00
|
|
|
"github.com/muety/wakapi/models/view"
|
2020-05-24 14:41:19 +03:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2020-11-06 23:19:54 +03:00
|
|
|
type HomeHandler struct {
|
2020-11-28 22:23:40 +03:00
|
|
|
config *conf.Config
|
2020-05-24 14:41:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var loginDecoder = schema.NewDecoder()
|
2020-05-24 17:34:32 +03:00
|
|
|
var signupDecoder = schema.NewDecoder()
|
2020-05-24 14:41:19 +03:00
|
|
|
|
2020-11-28 22:23:40 +03:00
|
|
|
func NewHomeHandler() *HomeHandler {
|
2020-11-06 23:19:54 +03:00
|
|
|
return &HomeHandler{
|
2020-11-28 22:23:40 +03:00
|
|
|
config: conf.Get(),
|
2020-05-24 14:41:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 23:19:54 +03:00
|
|
|
func (h *HomeHandler) GetIndex(w http.ResponseWriter, r *http.Request) {
|
2020-05-24 14:41:19 +03:00
|
|
|
if h.config.IsDev() {
|
|
|
|
loadTemplates()
|
|
|
|
}
|
|
|
|
|
|
|
|
if cookie, err := r.Cookie(models.AuthCookieKey); err == nil && cookie.Value != "" {
|
2020-10-04 11:37:38 +03:00
|
|
|
http.Redirect(w, r, fmt.Sprintf("%s/summary", h.config.Server.BasePath), http.StatusFound)
|
2020-05-24 14:41:19 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-06 23:19:54 +03:00
|
|
|
templates[conf.IndexTemplate].Execute(w, h.buildViewModel(r))
|
2020-05-24 14:41:19 +03:00
|
|
|
}
|
|
|
|
|
2020-11-06 23:19:54 +03:00
|
|
|
func (h *HomeHandler) buildViewModel(r *http.Request) *view.HomeViewModel {
|
|
|
|
return &view.HomeViewModel{
|
|
|
|
Success: r.URL.Query().Get("success"),
|
|
|
|
Error: r.URL.Query().Get("error"),
|
|
|
|
}
|
|
|
|
}
|