webserver.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package kyoketsu
  2. import (
  3. "embed"
  4. "fmt"
  5. "html/template"
  6. "log"
  7. "net/http"
  8. "path"
  9. "strings"
  10. )
  11. // Holding all static web server resources
  12. //
  13. //go:embed html/bootstrap-5.0.2-dist/js/* html/bootstrap-5.0.2-dist/css/* html/htmx.min.js html/templates/* html/img/*
  14. var content embed.FS
  15. /*
  16. Run a new webserver
  17. :param port: port number to run the webserver on
  18. */
  19. func RunHttpServer(port int, dbhook TopologyDatabaseIO) {
  20. assets := &AssetHandler{Root: content, RelPath: "static", EmbedRoot: "html"}
  21. tmpl, err := template.ParseFS(content, "html/templates/*.html")
  22. if err != nil {
  23. log.Fatal(err)
  24. }
  25. htmlHndl := &HtmlHandler{Home: tmpl, DbHook: dbhook}
  26. http.Handle("/static/", assets)
  27. http.Handle("/home", htmlHndl)
  28. log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", port), nil))
  29. }
  30. // handlers //
  31. type HtmlHandler struct {
  32. Home *template.Template // pointer to the HTML homepage
  33. DbHook TopologyDatabaseIO
  34. }
  35. /*
  36. Handler function for HTML serving
  37. :param w: http.ResponseWriter interface for sending data back
  38. :param r: pointer to the http.Request coming in
  39. */
  40. func (h *HtmlHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  41. data, err := h.DbHook.All()
  42. if err != nil {
  43. fmt.Fprintf(w, "You have made it to the kyoketsu web server!\nThere was an error getting the db table, though.\n%s", err)
  44. }
  45. h.Home.Execute(w, data)
  46. }
  47. type AssetHandler struct {
  48. Root embed.FS // Should be able to use anything that implements the fs.FS interface for serving asset files
  49. EmbedRoot string // This is the root of the embeded file system
  50. RelPath string // The path that will be used for the handler, relative to the root of the webserver (/static, /assets, etc)
  51. }
  52. /*
  53. Handler function to serve out asset files (HTMX, bootstrap, pngs etc)
  54. :param w: http.ResponseWriter interface for sending data back to the caller
  55. :param r: pointer to an http.Request
  56. */
  57. func (a *AssetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  58. var uripath string // the path from the request
  59. var pathSp []string // the path from the request split, so that we can point the request path to the embedded fs
  60. var assetPath []string // the cleaned path for the requested asset
  61. var fname string // filename of the requested asset
  62. var ctype string
  63. var b []byte
  64. var err error
  65. uripath = strings.TrimPrefix(r.URL.Path, a.RelPath)
  66. uripath = strings.Trim(uripath, "/")
  67. pathSp = strings.Split(uripath, "/")
  68. fname = pathSp[len(pathSp)-1]
  69. assetPath = append(assetPath, a.EmbedRoot)
  70. for i := 1; i < len(pathSp); i++ {
  71. assetPath = append(assetPath, pathSp[i])
  72. }
  73. b, err = a.Root.ReadFile(path.Join(assetPath...))
  74. if err != nil {
  75. fmt.Fprintf(w, "Error occured: %s. path split: '%s'\nAsset Path: %s", err, pathSp, assetPath)
  76. }
  77. switch {
  78. case strings.Contains(fname, "css"):
  79. ctype = "text/css"
  80. case strings.Contains(fname, "js"):
  81. ctype = "text/javascript"
  82. case strings.Contains(fname, "html"):
  83. ctype = "text/html"
  84. case strings.Contains(fname, "json"):
  85. ctype = "application/json"
  86. case strings.Contains(fname, "png"):
  87. ctype = "image/png"
  88. default:
  89. ctype = "text"
  90. }
  91. w.Header().Add("Content-Type", ctype)
  92. fmt.Fprint(w, string(b))
  93. }