main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main import ( "html/template" "log" "net/http" "path/filepath" "strings" "j3s.sh/poop.is/word" ) var globalURLMap = make(map[string]string) func main() { // pass everything through a muxer because // we need to differentiate index from everything // else http.HandleFunc("/", muxHandler) words := word.GetWordList() adjective := words.RandomAdjective() for { if inUse(adjective) { adjective = words.RandomAdjective() // TODO: handle running out of words -.- } else { break } } log.Println(adjective) log.Println("listening on :8008 tbh") err := http.ListenAndServe(":8008", nil) if err != nil { log.Fatal(err) } // url := makeURL(adjective) } func muxHandler(w http.ResponseWriter, r *http.Request) { if len(r.URL.Path) == 1 { indexHandler(w, r) } else { redirectHandler(w, r) } } func redirectHandler(w http.ResponseWriter, r *http.Request) { path := strings.TrimPrefix(r.URL.Path, "/") if globalURLMap[path] != "" { log.Printf("%s", "found") http.Redirect(w, r, globalURLMap[path], 302) } else { log.Printf("%s", "not found") http.Redirect(w, r, "/", 302) } } func indexHandler(w http.ResponseWriter, r *http.Request) { tfile := filepath.Join("templates", "index.html") tmpl, err := template.ParseFiles(tfile) if err != nil { log.Println(err.Error()) http.Error(w, http.StatusText(500), 500) return } if r.Method != http.MethodPost { tmpl.Execute(w, nil) return } url := r.FormValue("url") // validate the string? shortenedURL := shortenURL(url) tmpl.Execute(w, shortenedURL) } func inUse(word string) bool { if globalURLMap[word] != "" { return true } return false } func shortenURL(url string) string { words := word.GetWordList() adjective := words.RandomAdjective() // TODO: make this loop not go infinite when the wordlist // has been fully consumed for { if len(globalURLMap) > len(words.Adjectives) { adjectiveOne := words.RandomAdjective() adjectiveTwo := words.RandomAdjective() adjective = adjectiveOne + adjectiveTwo } if inUse(adjective) { adjective = words.RandomAdjective() } else { break } } var thing string thing = "https://poop.is/" + adjective associate(adjective, url) log.Printf("%+v", url) return thing } func associate(adjective string, url string) { log.Printf("%s: %s", adjective, url) globalURLMap[adjective] = url }