All checks were successful
Build and copy to prod / build-and-copy (push) Successful in 43s
62 lines
1.3 KiB
Go
62 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"embed"
|
|
"encoding/json"
|
|
"html/template"
|
|
"net/http"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
"lewisdale.dev/oopsie/data"
|
|
"lewisdale.dev/oopsie/ping"
|
|
"lewisdale.dev/oopsie/sites"
|
|
)
|
|
|
|
//go:embed templates/*
|
|
var templates embed.FS
|
|
|
|
//go:embed static/*
|
|
var staticFiles embed.FS
|
|
|
|
func main() {
|
|
db := data.Connect("test.sqlite3")
|
|
|
|
db.Exec("PRAGMA foreign_keys = ON;")
|
|
|
|
sites.CreateTable(db)
|
|
ping.CreateTable(db)
|
|
|
|
site := sites.Site{Name: "Lewisdale.dev", Url: "https://lewisdale.dev"}
|
|
site.Save(db)
|
|
|
|
ping.SendPing(db, site)
|
|
|
|
failureSite := sites.Site{Name: "Example", Url: "https://notreal.tld"}
|
|
failureSite.Save(db)
|
|
|
|
ping.SendPing(db, failureSite)
|
|
|
|
http.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) {
|
|
pings := ping.ListGroupedBySite(db)
|
|
|
|
if _, err := json.Marshal(pings); err != nil {
|
|
w.Write([]byte(err.Error()))
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
} else {
|
|
w.Header().Set("Content-Type", "text/html")
|
|
|
|
t, _ := template.ParseFS(templates, "templates/index.html", "templates/base.html", "templates/head.html")
|
|
t.Execute(w, nil)
|
|
}
|
|
})
|
|
|
|
http.HandleFunc("POST /{$}", func(w http.ResponseWriter, r *http.Request) {
|
|
w.Write([]byte("This was a POST request!"))
|
|
})
|
|
|
|
http.Handle("/static/", http.FileServerFS(staticFiles))
|
|
|
|
http.ListenAndServe(":8000", nil)
|
|
}
|