diff --git a/src/blog/posts/2024/5/learning-go-day-11.md b/src/blog/posts/2024/5/learning-go-day-11.md new file mode 100644 index 0000000..b0f4e94 --- /dev/null +++ b/src/blog/posts/2024/5/learning-go-day-11.md @@ -0,0 +1,100 @@ +--- +title: "Learning Go: Day Eleven" +date: 2024-05-13T08:00:00.0Z +tags: + - learning + - go +excerpt: "Now it's time to actually try and send a \"ping\" to a website" +--- + +So, next up on my task list is to start populating my database with data. So to do that I want to be able to send a request to a given website, and then store the success or failure status based on the response. + +## Sending a request + +It turns out that this is actually quite simple to do. The `net/http` module has functions to do this, so all I need to do is: + +```go +// ping/ping.go + +import ( + "net/http" +) + +func SendPing(db *sql.DB, site sites.Site) { + response, error := http.Get(site.url) + + // Do something with the response or error +} +``` + +Easy enough! + +## Store the result + +Now all I need to do is save the output to the database. I do that by using the inline if-statement syntax, and assigning `ping.Status` depending on whether or not there is an error present. I don't actually _care_ about the response, I just care whether or not the call succeeded. + +```go +// ping/ping.go + +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) +} +``` + +And as a little refactor, I can default the `Status` to `Success`, and get rid of the `else-` branch. + +```go +// ping/ping.go + +func SendPing(db *sql.DB, site sites.Site) { + p := Ping{ + Site: site, + Status: Success + } + + if _, err := http.Get(site.Url); err != nil { + p.Status = Failure + } + + p.Save(db) +} +``` + +Now, if I write a quick test in my `main.go`[^1], I can see that it succeeds: + +```go +// main.go + +func main() { + site := sites.Site{Name: "Lewisdale.dev", Url: "https://lewisdale.dev"} + site.Save(db) + + ping.SendPing(db, site) +} +``` + +```bash +sqlite> SELECT ping.id, ping.site, ping.timestamp, statuses.name FROM ping LEFT JOIN statuses ON (ping.status = statuses.id); +1|https://lewisdale.dev|2024-05-10 05:57:12|Success +``` + +And if I then force a failure, it should also work: + +```bash +5|https://notreal.tld|2024-05-10 08:00:46|Failure +``` + +## Adding some output + +Okay, now I have a database with some data in it, I'd like to expose that data +[^1]: Yes, despite what I said in another post I've not written any _actual_ tests. I'm human, alright? \ No newline at end of file diff --git a/src/blog/posts/2024/5/macclesfield-bikeathon.md b/src/blog/posts/2024/5/macclesfield-bikeathon.md new file mode 100644 index 0000000..d161236 --- /dev/null +++ b/src/blog/posts/2024/5/macclesfield-bikeathon.md @@ -0,0 +1,17 @@ +--- +title: "Macclesfield Bikeathon" +date: 2024-05-12T08:00:00.0Z +tags: + - cycling +excerpt: "Just some thoughts about finishing the Macclesfield Bikeathon yesterday" +--- + +I finished my first [charity bike ride](/post/fundraising-for-cyclists-fighting-cancer-in-2024/) yesterday, which was the Macclesfield Bikeathon. Originally I'd planned to travel down by car or train, and then just ride the 100km route, but my car broke down so instead I cycled from Manchester - Macclesfield, which added an extra 35km to the route. + +The route itself was really nice, I spent the first 50-or-so kilometres out in front on my own, before being passed by two much faster cyclists. I stuck with them for a little while, but got dropped on the last 20km after my legs gave out a bit. Then that final 20km was pretty much entirely uphill, so I went _very_ slowly for it. + +I tried to stay on top of refuelling & drinking, but I still was pretty worn out by the time I got to the end. Still, I managed a good average pace of 27.5km/hr, not bad for my first 100km+ ride since September. + +As for the event itself, the route could have been more clearly marked, and there was no "start' line. Instead you just grabbed your number from the desk and set off, which was a bit confusing. + +Anyway, that's the first event out of the way. My next one will be the Manchester - Blackpool ride on 14/07/24, unless I find an extra event to do in June. \ No newline at end of file