summaryrefslogtreecommitdiff
path: root/server.go
blob: b06692e8ed195ca6c8d0a1d20a3e8f2879279347 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main

import (
  "fmt"
  "log"
  "net/http"
  "os"
  "html/template"
  "flag"
)

var tmpl *template.Template

func handleGet( w http.ResponseWriter, r *http.Request) {

  path := r.URL.Path[1:]
  if path == "" {
    path = "."
  }
  fmt.Printf("Trying to get [%s]\n", r.URL.Path[1:])

  stat, err := os.Stat(path)

  switch {
    case err != nil || stat == nil :
      if os.IsNotExist(err) {
        w.WriteHeader(http.StatusNotFound)
        fmt.Fprintln(w, "File not found")
        fmt.Fprintln(w, "")
        fmt.Fprintln(w, "Sry bro")
        return
      }
    case stat.IsDir() :
      // If it's a collection list return the list of the collection

      type Entry struct {
        Name string
        Size int
      }

      var files []Entry
      f, err := os.Open(path)

      if err != nil {
        w.WriteHeader(http.StatusNotFound)
        fmt.Fprintln(w, "File not found")
        fmt.Fprintln(w, "")
        fmt.Fprintln(w, "Sry bro")
        return
      }

      fileInfo, err := f.Readdir(-1)
      f.Close()

      if err != nil {
        w.WriteHeader(http.StatusNotFound)
        fmt.Fprintln(w, "File not found")
        fmt.Fprintln(w, "")
        fmt.Fprintln(w, "Sry bro")
      }

      for _, file := range fileInfo {
        files = append(files, Entry{file.Name(), 0})
      }


      type Collection struct {
        Name string
        ListEntries []Entry
      }
      tmpl.Execute(w, Collection{ "Title" , files})

    case stat.Mode().IsRegular():
      http.ServeFile(w, r, r.URL.Path[1:])
    default:
  }
}
func handlePost( w http.ResponseWriter, r *http.Request) {
}
func handleDelete( w http.ResponseWriter, r *http.Request) {
}
func handlePut( w http.ResponseWriter, r *http.Request) {
}

func handleMethod( w http.ResponseWriter, r *http.Request) {
  switch r.Method {
  case http.MethodGet:
    handleGet(w, r)
  case http.MethodPost:
    handlePost(w, r)
  case http.MethodPut:
    handlePut(w, r)
  case http.MethodDelete:
    // Remove the record.
    handleDelete(w, r)
  default:
    w.WriteHeader(http.StatusTeapot)
    fmt.Fprintln(w, "Sry, i'm a teapot >///<")
    fmt.Fprintln(w, "")
    fmt.Fprintln(w, "Here's some tea. 🍵")
  }
}

func main() {
  var dir string

  const (
      defaultDir = "."
      usageDir   = "the directory to serve"
  )
  flag.StringVar(&dir, "directory", defaultDir, usageDir)
  flag.StringVar(&dir, "d", defaultDir, usageDir+" (shorthand)")

 flag.Parse()

 if dir == "" {
  dir = defaultDir
 }

  tmpl = template.Must(template.ParseFiles("dir.html"))

  fmt.Println("Launch server")
  http.HandleFunc("/", handleMethod)
  log.Fatal(http.ListenAndServe(":8080", nil))
  fmt.Println("Bye bye")
}