-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
84 lines (68 loc) · 1.95 KB
/
server.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
package shortener
import (
"html/template"
"log"
"net/http"
"time"
"github.com/w32blaster/shortana/db"
"github.com/w32blaster/shortana/stats"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/httprate"
)
var (
tmplIndex = template.Must(template.ParseFiles("templates/index.html"))
)
type (
AllLinksData struct {
Links []db.ShortURL
Error error
WrongUrl string
Hostname string
}
)
func makeRequestProcessor(db *db.Database, stats *stats.Statistics, hostname string) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
shortUrl := chi.URLParam(req, "shortUrl")
if len(shortUrl) == 0 {
w.Write([]byte("Short URL is not provided"))
return
}
url, err := db.GetUrl(shortUrl)
if err != nil {
printIndex(db, w, hostname, shortUrl)
return
}
go stats.ProcessRequest(req, shortUrl)
w.Header().Add("Location", url.TargetUrl)
w.WriteHeader(http.StatusMovedPermanently)
}
}
// printIndex prints page with available public (!) links in case if short URL was wrong
func printIndex(db *db.Database, w http.ResponseWriter, hostname, wrongUrl string) {
w.Header().Add("Content-Type", "text/html; charset=utf-8")
links, err := db.GetAll()
data := AllLinksData{
Links: links,
Error: err,
Hostname: hostname,
WrongUrl: wrongUrl,
}
if err = tmplIndex.Execute(w, data); err != nil {
log.Println("Error while rendering page: " + err.Error())
}
}
// StartServer starts the server that handles all the requests
func StartServer(db *db.Database, stats *stats.Statistics, host string) {
r := chi.NewRouter()
r.Use(middleware.RealIP)
r.Use(middleware.Logger)
r.Use(middleware.NoCache)
r.Use(middleware.Recoverer)
r.Use(httprate.LimitByIP(100, 1*time.Minute))
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
printIndex(db, w, host, "")
})
r.Get("/{shortUrl}", makeRequestProcessor(db, stats, host))
http.ListenAndServe(":3000", r)
}