server.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "html/template"
  6. "log"
  7. "net/http"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. )
  12. var host = flag.String("h", "localhost", "Host on which to serve")
  13. var port = flag.String("p", "9090", "Port on which to serve")
  14. var mainFolder = flag.String("f", "", "Absolute path to where files are kept")
  15. var logFilePath = flag.String("l", "", "Path to log file")
  16. type paths struct {
  17. textFile string
  18. htmlFile string
  19. }
  20. func getPaths(folder string) (paths, error) {
  21. destination := filepath.Join(*mainFolder, folder)
  22. if !strings.HasPrefix(destination, *mainFolder) {
  23. return paths{}, fmt.Errorf("Wrong destination: %s", destination)
  24. }
  25. if _, err := os.Stat(destination); os.IsNotExist(err) {
  26. return paths{}, fmt.Errorf("Destination folder doesn't exist at %s!", destination)
  27. }
  28. textFile := filepath.Join(destination, "index.txt")
  29. if _, err := os.Stat(textFile); os.IsNotExist(err) {
  30. return paths{}, fmt.Errorf("Text file doesn't exist at %s!", textFile)
  31. }
  32. return paths{textFile, filepath.Join(destination, "index.html")}, nil
  33. }
  34. type params struct {
  35. folder string
  36. option string
  37. }
  38. func getParameters(path string) (params, error) {
  39. if path == "" {
  40. return params{}, fmt.Errorf("Empty string!")
  41. }
  42. parts := strings.Split(path, "/")
  43. partsCount := len(parts)
  44. if partsCount > 2 {
  45. return params{}, fmt.Errorf("Too many parameters: %s", parts)
  46. }
  47. if partsCount == 2 {
  48. return params{parts[0], parts[1]}, nil
  49. }
  50. return params{parts[0], ""}, nil
  51. }
  52. func serve(w http.ResponseWriter, r *http.Request) {
  53. pathFromURL := strings.TrimPrefix(r.URL.Path, "/")
  54. t := template.Must(template.ParseFiles("./html/result.html"))
  55. parameters, err := getParameters(pathFromURL)
  56. if err != nil {
  57. log.Println(err)
  58. t.Execute(w, "Bad parameters!")
  59. return
  60. }
  61. paths, err := getPaths(parameters.folder)
  62. if err != nil {
  63. log.Println(err)
  64. w.WriteHeader(http.StatusNotFound)
  65. t.Execute(w, "Not found!")
  66. return
  67. }
  68. if parameters.option == "t" {
  69. http.ServeFile(w, r, paths.textFile)
  70. return
  71. }
  72. if _, err := os.Stat(paths.htmlFile); os.IsNotExist(err) {
  73. err := convert(paths)
  74. if err != nil {
  75. log.Println(err)
  76. http.ServeFile(w, r, paths.textFile)
  77. return
  78. }
  79. }
  80. http.ServeFile(w, r, paths.htmlFile)
  81. }
  82. func setLog() *os.File {
  83. if *logFilePath == "" {
  84. return nil
  85. }
  86. file, err := os.OpenFile(*logFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
  87. if err != nil {
  88. log.Fatalf("Can't open logs file: %s", err)
  89. }
  90. log.SetOutput(file)
  91. return file
  92. }
  93. func main() {
  94. flag.Parse()
  95. logFile := setLog()
  96. if logFile != nil {
  97. defer logFile.Close()
  98. }
  99. hostname := *host + ":" + *port
  100. http.HandleFunc("/", serve)
  101. log.Fatal(http.ListenAndServe(hostname, nil))
  102. }