78 lines
2.3 KiB
Go
78 lines
2.3 KiB
Go
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
|
|
//}
|
|
|
|
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()
|
|
|
|
// Index
|
|
router.HandleFunc("/", h.showIndexView).Methods(http.MethodGet)
|
|
//router.HandleFunc("/login", h.showLoginView).Methods(http.MethodGet)
|
|
//router.HandleFunc("/check-login", h.checkLogin).Methods(http.MethodPost)
|
|
//router.HandleFunc("/help", h.showHelpView).Methods(http.MethodGet)
|
|
//router.HandleFunc("/profile/{username}", h.showProfileView).Methods(http.MethodGet)
|
|
//router.HandleFunc("/register", h.handleRegister)
|
|
//router.HandleFunc("/editor", h.handleEditor)
|
|
//router.HandleFunc("/upload", h.upload)
|
|
//router.HandleFunc("/files", h.handleFiles)
|
|
//router.HandleFunc("/new-folder", h.handleNewFolder)
|
|
//router.HandleFunc("/new-file", h.handleNewFile)
|
|
//router.HandleFunc("/homepages", h.showHomepagesView).Methods(http.MethodGet)
|
|
//router.HandleFunc("/rename", h.handleRename)
|
|
//router.HandleFunc("/delete", h.handleDelete).Methods(http.MethodPost)
|
|
//router.HandleFunc("/logout", h.logout).Methods(http.MethodGet)
|
|
//router.PathPrefix("/").Handler(http.FileServer(http.Dir(cfg.AssetsDir)))
|
|
|
|
return router, nil
|
|
}
|