From 3657cb09566186111d3b4a4f77d6b76a6c31137f Mon Sep 17 00:00:00 2001 From: Nikita Osokin Date: Mon, 13 Nov 2023 20:45:29 +0500 Subject: [PATCH] =?UTF-8?q?1.=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D1=84=D0=B0=D0=B9=D0=BB=20.gitignore.=202.=20=D0=94?= =?UTF-8?q?=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=20=D1=84=D0=B0=D0=B9?= =?UTF-8?q?=D0=BB=20500.html.=20=D0=A2=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20?= =?UTF-8?q?=D1=81=D0=B5=D1=80=D0=B2=D0=B5=D1=80=20=D0=BC=D0=BE=D0=B6=D0=B5?= =?UTF-8?q?=D1=82=20=D0=BE=D1=82=D0=BF=D1=80=D0=B0=D0=B2=D0=B8=D1=82=D1=8C?= =?UTF-8?q?=20=D1=81=D0=BE=D0=B4=D0=B5=D1=80=D0=B6=D0=B8=D0=BC=D0=BE=D0=B5?= =?UTF-8?q?=20=D1=8D=D1=82=D0=BE=D0=B3=D0=BE=20=D1=84=D0=B0=D0=B9=D0=BB?= =?UTF-8?q?=D0=B0,=20=D0=B2=D0=BC=D0=B5=D1=81=D1=82=D0=BE=20=D0=BA=D0=B0?= =?UTF-8?q?=D0=BA=D0=BE=D0=B9-=D0=BB=D0=B8=D0=B1=D0=BE=20=20=20=20HTML-?= =?UTF-8?q?=D1=81=D1=82=D1=80=D0=B0=D0=BD=D0=B8=D1=86=D1=8B,=20=D0=B5?= =?UTF-8?q?=D1=81=D0=BB=D0=B8=20=D0=B2=D0=BE=20=D0=B2=D1=80=D0=B5=D0=BC?= =?UTF-8?q?=D1=8F=20=D0=B5=D1=91=20=D0=BE=D1=82=D0=BA=D1=80=D1=8B=D1=82?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=BF=D1=80=D0=BE=D0=B8=D0=B7=D0=BE=D1=88=D0=BB?= =?UTF-8?q?=D0=B0=20=D0=B2=D0=BD=D1=83=D1=82=D1=80=D0=B5=D0=BD=D0=BD=D1=8F?= =?UTF-8?q?=D1=8F=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B0=20=D1=81=D0=B5?= =?UTF-8?q?=D1=80=D0=B2=D0=B5=D1=80=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 ++ 500.html | 34 ++++++++++++++++++++++++++++++++++ main.go | 32 ++++++++++++++++++++++++++++++-- 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 500.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..211479d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +classes +main diff --git a/500.html b/500.html new file mode 100644 index 0000000..8159b68 --- /dev/null +++ b/500.html @@ -0,0 +1,34 @@ + + + + + + + + +

500

+

Произошла внутренняя ошибка сервера при открытии данной страницы. Попробуйте повторить свой запрос позже.

+ + + diff --git a/main.go b/main.go index e5c166a..4f26509 100644 --- a/main.go +++ b/main.go @@ -36,6 +36,7 @@ import ( var schedFileRWMutex sync.RWMutex var classesRWMutexes map[string]*sync.RWMutex var classesNames []string +var error500Text []byte func isValidClass(class string) bool { for i := 0; i < len(classesNames); i++ { @@ -122,12 +123,14 @@ func respondWithFile(writer http.ResponseWriter, filename string) { file, err := os.Open(filename) if err != nil { writer.WriteHeader(http.StatusInternalServerError) + writer.Write(error500Text) return } stat, err := file.Stat() if err != nil { writer.WriteHeader(http.StatusInternalServerError) + writer.Write(error500Text) file.Close() return } @@ -136,6 +139,7 @@ func respondWithFile(writer http.ResponseWriter, filename string) { _, err = file.Read(buffer); if err != nil { writer.WriteHeader(http.StatusInternalServerError) + writer.Write(error500Text) file.Close() return } @@ -328,7 +332,7 @@ func handleEditPage(writer http.ResponseWriter, request *http.Request) { respondWithFile(writer, "editpage.html") } } -/* TODO: в эту и похожие функции нужно добавть возможность отправить статус 500 */ + func handleSchedule(writer http.ResponseWriter, request *http.Request) { respondWithFile(writer, "schedule.html") } @@ -371,12 +375,36 @@ func handleScheduleGet(writer http.ResponseWriter, request *http.Request) { schedFile.Close() } +// TODO: Переформатировать эту функцию. func main() { + var err error + + error500File, err := os.Open("500.html") + if err != nil { + fmt.Printf("[!!!]Не удалось открыть файл 500.html.\n") + return + } + + error500FileStat, err := error500File.Stat() + if err != nil { + error500File.Close() + fmt.Printf("[!!!]Не удалось прочитать информацию о файле 500.html.\n") + return + } + error500Text = make([]byte, error500FileStat.Size()) + + _, err = error500File.Read(error500Text); + if err != nil { + error500File.Close() + fmt.Printf("[!!!]Не удалось прочитать файл 500.html.\n") + return + } + error500File.Close() + classesRWMutexes = make(map[string]*sync.RWMutex) var classesDir *os.File var classes []os.DirEntry - var err error classesDir, err = os.Open("classes") if err != nil {