package main import ( "fmt" "io" "io/ioutil" "net/http" "os" ) var SFU_FILES_DIR string = get_envvar_or_fatal("SFU_FILES_DIR") var SFU_PORT string = get_envvar_or_fatal("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) } for _, f := range files { fmt.Fprintf(w, "%v\n", f.Name()) } } func get_envvar_or_fatal(envvar_name string) string { envvar_value, isSet := os.LookupEnv(envvar_name) if !isSet { Error.Println(fmt.Sprintf("%v is not set", envvar_name)) os.Exit(1) } 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 } switch req.Method { case http.MethodGet: list_uploaded_files(w, req) case http.MethodPost: upload_file(w, req) default: http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) Warning.Println(fmt.Sprintf("%v not allowed", req.Method)) Error.Println(fmt.Sprintf("will return %v", http.StatusMethodNotAllowed)) } }) port := fmt.Sprintf(":%v", SFU_PORT) Info.Println(fmt.Sprintf("running SFU on port %v", port)) err := http.ListenAndServe(port, nil) if err != nil { Error.Println(fmt.Sprintf("%v port may not be available", port)) os.Exit(1) } }