Compare commits

...
15 Commits
Author SHA1 Message Date
Lewis Dale 701544df9c Inherited templates, staticfiles
Build and copy to prod / build-and-copy (push) Successful in 43s
2024-05-15 07:56:14 +01:00
Lewis Dale 27f6d749d3 Start outputting HTML
Build and copy to prod / build-and-copy (push) Successful in 36s
2024-05-14 13:01:40 +01:00
Lewis Dale a7a55b6a90 More output on the home page
Build and copy to prod / build-and-copy (push) Successful in 32s
2024-05-13 07:37:11 +01:00
Lewis Dale 72122553dd Output data as JSON
Build and copy to prod / build-and-copy (push) Successful in 37s
2024-05-12 16:54:00 +01:00
Lewis Dale 4df6d17739 Test a site and store the output
Build and copy to prod / build-and-copy (push) Successful in 34s
2024-05-10 09:02:02 +01:00
Lewis Dale 2561f492f9 Add save function to pings
Build and copy to prod / build-and-copy (push) Successful in 32s
2024-05-09 08:04:25 +01:00
Lewis Dale 6d0f27eba7 Don't commit the database, silly
Build and copy to prod / build-and-copy (push) Successful in 1m27s
2024-05-09 07:12:44 +01:00
Lewis Dale fe8dcfe3e3 Add ping and status table 2024-05-09 07:12:24 +01:00
Lewis Dale 0ea09941a5 Use url as the unique id for a site, rather than an id
Build and copy to prod / build-and-copy (push) Successful in 36s
2024-05-08 09:26:26 +01:00
Lewis Dale 49d33b2366 Day 9 content, connecting to database
Build and copy to prod / build-and-copy (push) Successful in 1m31s
2024-05-08 09:00:58 +01:00
Lewis Dale d3ee10669e Stop, copy, start
Build and copy to prod / build-and-copy (push) Successful in 36s
2024-05-05 20:43:25 +01:00
Lewis Dale 3dc5e5cf7f Restart the service
Build and copy to prod / build-and-copy (push) Failing after 36s
2024-05-05 20:35:02 +01:00
Lewis Dale a0b22059a0 Upload to blog-builder directory
Build and copy to prod / build-and-copy (push) Successful in 35s
2024-05-05 19:54:25 +01:00
Lewis Dale 6139172f59 Copy to prod
Build and copy to prod / build-and-copy (push) Failing after 34s
2024-05-05 19:50:51 +01:00
Lewis Dale 4d88c50a22 Install SSH keys 2024-05-05 19:50:45 +01:00
12 changed files with 368 additions and 5 deletions

No files matched your search

