main.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Simpliest server
  2. package main
  3. //go:generate folder2go assets binAssets
  4. import (
  5. "fmt"
  6. "hexasoftware/cmd/httpServe/binAssets"
  7. _ "hexasoftware/lib/prettylog/global"
  8. "io"
  9. "io/ioutil"
  10. "log"
  11. "mime"
  12. "net"
  13. "net/http"
  14. "os"
  15. "path/filepath"
  16. "strings"
  17. )
  18. func CreateHandleFunc(prefix string) func(http.ResponseWriter, *http.Request) {
  19. return func(w http.ResponseWriter, r *http.Request) {
  20. var solvedPath = r.URL.Path
  21. if solvedPath == prefix {
  22. solvedPath = prefix + "/index.html"
  23. }
  24. log.Printf("%s - (embed)%s", r.Method, r.URL.Path)
  25. if strings.HasPrefix(solvedPath, prefix) {
  26. solvedPath = solvedPath[len(prefix):]
  27. }
  28. data, ok := binAssets.Data[solvedPath]
  29. if !ok {
  30. w.WriteHeader(404)
  31. }
  32. w.Header().Set("Content-type", mime.TypeByExtension(filepath.Ext(solvedPath)))
  33. w.Write(data)
  34. }
  35. }
  36. func HandleMarkDown(w http.ResponseWriter, r *http.Request, path string) error {
  37. log.Println("Handling markdown")
  38. f, err := os.Open(path)
  39. if err != nil {
  40. return err
  41. }
  42. defer f.Close()
  43. /* // Server side markdown2html
  44. data, err := ioutil.ReadAll(f)
  45. if err != nil {
  46. return err
  47. }
  48. w.Write([]byte("<html><head><link rel='stylesheet' href='/.httpServe/strapdown.css'><link rel='stylesheet' href='/.httpServe/themes/united.min.css'></head><body>"))
  49. mdData := blackfriday.MarkdownCommon(data)
  50. w.Write(mdData)
  51. w.Write([]byte("</body></html>"))
  52. return nil
  53. */
  54. w.Write([]byte(
  55. `
  56. <!DOCTYPE html>
  57. <html>
  58. <xmp theme="paper" style="display:none;">
  59. `))
  60. io.Copy(w, f)
  61. w.Write([]byte(
  62. `
  63. </xmp>
  64. <script src=".httpServe/strapdown.js"></script>
  65. </html>`))
  66. return nil
  67. }
  68. func HandleFolder(w http.ResponseWriter, r *http.Request, path string) error {
  69. res, err := ioutil.ReadDir(path)
  70. if err != nil {
  71. return err
  72. }
  73. w.Write([]byte(
  74. `<html>
  75. <body>
  76. <ul>`))
  77. for _, f := range res {
  78. w.Write([]byte(fmt.Sprintf(`<li><a href="/%s">%s</a>`, path+"/"+f.Name(), f.Name())))
  79. }
  80. w.Write([]byte(
  81. `</ul>
  82. </body>
  83. </html>
  84. `))
  85. return nil
  86. }
  87. type FileServer struct {
  88. }
  89. func (FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  90. path := r.URL.Path[1:]
  91. if path == "" {
  92. path = "index.html"
  93. if _, err := os.Stat(path); os.IsNotExist(err) {
  94. path = "."
  95. }
  96. }
  97. if strings.Contains(path, "..") {
  98. http.ServeFile(w, r, path)
  99. }
  100. log.Printf("%s - %s", r.Method, r.URL.Path)
  101. fstat, err := os.Stat(path)
  102. if err != nil {
  103. log.Println("ERR:", err)
  104. http.ServeFile(w, r, path)
  105. return
  106. }
  107. if fstat.IsDir() {
  108. HandleFolder(w, r, path)
  109. return
  110. }
  111. if filepath.Ext(path) == ".md" {
  112. err := HandleMarkDown(w, r, path)
  113. if err != nil {
  114. http.ServeFile(w, r, path)
  115. }
  116. return
  117. }
  118. // Default file server
  119. http.ServeFile(w, r, path)
  120. }
  121. func main() {
  122. mux := http.NewServeMux()
  123. mux.HandleFunc("/.httpServe/", CreateHandleFunc("/.httpServe"))
  124. mux.Handle("/", FileServer{})
  125. var port = 8080
  126. for {
  127. addr := fmt.Sprintf(":%d", port)
  128. listener, err := net.Listen("tcp", addr)
  129. if err != nil {
  130. log.Println("Err opening", port, err)
  131. port++
  132. log.Println("Trying port", port)
  133. continue
  134. }
  135. log.Println("Listening with port:", port)
  136. http.Serve(listener, mux)
  137. }
  138. //http.ListenAndServe(":8080", http.FileServer(http.Dir('.')))
  139. }