Publish HTML quickly. https://html.house
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

73 lines
1.7 KiB

  1. package htmlhouse
  2. import (
  3. "net/http"
  4. "github.com/gorilla/mux"
  5. "github.com/writeas/impart"
  6. )
  7. type app struct {
  8. cfg *config
  9. router *mux.Router
  10. }
  11. func newApp() (*app, error) {
  12. var err error
  13. app := &app{}
  14. app.cfg, err = newConfig()
  15. if err != nil {
  16. return app, err
  17. }
  18. app.initRouter()
  19. return app, nil
  20. }
  21. func (app *app) initRouter() {
  22. app.router = mux.NewRouter()
  23. /*
  24. api := app.router.PathPrefix("/api/").Subrouter()
  25. anon := api.PathPrefix("/anon/").Subrouter()
  26. anon.HandleFunc("/signup", app.handler(signupAnonymous, authLevelNone)).Methods("POST").Name("signupAnon")
  27. auth := api.PathPrefix("/auth/").Subrouter()
  28. // auth.HandleFunc("/", app.handler(login, authLevelNone)).Methods("POST").Name("login")
  29. auth.HandleFunc("/", app.handler(logout, authLevelUser)).Methods("DELETE").Name("logout")
  30. auth.HandleFunc("/signup", app.handler(signup, authLevelNone)).Methods("POST").Name("signup")
  31. users := api.PathPrefix("/users/").Subrouter()
  32. users.HandleFunc("/{username:[A-Za-z0-9.-]+}", app.handler(userByUsername, authLevelNone)).Methods("GET").Name("user")
  33. */
  34. app.router.PathPrefix("/").Handler(http.FileServer(http.Dir(app.cfg.StaticDir)))
  35. }
  36. type handlerFunc func(w http.ResponseWriter, r *http.Request) error
  37. func (app *app) handler(h handlerFunc) http.HandlerFunc {
  38. return func(w http.ResponseWriter, r *http.Request) {
  39. handleError(w, r, func() error {
  40. return h(w, r)
  41. }())
  42. }
  43. }
  44. func handleError(w http.ResponseWriter, r *http.Request, err error) {
  45. if err == nil {
  46. return
  47. }
  48. if err, ok := err.(impart.HTTPError); ok {
  49. impart.WriteError(w, err)
  50. return
  51. }
  52. impart.WriteError(w, impart.HTTPError{http.StatusInternalServerError, "This is an unhelpful error message for a miscellaneous internal error."})
  53. }