You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
1.5 KiB
73 lines
1.5 KiB
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"github.com/aws/aws-lambda-go/events"
|
|
"github.com/aws/aws-lambda-go/lambda"
|
|
"math/rand"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
var nouns = []string{}
|
|
var verbs = []string{}
|
|
var ends = []string{}
|
|
var nends = []string{}
|
|
|
|
func init() {
|
|
rand.Seed(time.Now().UnixNano())
|
|
nouns, _ = readFile("nouns.txt")
|
|
verbs, _ = readFile("verbs.txt")
|
|
ends, _ = readFile("ends.txt")
|
|
nends = append(nouns, ends...)
|
|
}
|
|
|
|
func readFile(path string) ([]string, error) {
|
|
file, err := os.Open(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer file.Close()
|
|
|
|
var lines []string
|
|
scanner := bufio.NewScanner(file)
|
|
for scanner.Scan() {
|
|
lines = append(lines, scanner.Text())
|
|
}
|
|
return lines, scanner.Err()
|
|
}
|
|
|
|
func getWord(wordlist []string) string {
|
|
return wordlist[rand.Intn(len(wordlist))]
|
|
}
|
|
|
|
func genSwear() string {
|
|
swear := "default"
|
|
r := rand.Intn(4)
|
|
switch r {
|
|
case 1:
|
|
swear = getWord(nouns) + "-" + getWord(verbs) + " " + getWord(nends)
|
|
case 2:
|
|
swear = getWord(nouns) + "-" + getWord(ends)
|
|
case 3:
|
|
swear = getWord(nouns) + "-" + getWord(verbs) + " " + getWord(nouns) + " " + getWord(ends)
|
|
default:
|
|
swear = getWord(nouns) + "-" + getWord(nends)
|
|
}
|
|
return strings.Title(swear)
|
|
}
|
|
|
|
func HandleRequest() (events.APIGatewayProxyResponse, error) {
|
|
return events.APIGatewayProxyResponse{
|
|
StatusCode: http.StatusOK,
|
|
Body: fmt.Sprintf(genSwear()),
|
|
}, nil
|
|
}
|
|
|
|
func main() {
|
|
lambda.Start(HandleRequest)
|
|
}
|