+12 -1
View File
@@ -12,4 +12,15 @@ jobs:
with: with:
go-version: 1.22 go-version: 1.22
- name: Build binary - name: Build binary
run: go build -o dist/oopsie run: go build -o dist/oopsie
- name: Install SSH Key
uses: shimataro/ssh-key-action@v2
with:
key: ${{ secrets.SSH_KEY }}
known_hosts: ${{ secrets.SSH_KNOWN_HOSTS }}
- name: Stop the service
run: ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} -o RemoteCommand="systemctl --user stop oopsie_service.service"
- name: Copy to prod
run: scp -rp dist/* ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}:oopsie/
- name: Restart the service
run: ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} -o RemoteCommand="systemctl --user start oopsie_service.service"
+2 -1
View File
@@ -21,4 +21,5 @@
# Go workspace file # Go workspace file
go.work go.work
dist dist
*.sqlite3
+8
View File
@@ -0,0 +1,8 @@
package data
import "database/sql"
func Connect(file string) *sql.DB {
db, _ := sql.Open("sqlite3", file)
return db
}
+2
View File
@@ -1,3 +1,5 @@
module lewisdale.dev/oopsie module lewisdale.dev/oopsie
go 1.22.2 go 1.22.2
require github.com/mattn/go-sqlite3 v1.14.22 // indirect
+2
View File
@@ -0,0 +1,2 @@
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
+49 -3
View File
@@ -1,15 +1,61 @@
package main package main
import ( import (
"embed"
"encoding/json"
"html/template"
"net/http" "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() { func main() {
http.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) { db := data.Connect("test.sqlite3")
w.Write([]byte("This was a GET request!"))
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) {
http.HandleFunc("POST /{$}", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("This was a POST request!")) w.Write([]byte("This was a POST request!"))
}) })
http.Handle("/static/", http.FileServerFS(staticFiles))
http.ListenAndServe(":8000", nil) http.ListenAndServe(":8000", nil)
} }
+143
View File
@@ -0,0 +1,143 @@
package ping
import (
"database/sql"
"net/http"
"lewisdale.dev/oopsie/sites"
)
type Status int16
const (
Success Status = iota
Failure
)
type Ping struct {
Site sites.Site
Timestamp string
Status Status
}
type PingResponse struct {
Site sites.Site
Timestamp string
Status string
}
const createQuery = `CREATE TABLE IF NOT EXISTS statuses (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS ping (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
site TEXT NOT NULL,
timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
status INTEGER NOT NULL,
FOREIGN KEY (site) REFERENCES sites(url),
FOREIGN KEY (status) REFERENCES statuses(id)
);`
const seedStatusQuery = `INSERT INTO statuses (id, name) VALUES (?, ?)
ON CONFLICT (id) DO NOTHING;`
func CreateTable(db *sql.DB) {
if _, err := db.Exec(createQuery); err != nil {
panic(err)
}
seedStatuses(db)
}
func seedStatuses(db *sql.DB) {
if _, err := db.Exec(seedStatusQuery, Success, "Success"); err != nil {
panic(err)
}
if _, err := db.Exec(seedStatusQuery, Failure, "Failure"); err != nil {
panic(err)
}
}
const saveQuery = `INSERT INTO ping (site, status) VALUES (?, ?);`
func (p *Ping) Save(db *sql.DB) {
db.Exec(saveQuery, p.Site.Url, p.Status)
}
func SendPing(db *sql.DB, site sites.Site) {
p := Ping{
Site: site,
}
if _, err := http.Get(site.Url); err != nil {
p.Status = Failure
} else {
p.Status = Success
}
p.Save(db)
}
func List(db *sql.DB) []PingResponse {
rows, err := db.Query(`SELECT sites.url as url, sites.name, ping.timestamp as timestamp, statuses.name as status FROM ping
JOIN sites ON ping.site = sites.url
JOIN statuses ON ping.status = statuses.id
ORDER BY timestamp DESC`)
if err != nil {
panic(err)
}
defer rows.Close()
pings := make([]PingResponse, 0)
for rows.Next() {
p := PingResponse{}
rows.Scan(&p.Site.Url, &p.Site.Name, &p.Timestamp, &p.Status)
pings = append(pings, p)
}
return pings
}
type SiteResponse struct {
created_at string
Name string
Url string
Pings []Ping
}
func ListGroupedBySite(db *sql.DB) []SiteResponse {
responses := make([]SiteResponse, 0)
for _, site := range sites.List(db) {
s := SiteResponse{Url: site.Url, Name: site.Name, created_at: site.Created_at}
s.Pings = listForSite(db, site)
responses = append(responses, s)
}
return responses
}
func listForSite(db *sql.DB, site sites.Site) []Ping {
rows, err := db.Query(`SELECT ping.timestamp, ping.status FROM ping WHERE site = ? ORDER BY timestamp DESC`, site.Url)
if err != nil {
panic(err)
}
defer rows.Close()
pings := make([]Ping, 0)
for rows.Next() {
p := Ping{}
rows.Scan(&p.Timestamp, &p.Status)
p.Site = site
pings = append(pings, p)
}
return pings
}
+53
View File
@@ -0,0 +1,53 @@
package sites
import (
"database/sql"
"fmt"
)
const createSitesTable = `CREATE TABLE IF NOT EXISTS sites (
url TEXT NOT NULL PRIMARY KEY,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
name TEXT NOT NULL
);`
func CreateTable(db *sql.DB) {
if _, err := db.Exec(createSitesTable); err != nil {
fmt.Println(fmt.Errorf(err.Error()))
}
}
type Site struct {
Created_at string
Name string
Url string
}
const upsertQuery = `INSERT INTO sites (url, name) VALUES (?, ?)
ON CONFLICT (url) DO UPDATE
SET
name = excluded.name
`
func (s *Site) Save(db *sql.DB) {
db.Exec(upsertQuery, s.Url, s.Name)
}
func List(db *sql.DB) []Site {
rows, err := db.Query(`SELECT url, name, created_at FROM sites ORDER BY created_at DESC`)
if err != nil {
panic(err)
}
defer rows.Close()
sites := make([]Site, 0)
for rows.Next() {
s := Site{}
rows.Scan(&s.Url, &s.Name, &s.Created_at)
sites = append(sites, s)
}
return sites
}
+80
View File
@@ -0,0 +1,80 @@
* Modern reset: https://piccalil.li/blog/a-modern-css-reset/ */
/* Box sizing rules */
*,
*::before,
*::after {
box-sizing: border-box;
}
/* Remove default margin */
body,
h1,
h2,
h3,
h4,
p,
figure,
blockquote,
dl,
dd {
margin: 0;
}
/* Remove list styles on ul, ol elements with a list role, which suggests default styling will be removed */
[role='list']
{
list-style: none;
padding: 0;
margin: 0;
}
/* Set core root defaults */
html:focus-within {
scroll-behavior: smooth;
}
/* Set core body defaults */
body {
min-height: 100vh;
min-height: 100dvh; /* safari-specific */
text-rendering: optimizeSpeed;
line-height: 1.75;
}
/* A elements that don't have a class get default styles */
a:not([class]) {
text-decoration-skip-ink: auto;
}
/* Make images easier to work with */
img,
picture {
max-width: 100%;
display: block;
height: auto;
}
/* Inherit fonts for inputs and buttons */
input,
button,
textarea,
select {
font: inherit;
}
/* Remove all animations, transitions and smooth scroll for people that prefer not to see them */
@media (prefers-reduced-motion: reduce) {
html:focus-within {
scroll-behavior: auto;
}
*,
*::before,
*::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
scroll-behavior: auto !important;
}
}
+7
View File
@@ -0,0 +1,7 @@
<!DOCTYPE html>
<html lang="en">
{{ template "head.html" . }}
<body>
{{ block "content" . }}{{ end }}
</body>
</html>
+5
View File
@@ -0,0 +1,5 @@
<head>
<title>Oopsie</title>
<link rel="stylesheet" href="/static/reset.css" />
</head>
+5
View File
@@ -0,0 +1,5 @@
{{ template "base.html" . }}
{{ define "content" }}
<h1>Oopsie uptime monitoring</h1>
{{ end }}