From 91e1e10501a46988ee482cc006f1d2f6f0fa44cc Mon Sep 17 00:00:00 2001 From: Nikita Osokin Date: Fri, 10 Nov 2023 23:11:24 +0500 Subject: [PATCH] =?UTF-8?q?=20*=20=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8F?= =?UTF-8?q?=20respondWithFile.=20=20*=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D1=8B=20=D0=B7=D0=B0=D0=BA=D1=80=D1=8B=D1=82?= =?UTF-8?q?=D0=B8=D1=8F=20=D1=84=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2,=20=D0=B1?= =?UTF-8?q?=D0=B5=D0=B7=20=D0=BA=D0=BE=D1=82=D0=BE=D1=80=D1=8B=D1=85=20?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B3=D1=80=D0=B0=D0=BC=D0=BC=D0=B0=20=D0=BC?= =?UTF-8?q?=D0=BE=D0=B3=D0=BB=D0=B0=20=D0=B7=D0=B0=20=D0=BF=D1=80=D0=BE?= =?UTF-8?q?=D0=B4=D0=BE=D0=BB=D0=B6=D0=B8=D1=82=D0=B5=D0=BB=D1=8C=D0=BD?= =?UTF-8?q?=D0=BE=D0=B5=20=D0=B2=D1=80=D0=B5=D0=BC=D1=8F=20=D1=80=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D1=8B=20=D0=B4=D0=BE=D1=81=D1=82=D0=B8=D1=87?= =?UTF-8?q?=D1=8C=20=D0=BB=D0=B8=D0=BC=D0=B8=D1=82=D0=B0=20=D0=BF=D0=BE=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BB=D0=B8=D1=87=D0=B5=D1=81=D1=82=D0=B2=D1=83=20?= =?UTF-8?q?=D0=BE=D1=82=D0=BA=D1=80=D1=8B=D1=82=D1=8B=D1=85=20=D1=84=D0=B0?= =?UTF-8?q?=D0=B9=D0=BB=D0=BE=D0=B2.=20=20*=20=D0=9F=D0=B0=D0=BC=D1=8F?= =?UTF-8?q?=D1=82=D1=8C,=20=D0=B7=D0=B0=D0=BD=D1=8F=D1=82=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D0=BC=D0=B8,=20=D0=BD=D1=83?= =?UTF-8?q?=D0=B6=D0=BD=D1=8B=D0=BC=D0=B8=20=D1=82=D0=BE=D0=BB=D1=8C=D0=BA?= =?UTF-8?q?=D0=BE=20=D0=B4=D0=BB=D1=8F=20=D0=B8=D0=BD=D0=B8=D1=86=D0=B8?= =?UTF-8?q?=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D0=B8=20=D1=82=D0=B5?= =?UTF-8?q?=D0=BF=D0=B5=D1=80=D1=8C=20=D0=BE=D1=81=D0=B2=D0=BE=D0=B1=D0=BE?= =?UTF-8?q?=D0=B6=D0=B4=D0=B0=D0=B5=D1=82=D1=81=D1=8F,=20=D0=BA=D0=BE?= =?UTF-8?q?=D0=B3=D0=B4=D0=B0=20=D1=8D=D1=82=D0=B8=20=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=BD=D1=8B=D0=B5=20=D1=81=D1=82=D0=B0=D0=BD=D0=BE=D0=B2=D1=8F?= =?UTF-8?q?=D1=82=D1=81=D1=8F=20=D0=BD=D0=B5=D0=BD=D1=83=D0=B6=D0=BD=D1=8B?= =?UTF-8?q?=D0=BC=D0=B8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index 829dea5..e5c166a 100644 --- a/main.go +++ b/main.go @@ -118,38 +118,38 @@ func writeUint64BE(writer io.Writer, num uint64) error { return nil } -func respondWithFile(writer http.ResponseWriter, request *http.Request, filename string) { +func respondWithFile(writer http.ResponseWriter, filename string) { file, err := os.Open(filename) if err != nil { - fmt.Println(" !!! Файл", filename, "не найден!") + writer.WriteHeader(http.StatusInternalServerError) return } stat, err := file.Stat() if err != nil { - fmt.Println(" !!! Не удалось получить информацию о файле", filename + "!") + writer.WriteHeader(http.StatusInternalServerError) + file.Close() return } buffer := make([]byte, stat.Size()) - count, err := file.Read(buffer); + _, err = file.Read(buffer); if err != nil { - fmt.Println(" !!! Не удалось прочитать содержимое файла", filename + "!") + writer.WriteHeader(http.StatusInternalServerError) + file.Close() return } - if int64(count) != stat.Size() { - fmt.Println(" *** Файл", filename, "был прочитан не полностью.") - } writer.Write(buffer) + file.Close() } func handleRootAnd404(writer http.ResponseWriter, request *http.Request) { if request.URL.Path != "/" { writer.WriteHeader(http.StatusNotFound) - respondWithFile(writer, request, "404.html") + respondWithFile(writer, "404.html") } else { - respondWithFile(writer, request, "index.html") + respondWithFile(writer, "index.html") } } @@ -325,12 +325,12 @@ func handleEditPage(writer http.ResponseWriter, request *http.Request) { classesRWMutexes[queryValues["c"][0]].Unlock() } else { - respondWithFile(writer, request, "editpage.html") + respondWithFile(writer, "editpage.html") } } /* TODO: в эту и похожие функции нужно добавть возможность отправить статус 500 */ func handleSchedule(writer http.ResponseWriter, request *http.Request) { - respondWithFile(writer, request, "schedule.html") + respondWithFile(writer, "schedule.html") } /* TODO: возможно стоит отправлять что-то кроме просто ошибки 500 */ @@ -385,6 +385,7 @@ func main() { } classes, err = classesDir.ReadDir(0) + classesDir.Close() if err != nil { fmt.Printf("[!!!]Не удалось прочитать содержимое директории classes.\n") return @@ -400,6 +401,8 @@ func main() { classesNames[i] = classes[i].Name() classesRWMutexes[classes[i].Name()] = new(sync.RWMutex) } + + classes = nil http.HandleFunc("/", handleRootAnd404) http.HandleFunc("/getClassesList", handleGetClassesList)