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.

75 lines
1.2 KiB

  1. package Webserver
  2. import (
  3. "net/http"
  4. "path/filepath"
  5. "PersonalWebsite/Variables"
  6. "github.com/gorilla/mux"
  7. )
  8. var (
  9. WebRoot string
  10. )
  11. func init() {
  12. WebRoot = filepath.Join(
  13. Variables.ProjectRoot,
  14. "/web",
  15. )
  16. }
  17. func webRootJoin(p string) string {
  18. return filepath.Join(
  19. WebRoot,
  20. p,
  21. )
  22. }
  23. func Start() error {
  24. var (
  25. r *mux.Router
  26. e error
  27. )
  28. r = mux.NewRouter()
  29. // For images / anything static
  30. r.PathPrefix("/static/").Handler(
  31. http.StripPrefix("/static/",
  32. http.FileServer(http.Dir(webRootJoin("static"))),
  33. ),
  34. )
  35. // Handle CSS
  36. r.PathPrefix("/css/").Handler(
  37. http.StripPrefix("/css/",
  38. http.FileServer(http.Dir(webRootJoin("css"))),
  39. ),
  40. )
  41. // Handle JS
  42. r.PathPrefix("/js/").Handler(
  43. http.StripPrefix("/js/",
  44. http.FileServer(http.Dir(webRootJoin("js"))),
  45. ),
  46. )
  47. // Interface endpoints
  48. r.HandleFunc("/", ViewIndex)
  49. r.HandleFunc("/post/{id}", ViewPost)
  50. // Administration
  51. r.HandleFunc("/admin", AdminView)
  52. r.HandleFunc("/admin/login", AdminLogin)
  53. r.HandleFunc("/admin/post/new", AdminNewPost)
  54. r.HandleFunc("/admin/post/{id}/edit", AdminEditPost)
  55. e = http.ListenAndServe(Variables.WebserverHost, r)
  56. if e != nil {
  57. return e
  58. }
  59. return nil
  60. }