-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
46 lines (37 loc) · 1.13 KB
/
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
package main
import (
"embed"
"log"
"net/http"
"github.com/joeychilson/hackernews/handlers"
"github.com/joeychilson/hackernews/pages"
"github.com/joeychilson/hackernews/pkg/hackernews"
)
//go:embed static
var static embed.FS
func main() {
client := hackernews.New()
mux := http.NewServeMux()
// Redirects to News to match the original site
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
pages.NotFound().Render(r.Context(), w)
return
}
http.Redirect(w, r, "/news", http.StatusFound)
})
// Static files
mux.Handle("/static/", http.FileServer(http.FS(static)))
// Pages
mux.HandleFunc("/ask", handlers.HandleAsk(client))
mux.HandleFunc("/item", handlers.HandleItem(client))
mux.HandleFunc("/jobs", handlers.HandleJobs(client))
mux.HandleFunc("/newest", handlers.HandleNewest(client))
mux.HandleFunc("/news", handlers.HandleNews(client))
mux.HandleFunc("/show", handlers.HandleShow(client))
mux.HandleFunc("/user", handlers.HandleUser(client))
log.Println("Listening on http://localhost:8080")
if err := http.ListenAndServe(":8080", mux); err != nil {
log.Fatal(err)
}
}