chore: moved vars and cleaned up code
9046bfe5
3 file(s) · +12 −19
| 8 | 8 | "sync" |
|
| 9 | 9 | ) |
|
| 10 | 10 | ||
| 11 | - | const sourceText = "moby.txt" |
|
| 12 | - | ||
| 13 | - | var sharedBabbler = sync.OnceValue(NewBabbler) |
|
| 14 | - | ||
| 15 | 11 | type MarkovChain struct { |
|
| 16 | 12 | chain map[string][]string |
|
| 17 | 13 | words []string |
|
| 18 | 14 | keys []string |
|
| 19 | 15 | } |
|
| 16 | + | ||
| 17 | + | var sharedBabbler = sync.OnceValue(NewBabbler) |
|
| 20 | 18 | ||
| 21 | 19 | func NewBabbler() *MarkovChain { |
|
| 22 | 20 | text, err := os.ReadFile(sourceText) |
| 6 | 6 | "net/http" |
|
| 7 | 7 | ) |
|
| 8 | 8 | ||
| 9 | - | // Plan |
|
| 10 | - | // - [x] Serve robots.txt with noindex |
|
| 11 | - | // - [x] Serve root path html with babbler and links to new paths |
|
| 12 | - | // - [x] Serve dynamic path that takes in path param to serve babbler and more links |
|
| 13 | - | // - [x] Slow down responses |
|
| 9 | + | var ( |
|
| 10 | + | dripDelayMinMs = envInt("GARGANTUA_DELAY_MIN_MS", 150) |
|
| 11 | + | dripDelayMaxMs = envInt("GARGANTUA_DELAY_MAX_MS", 250) |
|
| 12 | + | dripChunkMax = envInt("GARGANTUA_CHUNK_MAX_BYTES", 45) |
|
| 13 | + | portEnv = envString("GARGANTUA_PORT", "8090") |
|
| 14 | + | hostEnv = envString("GARGANTUA_HOST", "127.0.0.1") |
|
| 15 | + | sourceText = envString("GARGANTUA_CORPUS", "moby.txt") |
|
| 16 | + | ) |
|
| 14 | 17 | ||
| 15 | 18 | func main() { |
|
| 16 | - | http.HandleFunc("/robots.txt", RobotsHandler) |
|
| 17 | 19 | http.HandleFunc("/", IndexHandler) |
|
| 18 | 20 | http.HandleFunc("/{slug}", IndexHandler) |
|
| 21 | + | http.HandleFunc("/robots.txt", RobotsHandler) |
|
| 19 | 22 | ||
| 20 | - | port := fmt.Sprintf(":%s", portEnv) |
|
| 23 | + | port := fmt.Sprintf("%s:%s", hostEnv, portEnv) |
|
| 21 | 24 | log.Println("Listening on", port) |
|
| 22 | 25 | log.Fatal(http.ListenAndServe(port, RequestMiddleware(http.DefaultServeMux))) |
|
| 23 | 26 | } |
| 11 | 11 | "time" |
|
| 12 | 12 | ) |
|
| 13 | 13 | ||
| 14 | - | var portEnv = envString("PORT", "8090") |
|
| 15 | - | ||
| 16 | - | var ( |
|
| 17 | - | dripDelayMinMs = envInt("DRIP_DELAY_MIN_MS", 50) |
|
| 18 | - | dripDelayMaxMs = envInt("DRIP_DELAY_MAX_MS", 150) |
|
| 19 | - | dripChunkMax = envInt("DRIP_CHUNK_MAX_BYTES", 60) |
|
| 20 | - | ) |
|
| 21 | - | ||
| 22 | 14 | //go:embed templates/*.html |
|
| 23 | 15 | var templatesFS embed.FS |
|
| 24 | 16 | var tmpl = template.Must(template.ParseFS(templatesFS, "templates/*.html")) |