feat: add /files/{file} GET and DELETE methods

This commit is contained in:
cătălin 2022-06-28 14:11:37 +02:00
commit 4a4a0a2149
No known key found for this signature in database
GPG key ID: C378F1E869F05A95
7 changed files with 252 additions and 60 deletions

114
main.go
View file

@ -2,44 +2,17 @@ package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"regexp"
"strings"
)
var SFU_FILES_DIR string = get_envvar_or_fatal("SFU_FILES_DIR")
var SFU_PORT string = get_envvar_or_fatal("SFU_PORT")
var SFU_FILES_DIR string = getEnvvar("SFU_FILES_DIR")
var SFU_PORT string = getEnvvar("SFU_PORT")
func upload_file(w http.ResponseWriter, req *http.Request) {
file, fileHeader, err := req.FormFile("file")
if err != nil {
Error.Println(err)
}
defer file.Close()
f, err := os.OpenFile(fmt.Sprintf("%v/%v", SFU_FILES_DIR, fileHeader.Filename), os.O_WRONLY|os.O_CREATE, 0666)
defer f.Close()
io.Copy(f, file)
w.WriteHeader(http.StatusCreated)
}
func list_uploaded_files(w http.ResponseWriter, req *http.Request) {
Info.Println(fmt.Sprintf("server: will list uploaded files on %v", SFU_FILES_DIR))
files, err := ioutil.ReadDir(SFU_FILES_DIR)
if err != nil {
Warning.Println(fmt.Sprintf("%v does not exist", SFU_FILES_DIR))
Info.Println(fmt.Sprintf("will create %v", SFU_FILES_DIR))
_ = os.Mkdir(SFU_FILES_DIR, os.ModePerm)
}
fmt.Fprint(w, "<html><body><ol>")
for _, f := range files {
fmt.Fprintf(w, "<li><a href='foo.barz'>%v</a></li>\n", f.Name())
}
fmt.Fprint(w, "</ol></body></html>")
}
func get_envvar_or_fatal(envvar_name string) string {
func getEnvvar(envvar_name string) string {
Info.Printf("getting envvar %v", envvar_name)
envvar_value, isSet := os.LookupEnv(envvar_name)
if !isSet {
Error.Println(fmt.Sprintf("%v is not set", envvar_name))
@ -48,31 +21,74 @@ func get_envvar_or_fatal(envvar_name string) string {
return envvar_value
}
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
path := req.URL.Path
Info.Println(fmt.Sprintf("received %v on %v", req.Method, path))
if path != "/" {
err_msg := fmt.Sprintf("path %v does not exist", path)
http.Error(w, err_msg, http.StatusNotFound)
Warning.Println(err_msg)
Error.Println(fmt.Sprintf("will return %v", http.StatusNotFound))
return
func emptyArray(s []string) []string {
var r []string
for _, str := range s {
if str != "" {
r = append(r, str)
}
switch req.Method {
}
return r
}
func routeFiles(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
Info.Println(fmt.Sprintf("received %v on %v", r.Method, path))
paths := emptyArray(strings.Split(path, "/"))
Info.Println(fmt.Sprintf("paths %v", paths))
switch len(paths) {
case 1:
switch r.Method {
case http.MethodGet:
list_uploaded_files(w, req)
listFiles(w, r)
case http.MethodPost:
upload_file(w, req)
uploadFile(w, r)
default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
Warning.Println(fmt.Sprintf("%v not allowed", req.Method))
Warning.Println(fmt.Sprintf("%v not allowed", r.Method))
Error.Println(fmt.Sprintf("will return %v", http.StatusMethodNotAllowed))
}
})
return
case 2:
switch r.Method {
case http.MethodGet:
downloadFile(w, r, paths[1])
case http.MethodDelete:
deleteFile(w, r, paths[1])
default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
Warning.Println(fmt.Sprintf("%v not allowed", r.Method))
Error.Println(fmt.Sprintf("will return %v", http.StatusMethodNotAllowed))
}
default:
http.Error(w, "Not found", http.StatusNotFound)
}
}
func route(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
switch {
case path == "/":
fmt.Fprintf(w, "Hello, world!")
case path == "/health":
Info.Println("health check OK")
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
case regexp.MustCompile("^/files").MatchString(path):
Info.Println("received request for files, will call files router")
routeFiles(w, r)
default:
w.WriteHeader(http.StatusNotFound)
}
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", route)
port := fmt.Sprintf(":%v", SFU_PORT)
Info.Println(fmt.Sprintf("running SFU on port %v", port))
err := http.ListenAndServe(port, nil)
err := http.ListenAndServe(port, mux)
if err != nil {
Error.Println(fmt.Sprintf("%v port may not be available", port))
os.Exit(1)