www.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package www
  2. import (
  3. "log"
  4. "net/http"
  5. "path/filepath"
  6. "strings"
  7. "imuslab.com/arozos/mod/user"
  8. )
  9. /*
  10. www package
  11. This package is the replacement handler for global homepage function in ArozOS.
  12. This allow users to host and create their website using any folder within the user
  13. access file system.
  14. */
  15. type Options struct {
  16. UserHandler *user.UserHandler
  17. }
  18. type Handler struct {
  19. Options Options
  20. }
  21. /*
  22. New WebRoot Handler create a new handler for handling and routing webroots
  23. */
  24. func NewWebRootHandler(options Options) *Handler {
  25. return &Handler{
  26. Options: options,
  27. }
  28. }
  29. func (h *Handler) RouteRequest(w http.ResponseWriter, r *http.Request) {
  30. //Check if it is reaching www root folder or any files directly under www.
  31. if filepath.ToSlash(filepath.Clean(r.RequestURI)) == "/www" {
  32. //Direct access of the root folder. Serve the homepage description.
  33. http.ServeFile(w, r, "web/SystemAO/www/index.html")
  34. return
  35. } else if filepath.ToSlash(filepath.Dir(r.RequestURI)) == "/www" {
  36. //Reaching file under www root and not root. Redirect to www root
  37. http.Redirect(w, r, "/www/", 307)
  38. return
  39. }
  40. //Check the user name of the user root
  41. parsedRequestURL := strings.Split(filepath.ToSlash(filepath.Clean(r.RequestURI)[1:]), "/")
  42. //Malparsed URL. Ignore request
  43. if len(parsedRequestURL) < 2 {
  44. http.NotFound(w, r)
  45. return
  46. }
  47. //Extract user information
  48. username := parsedRequestURL[1]
  49. _, err := h.Options.UserHandler.GetUserInfoFromUsername(username)
  50. if err != nil {
  51. http.NotFound(w, r)
  52. return
  53. }
  54. log.Println("Serving user webroot: ", username)
  55. sendOK(w)
  56. }