/
app.go
184 lines (157 loc) · 5.12 KB
/
app.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
// static directory is accessed at /
// /files redirects to /files/id
// posts to /files/id saves file
// yep
import (
"fmt"
"io/ioutil"
"net/http"
"path"
"math/rand"
"strings"
)
const (
filesDir = "files/"
filesExt = ".asm"
)
type AsmFile struct {
Id string
Text []byte
}
func (af *AsmFile) Save() error {
return ioutil.WriteFile(af.GetPath(), af.Text, 0600)
}
func (af *AsmFile) GetPath() string {
return path.Join(filesDir, af.Id + filesExt)
}
func LoadAsmFile(id string) (*AsmFile, error) {
af := &AsmFile{id, nil}
filename := af.GetPath()
var err error = nil
af.Text, err = ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return af, nil
}
type RequestHandler struct {
newIds chan string
}
var (
NATURES = []string { "hardy", "lonely", "brave", "adamant", "naughty",
"bold", "docile", "relaxed", "impish", "lax", "timid", "hasty",
"serious", "jolly", "naive", "modest", "mild", "mild", "quiet",
"bashful", "rash", "calm", "gentle", "sassy", "careful", "quirky",
}
IVS = []string {
"perfect", "one-iv", "two-iv", "three-iv", "four-iv", "five-iv",
"zero-iv",
}
POKEMON = []string {
"bulbasaur", "ivysaur", "venasaur", "charmander", "charmeleon",
"charizard", "squirtle", "wartortle", "blastoise", "caterpie",
"metapod", "butterfree", "weedle", "kakuna", "beedrill", "pidgey",
"pidgeotto", "pidgeot", "rattata", "raticate", "spearow", "fearow",
"ekans", "arbok", "pikachu", "raichu", "sandshrew", "sandslash",
"nidoran", "nidorina", "nidoqueen", "nidorino", "nidoking",
"clefairy", "clefable", "vulpix", "ninetales", "jigglypuff",
"wigglytuff", "zubat", "golbat", "oddish", "gloom", "vileplume",
"paras", "parasect", "venonat", "venomoth", "diglett", "dugtrio",
"meowth", "persian", "psyduck", "golduck", "mankey", "primeape",
"growlithe", "arcanine", "poliwag", "poliwhirl", "poliwrath", "abra",
"kadabra", "alakazam", "machop", "machoke", "machamp", "bellsprout",
"weepinbell", "victreebell", "tentacool", "tentacruel", "geodude",
"graveler", "golem", "ponyta", "rapidash", "slowpoke", "slowbro",
"magnemite", "magneton", "farfetchd", "doduo", "dodrio", "seel",
"dewgong", "grimer", "muk", "shellder", "cloyster", "gastly",
"haunter", "gengar", "onix", "drowzee", "hypno", "krabby", "kingler",
"voltorb", "electrode", "exeggcute", "exeggutor", "cubone", "marowak",
"hitmonlee", "hitmonchan", "lickitun", "koffing", "weezing",
"rhyhorn", "rhydon", "chansey", "tangela", "kangaskhan", "horsea",
"seadra", "goldeen", "seaking", "staryu", "starmie", "mrmime",
"scyther", "jynx", "electabuzz", "magmar", "pinsir", "tauros",
"magikarp", "gyarados", "lapras", "ditto", "eevee", "vaporeon",
"jolteon", "flareon", "porygon", "omanyte", "omastar", "kabuto",
"kabutops", "aerodactyl", "snorlax", "articuno", "zapdos", "moltres",
"dratini", "dragonair", "dragonite", "mewtwo", "mew",
}
)
func (handler *RequestHandler) GenerateIds() {
for {
nature := rand.Intn(len(NATURES))
iv := rand.Intn(len(IVS))
pokemon := rand.Intn(len(POKEMON))
fmt.Println("thought of a good one: "+NATURES[nature]+"-"+IVS[iv]+"-"+POKEMON[pokemon])
handler.newIds <- NATURES[nature]+"-"+IVS[iv]+"-"+POKEMON[pokemon]
}
}
func IdFromPath(path string) string {
pathComps := strings.Split(path, "/")
return pathComps[len(pathComps)-1]
}
func (handler *RequestHandler) HandlePost(w http.ResponseWriter, r *http.Request) {
text := []byte(r.FormValue("text"))
id := IdFromPath(r.URL.Path)
af, err := LoadAsmFile(id)
if err != nil {
af = &AsmFile{id, text}
} else {
af.Text = text
}
err = af.Save()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func (handler *RequestHandler) HandleGet(w http.ResponseWriter, r *http.Request) {
if(len(r.URL.Path) <= 1) {
id := <- handler.newIds
fmt.Println("Redirecting");
af := &AsmFile{id, make([]byte, 0)}
err := af.Save()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/files/"+id, http.StatusFound)
return
}
id := IdFromPath(r.URL.Path)
fmt.Println("Id: "+id)
af, err := LoadAsmFile(id)
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
if(strings.HasPrefix(r.URL.Path, "get")) {
// raw data wanted "get"
fmt.Println("serving a get!")
http.ServeFile(w, r, af.GetPath())
return;
}
// pretty
fmt.Println("serving file.html")
http.ServeFile(w, r, "static/file.html")
}
func (handler *RequestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
handler.HandleGet(w, r)
} else if r.Method == "POST" {
handler.HandlePost(w, r)
}
}
func main() {
fmt.Println("spooling up ftl drives")
handler := &RequestHandler{make(chan string)}
go func() {
handler.GenerateIds()
}()
http.Handle("/files/", http.StripPrefix("/files/", handler))
http.Handle("/", http.FileServer(http.Dir("static")))
err := http.ListenAndServe(":9001", nil)
if err != nil {
fmt.Println("I has a sad")
}
}