-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
124 lines (107 loc) · 3.34 KB
/
main.go
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
package main
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"github.com/kelseyhightower/envconfig"
"github.com/rs/zerolog/log"
"net/http"
)
type Specification struct {
BasePath string `default:"/tmp/gota"`
Port int `default:"8080"`
FormField string `default:"file"`
MaxFileSizeMB int64 `default:"5"`
}
var config = Specification{}
func initConfig() error {
return envconfig.Process("gota", &config)
}
func setupHttpServer() {
http.HandleFunc("/upload", postHandler())
http.HandleFunc("/download/", getHandler())
}
func main() {
err := initConfig()
if err != nil {
log.Fatal().Err(err).Msg("failed to process env config")
}
err = os.Mkdir(config.BasePath, 0700)
if err != nil && !errors.Is(err, os.ErrExist) {
log.Fatal().Err(err).Msg("failed to create base path")
}
setupHttpServer()
// start http server
log.Info().Msg("Starting http server")
log.Fatal().Err(http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil)).Msg("failed to start http server")
}
func getHandler() http.HandlerFunc {
log := log.With().Str("handler", "getHandler").Logger()
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
log.Warn().Msg("Method not allowed")
return
}
log.Info().Str("path", r.URL.Path).Msg("GET request")
http.StripPrefix("/download/", http.FileServer(http.Dir(config.BasePath))).ServeHTTP(w, r)
}
}
func postHandler() http.HandlerFunc {
log := log.With().Str("handler", "postHandler").Logger()
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
log.Warn().Msg("Method not allowed")
return
}
file, info, err := r.FormFile(config.FormField)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Error while uploading"))
log.Warn().Err(err).Msg("Error while uploading")
return
}
defer file.Close()
if info.Size > config.MaxFileSizeMB*1024*1024 {
log.Warn().Int64("maxFileSizeMB", config.MaxFileSizeMB).Int64("fileSize", info.Size).Msg("File size is too big")
w.WriteHeader(http.StatusRequestEntityTooLarge)
w.Write([]byte("Error file size is too big"))
return
}
body, err := ioutil.ReadAll(file)
if err != nil {
log.Warn().Err(err).Msg("failed to read the uploaded file")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Error while uploading"))
return
}
filename := info.Filename
if filename == "" {
log.Warn().Err(err).Msg("filename is empty")
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Filename is empty"))
return
}
dstPath := path.Join(config.BasePath, filename)
dstFile, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
log.Warn().Err(err).Msg("failed to create the file")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Error could not create the file"))
return
}
defer dstFile.Close()
if _, err := dstFile.Write(body); err != nil {
log.Warn().Err(err).Msg("failed to write the file")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Error while writing the file"))
return
}
log.Info().Str("path", dstPath).Msg("File uploaded successfully")
w.WriteHeader(http.StatusOK)
w.Write([]byte("File uploaded successfully"))
}
}