mirror of
https://github.com/idanoo/GoCatFacts
synced 2025-07-01 23:02:15 +00:00
Compare commits
No commits in common. "748270c22aafbea3e61f7c79ac88c14bc59e2c14" and "c51b6236f0bd43be7fa3b53a20d5da68123d7a79" have entirely different histories.
748270c22a
...
c51b6236f0
4 changed files with 4 additions and 1325 deletions
|
@ -1,10 +1,7 @@
|
|||
# GoCatFacts
|
||||
|
||||
Beautiful program. HTTP server. You hit? You get fact. Boom. Default port 80.
|
||||
Quick and dirty proof of concept.
|
||||
|
||||
```shell
|
||||
go run main.go
|
||||
```
|
||||
|
||||
Initial fact list copied from https://github.com/0x27/catfacts/blob/master/catfacts.txt
|
||||
```
|
|
@ -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
52
main.go
|
@ -1,17 +1,9 @@
|
|||
package main
|
||||
package gocatfacts
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
import "os"
|
||||
|
||||
var (
|
||||
port = "8080"
|
||||
facts = []string{}
|
||||
port = "8080"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -21,42 +13,4 @@ func main() {
|
|||
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))]
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue