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.

79 lines
1.4 KiB

  1. package webserver
  2. import (
  3. "net/http"
  4. "path/filepath"
  5. "git.tovijaeschke.xyz/tovi/personal_website/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. // Public endpoints
  48. r.HandleFunc("/", ViewIndex)
  49. r.HandleFunc("/links", ViewLinks)
  50. r.HandleFunc("/programming", ViewPostListProgramming)
  51. r.HandleFunc("/pentesting", ViewPostListPentesting)
  52. r.HandleFunc("/post/{id}", ViewPost)
  53. r.HandleFunc("/error", ViewError)
  54. // Administration
  55. r.HandleFunc("/admin", AdminView)
  56. r.HandleFunc("/admin/login", AdminLogin)
  57. r.HandleFunc("/admin/logout", AdminLogout)
  58. r.HandleFunc("/admin/post/new", AdminNewPost)
  59. r.HandleFunc("/admin/post/{id}/edit", AdminEditPost)
  60. e = http.ListenAndServe(variables.WebserverHost, r)
  61. if e != nil {
  62. return e
  63. }
  64. return nil
  65. }