2021-11-22 09:06:23 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
|
|
|
|
"status/config"
|
|
|
|
|
"status/storage"
|
|
|
|
|
"status/web/session"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func serverError(w http.ResponseWriter, err error) {
|
|
|
|
|
log.Println("[server error]", err)
|
|
|
|
|
http.Error(w, fmt.Sprintf("server error: %s", err), http.StatusInternalServerError)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func notFound(w http.ResponseWriter) {
|
|
|
|
|
http.Error(w, "Page Not Found", http.StatusNotFound)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func unauthorized(w http.ResponseWriter) {
|
|
|
|
|
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Handler struct {
|
|
|
|
|
cfg *config.Config
|
|
|
|
|
mux *mux.Router
|
|
|
|
|
storage *storage.Storage
|
|
|
|
|
sess *session.Session
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//func (h *Handler) getUser(r *http.Request) (string, error) {
|
|
|
|
|
// user, err := h.sess.Get(r)
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// return "", err
|
|
|
|
|
// }
|
|
|
|
|
// if h.cfg.Env != "PROD" {
|
|
|
|
|
// return user, err
|
|
|
|
|
// }
|
|
|
|
|
// // Removes "https://" from referer before checking.
|
|
|
|
|
// // Thank you crussel for this fix!
|
|
|
|
|
// if !strings.HasPrefix(r.Referer()[8:], h.cfg.Host) && !strings.HasPrefix(r.Referer()[8:], user+h.cfg.Host) {
|
|
|
|
|
// err = errors.New("wrong referer")
|
|
|
|
|
// }
|
|
|
|
|
// return user, err
|
|
|
|
|
//}
|
|
|
|
|
|
2021-11-22 13:00:19 +00:00
|
|
|
func (h *Handler) getUser(r *http.Request) (string, error) {
|
|
|
|
|
user, err := h.sess.Get(r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
return user, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-22 09:06:23 +00:00
|
|
|
func New(cfg *config.Config, sess *session.Session, data *storage.Storage) (http.Handler, error) {
|
|
|
|
|
router := mux.NewRouter()
|
|
|
|
|
h := &Handler{
|
|
|
|
|
cfg: cfg,
|
|
|
|
|
mux: router,
|
|
|
|
|
storage: data,
|
|
|
|
|
sess: sess,
|
|
|
|
|
}
|
|
|
|
|
h.initTpl()
|
|
|
|
|
router.HandleFunc("/", h.showIndexView).Methods(http.MethodGet)
|
2021-11-22 09:26:30 +00:00
|
|
|
router.HandleFunc("/login", h.showLoginView).Methods(http.MethodGet)
|
|
|
|
|
router.HandleFunc("/check-login", h.checkLogin).Methods(http.MethodPost)
|
|
|
|
|
router.HandleFunc("/register", h.handleRegister)
|
|
|
|
|
router.HandleFunc("/logout", h.logout).Methods(http.MethodGet)
|
2021-11-22 13:00:19 +00:00
|
|
|
|
2021-11-25 06:37:30 +00:00
|
|
|
router.HandleFunc("/settings", h.showSettingsView).Methods(http.MethodGet)
|
|
|
|
|
router.HandleFunc("/settings-update", h.updateSettings).Methods(http.MethodPost)
|
|
|
|
|
|
2021-11-28 07:40:11 +00:00
|
|
|
router.HandleFunc("/add", h.showNewStatusView).Methods(http.MethodGet)
|
|
|
|
|
router.HandleFunc("/add", h.saveStatus).Methods(http.MethodPost)
|
|
|
|
|
router.HandleFunc("/remove", h.handleRemoveStatus)
|
|
|
|
|
router.HandleFunc("/edit", h.showEditStatusView).Methods(http.MethodGet)
|
|
|
|
|
router.HandleFunc("/edit", h.updateStatus).Methods(http.MethodPost)
|
2021-11-28 21:38:08 +00:00
|
|
|
router.HandleFunc("/current-status.js", h.showCurrentStatusJSView).Methods(http.MethodGet)
|
|
|
|
|
router.HandleFunc("/current-status", h.showCurrentStatusView).Methods(http.MethodGet)
|
2021-11-28 07:40:11 +00:00
|
|
|
router.HandleFunc("/manage", h.showManageView).Methods(http.MethodGet)
|
2021-11-28 21:38:08 +00:00
|
|
|
router.HandleFunc("/intro", h.showIntroView).Methods(http.MethodGet)
|
|
|
|
|
router.HandleFunc("/about/status-updater", h.showStatusUpdaterView).Methods(http.MethodGet)
|
2021-11-28 14:31:51 +00:00
|
|
|
router.HandleFunc("/users/{user}.atom", h.showAtomView).Methods(http.MethodGet)
|
2021-11-24 06:30:19 +00:00
|
|
|
router.HandleFunc("/users/{user}", h.showUserView).Methods(http.MethodGet)
|
|
|
|
|
router.HandleFunc("/users/{user}/status.json", h.showUserStatusView).Methods(http.MethodGet)
|
2021-11-28 14:31:51 +00:00
|
|
|
|
2021-11-25 05:51:40 +00:00
|
|
|
router.HandleFunc("/users/{user}/status.png", h.showUserStatusImageView).Methods(http.MethodGet)
|
2021-11-24 06:30:19 +00:00
|
|
|
router.PathPrefix("/assets/").Handler(
|
|
|
|
|
http.StripPrefix("/assets/",
|
|
|
|
|
http.FileServer(
|
|
|
|
|
http.Dir(cfg.AssetsDir),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
)
|
2021-11-22 09:06:23 +00:00
|
|
|
|
|
|
|
|
return router, nil
|
|
|
|
|
}
|