Compare commits

..

No commits in common. "748270c22aafbea3e61f7c79ac88c14bc59e2c14" and "c51b6236f0bd43be7fa3b53a20d5da68123d7a79" have entirely different histories.

4 changed files with 4 additions and 1325 deletions

View file

@ -1,10 +1,7 @@
# 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

File diff suppressed because it is too large Load diff

View file

@ -1,17 +0,0 @@
[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

52
main.go
View file

@ -1,17 +1,9 @@
package main package gocatfacts
import ( import "os"
"bufio"
"io"
"log"
"math/rand"
"net/http"
"os"
)
var ( var (
port = "8080" port = "8080"
facts = []string{}
) )
func main() { func main() {
@ -21,42 +13,4 @@ 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))]
} }