oopsie/main.go

62 lines
1.3 KiB
Go
Raw Normal View History

package main
import (
2024-05-14 12:52:36 +01:00
"embed"
2024-05-12 16:54:00 +01:00
"encoding/json"
2024-05-14 12:52:36 +01:00
"html/template"
"net/http"
2024-05-08 09:00:58 +01:00
_ "github.com/mattn/go-sqlite3"
"lewisdale.dev/oopsie/data"
2024-05-09 07:12:24 +01:00
"lewisdale.dev/oopsie/ping"
2024-05-08 09:00:58 +01:00
"lewisdale.dev/oopsie/sites"
)
2024-05-14 12:52:36 +01:00
//go:embed templates/*
2024-05-15 07:56:14 +01:00
var templates embed.FS
//go:embed static/*
var staticFiles embed.FS
2024-05-14 12:52:36 +01:00
func main() {
2024-05-08 09:00:58 +01:00
db := data.Connect("test.sqlite3")
2024-05-09 07:12:24 +01:00
db.Exec("PRAGMA foreign_keys = ON;")
2024-05-08 09:00:58 +01:00
sites.CreateTable(db)
2024-05-09 07:12:24 +01:00
ping.CreateTable(db)
2024-05-08 09:00:58 +01:00
site := sites.Site{Name: "Lewisdale.dev", Url: "https://lewisdale.dev"}
site.Save(db)
2024-05-10 09:02:02 +01:00
ping.SendPing(db, site)
failureSite := sites.Site{Name: "Example", Url: "https://notreal.tld"}
failureSite.Save(db)
ping.SendPing(db, failureSite)
2024-05-15 07:56:14 +01:00
http.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) {
2024-05-13 07:37:11 +01:00
pings := ping.ListGroupedBySite(db)
2024-05-12 16:54:00 +01:00
2024-05-14 12:52:36 +01:00
if _, err := json.Marshal(pings); err != nil {
2024-05-12 16:54:00 +01:00
w.Write([]byte(err.Error()))
w.WriteHeader(http.StatusInternalServerError)
return
} else {
2024-05-14 12:52:36 +01:00
w.Header().Set("Content-Type", "text/html")
2024-05-15 07:56:14 +01:00
t, _ := template.ParseFS(templates, "templates/index.html", "templates/base.html", "templates/head.html")
2024-05-14 12:52:36 +01:00
t.Execute(w, nil)
2024-05-12 16:54:00 +01:00
}
2024-05-05 08:41:01 +01:00
})
2024-05-08 09:00:58 +01:00
2024-05-15 07:56:14 +01:00
http.HandleFunc("POST /{$}", func(w http.ResponseWriter, r *http.Request) {
2024-05-05 08:41:01 +01:00
w.Write([]byte("This was a POST request!"))
})
2024-05-08 09:00:58 +01:00
2024-05-15 07:56:14 +01:00
http.Handle("/static/", http.FileServerFS(staticFiles))
http.ListenAndServe(":8000", nil)
}