-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
107 lines (90 loc) · 2.9 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
urlexpander "github.com/vanekjar/urlexpander/lib"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"strconv"
"syscall"
"time"
)
// parse flags from cmd-line
func parseArguments() (port int, userAgent string, cacheCapacity int, cacheExpiration time.Duration, apiOnly bool) {
flag.BoolVar(&apiOnly, "api-only", false, "Expose only the API end points")
flag.IntVar(&port, "port", 8080, "Bind webserver to given port.")
flag.StringVar(&userAgent, "user-agent",
"Mozilla/5.0 (compatible; UrlExpander/1.0)",
" User agent string used when translating shortened url.")
flag.IntVar(&cacheCapacity, "cache-capacity",
100000,
"Expanded urls are cached for repeated queries. Using this option cache capacity can be set.")
var cacheExpirationMinutes int
flag.IntVar(&cacheExpirationMinutes, "cache-expiration",
60,
"Set cache expiration time in minutes.")
help := flag.Bool("help", false, "Print usage.")
flag.Parse()
cacheExpiration = time.Duration(cacheExpirationMinutes) * time.Minute
// print usage
if *help {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
syscall.Exit(0)
}
return
}
type App struct {
Expander urlexpander.UrlExpander
}
func main() {
port, userAgent, cacheCapacity, cacheExpiration, apiOnly := parseArguments()
conf := urlexpander.Config{
CacheCapacity: cacheCapacity,
CacheExpiration: cacheExpiration,
UserAgent: userAgent,
ShortUrlMaxLength: 32,
}
log.Printf("INFO: Used configuration: %#v", conf)
app := App{Expander: urlexpander.NewFromConfig(conf)}
log.Printf("INFO: Listening on port %d", port)
if !apiOnly {
http.Handle("/", http.FileServer(http.Dir("./static")))
}
http.HandleFunc("/api/expand", app.expand)
err := http.ListenAndServe(":"+strconv.Itoa(port), nil)
if err != nil {
panic(err)
}
}
type responseMessage struct {
Original string `json:"original,omitempty"`
Expanded string `json:"expanded,omitempty"`
Error string `json:"error,omitempty"`
}
// Handle /expand API endpoint calls
func (app *App) expand(w http.ResponseWriter, r *http.Request) {
shortened := r.URL.Query().Get("url")
if shortened == "" {
resp, _ := json.Marshal(responseMessage{Error: "No url provided. Use 'url' param."})
http.Error(w, string(resp), http.StatusBadRequest)
return
}
expanded, err := app.Expander.ExpandUrl(shortened)
if err != nil {
log.Printf("ERROR: (%s) %s [url: %s]", r.RemoteAddr, err.Error(), shortened)
switch err {
case urlexpander.ErrDisallowedByRobotsTxt, urlexpander.ErrInvalidUrl, urlexpander.ErrLongUrl:
resp, _ := json.Marshal(responseMessage{Error: err.Error()})
http.Error(w, string(resp), http.StatusBadRequest)
default:
resp, _ := json.Marshal(responseMessage{Error: "Uknown error"})
http.Error(w, string(resp), http.StatusInternalServerError)
}
return
}
resp := responseMessage{Original: shortened, Expanded: expanded}
json.NewEncoder(w).Encode(resp)
}