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.

77 lines
1.3 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("/programming", ViewPostListProgramming)
  50. r.HandleFunc("/pentesting", ViewPostListPentesting)
  51. r.HandleFunc("/post/{id}", ViewPost)
  52. // Administration
  53. r.HandleFunc("/admin", AdminView)
  54. r.HandleFunc("/admin/login", AdminLogin)
  55. r.HandleFunc("/admin/post/new", AdminNewPost)
  56. r.HandleFunc("/admin/post/{id}/edit", AdminEditPost)
  57. e = http.ListenAndServe(Variables.WebserverHost, r)
  58. if e != nil {
  59. return e
  60. }
  61. return nil
  62. }