1. Добавлен файл .gitignore.

2. Добавлен файл 500.html. Теперь сервер может отправить содержимое этого файла, вместо какой-либо
   HTML-страницы, если во время её открытия произошла внутренняя ошибка сервера.
This commit is contained in:
Nikita Osokin 2023-11-13 20:45:29 +05:00
parent 91e1e10501
commit 3657cb0956
3 changed files with 66 additions and 2 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
classes
main

34
500.html Normal file
View file

@ -0,0 +1,34 @@
<!DOCTYPE html>
<!--
Copyright (c) 2023 Nikita Osokin.
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or other materials provided
with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
-->
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<h1>500</h1>
<p>Произошла внутренняя ошибка сервера при открытии данной страницы. Попробуйте повторить свой запрос позже.</p>
</body>
</html>

32
main.go
View file

@ -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 {