summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--\66
-rw-r--r--main.go49
2 files changed, 115 insertions, 0 deletions
diff --git a/\ b/\
new file mode 100644
index 0000000..2379089
--- /dev/null
+++ b/\
@@ -0,0 +1,66 @@
+package main
+
+import (
+ "fmt"
+ "io"
+ "log"
+ "net/http"
+ "net/url"
+ "os"
+ "os/signal"
+ "syscall"
+
+ endpoints "physick.ru/culture_exam/Endpoints"
+ settings "physick.ru/culture_exam/Settings"
+)
+
+const tgBase = "https://api.telegram.org/bot8248946057:AAHpKUlQ7V-N5hBj7rogwF0hJazL6Mq8bzc/sendMessage?chat_id=-5042695760&text=%v"
+
+func main() {
+ settings.ReadSettings()
+ endpoints.RegisterEndpoints()
+ http.Handle("/", http.FileServer(http.FS(os.DirFS("./static"))))
+ var _, _ = endpoints.GetAvaliableSongs()
+ fmt.Println(settings.Current)
+
+ sendNotification("Culture exam server started")
+
+ ch := make(chan os.Signal, 3)
+ signal.Notify(ch, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
+ go func() {
+ _ <- ch
+ sendNotification("Culture exam server stopped")
+ os.Exit(0)
+ }()
+
+ var err = http.ListenAndServe(":6969", nil)
+ if err != nil {
+ log.Println(err.Error())
+ sendNotification(fmt.Sprintf("Culture exam crashed: %v", err.Error()))
+ }
+}
+
+func sendNotification(message string) {
+ var httpClient = http.Client{}
+ var req, reqerr = http.NewRequest("GET", fmt.Sprintf(tgBase, url.PathEscape(message)), nil)
+ if reqerr != nil {
+ log.Printf("Failed to generate a tg request: %v", reqerr.Error())
+ return
+ }
+ var resp, err = httpClient.Do(req)
+ if err != nil {
+ log.Printf("Failed to execute a tg request: %v", err.Error())
+ return
+ }
+ var body = parseResponceBody(resp)
+ log.Printf(string(body))
+}
+
+func parseResponceBody(r *http.Response) []byte {
+ var buf, err = io.ReadAll(r.Body)
+ if err != nil {
+ return []byte{}
+ }
+ return buf
+}
+
diff --git a/main.go b/main.go
index 49b9217..5ff4e92 100644
--- a/main.go
+++ b/main.go
@@ -2,23 +2,72 @@ package main
import (
"fmt"
+ "io"
"log"
"net/http"
+ "net/url"
"os"
+ "os/signal"
+ "syscall"
endpoints "physick.ru/culture_exam/Endpoints"
settings "physick.ru/culture_exam/Settings"
)
+const tgBase = "https://api.telegram.org/bot8248946057:AAHpKUlQ7V-N5hBj7rogwF0hJazL6Mq8bzc/sendMessage?chat_id=-5042695760&text=%v"
+
func main() {
+ attachInterruption(func() {
+ sendNotification("Culture exam server died you dumb fuck")
+ })
+
settings.ReadSettings()
endpoints.RegisterEndpoints()
http.Handle("/", http.FileServer(http.FS(os.DirFS("./static"))))
var _, _ = endpoints.GetAvaliableSongs()
fmt.Println(settings.Current)
+ sendNotification("Culture exam server started")
+
var err = http.ListenAndServe(":6969", nil)
if err != nil {
log.Println(err.Error())
+ sendNotification(fmt.Sprintf("Culture exam crashed: %v", err.Error()))
}
}
+
+func attachInterruption(deferred func()) {
+ ch := make(chan os.Signal, 3)
+ signal.Notify(ch, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
+ go func() {
+ <- ch
+ signal.Stop(ch)
+ deferred()
+ os.Exit(0)
+ }()
+}
+
+func sendNotification(message string) {
+ var httpClient = http.Client{}
+ var req, reqerr = http.NewRequest("GET", fmt.Sprintf(tgBase, url.PathEscape(message)), nil)
+ if reqerr != nil {
+ log.Printf("Failed to generate a tg request: %v", reqerr.Error())
+ return
+ }
+ var resp, err = httpClient.Do(req)
+ if err != nil {
+ log.Printf("Failed to execute a tg request: %v", err.Error())
+ return
+ }
+ var body = parseResponceBody(resp)
+ log.Printf(string(body))
+}
+
+func parseResponceBody(r *http.Response) []byte {
+ var buf, err = io.ReadAll(r.Body)
+ if err != nil {
+ return []byte{}
+ }
+ return buf
+}
+