Compare commits

...

3 commits

Author SHA1 Message Date
748270c22a
Moar 2025-06-14 19:52:34 +12:00
536ac1ec68
Add sysd service + newline 2025-06-14 19:40:53 +12:00
d39591b3c0
Working proof of concept 2025-06-14 19:36:43 +12:00
4 changed files with 1325 additions and 4 deletions

View file

@ -1,7 +1,10 @@
# GoCatFacts # GoCatFacts
Beautiful program. HTTP server. You hit? You get fact. Boom. Default port 80. Beautiful program. HTTP server. You hit? You get fact. Boom. Default port 80.
Quick and dirty proof of concept.
```shell ```shell
go run main.go go run main.go
``` ```
Initial fact list copied from https://github.com/0x27/catfacts/blob/master/catfacts.txt

1255
facts.txt Normal file

File diff suppressed because it is too large Load diff

17
gocatfacts.service Normal file
View file

@ -0,0 +1,17 @@
[Unit]
Description=GoCatFacts
After=network.target
[Service]
Type=simple
WorkingDirectory=/root/GoCatFacts
ExecStart=/usr/local/go/bin/go run main.go
Environment="GOCACHE=/tmp/gocatfacts"
Restart=on-failure
RestartSec=10
StandardOutput=syslog
StandardError=syslog
[Install]
WantedBy=multi-user.target

50
main.go
View file

@ -1,9 +1,17 @@
package gocatfacts package main
import "os" import (
"bufio"
"io"
"log"
"math/rand"
"net/http"
"os"
)
var ( var (
port = "8080" port = "8080"
facts = []string{}
) )
func main() { func main() {
@ -13,4 +21,42 @@ func main() {
port = portOverride port = portOverride
} }
// Load it up
err := loadFacts()
if err != nil {
log.Fatalf("Error loading facts: %v", err)
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, getRandomFact()+"\n")
})
err = http.ListenAndServe(":"+port, nil)
if err != nil {
log.Fatalf("Error loading facts: %v", err)
}
}
// loadFacts loads cat facts from a file named "facts.txt"
func loadFacts() error {
file, err := os.Open("facts.txt")
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
facts = append(facts, scanner.Text())
}
return scanner.Err()
}
// getRandomFact returns a random cat fact from the loaded facts
func getRandomFact() string {
if len(facts) == 0 {
return "No facts available."
}
return facts[rand.Intn(len(facts))]
} }