-
Notifications
You must be signed in to change notification settings - Fork 64
/
main.go
87 lines (80 loc) · 2.31 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
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
package main
import (
"database/sql"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"time"
"github.com/gorilla/mux"
_ "github.com/lib/pq"
"github.com/narenaryan/urlshortener/models"
base62 "github.com/narenaryan/urlshortener/utils"
)
// DB stores the database session imformation. Needs to be initialized once
type DBClient struct {
db *sql.DB
}
// Model the record struct
type Record struct {
ID int `json:"id"`
URL string `json:"url"`
}
// GetOriginalURL fetches the original URL for the given encoded(short) string
func (driver *DBClient) GetOriginalURL(w http.ResponseWriter, r *http.Request) {
var url string
vars := mux.Vars(r)
// Get ID from base62 string
id := base62.ToBase10(vars["encoded_string"])
err := driver.db.QueryRow("SELECT url FROM web_url WHERE id = $1", id).Scan(&url)
// Handle response details
if err != nil {
w.Write([]byte(err.Error()))
} else {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
responseMap := map[string]interface{}{"url": url}
response, _ := json.Marshal(responseMap)
w.Write(response)
}
}
// GenerateShortURL adds URL to DB and gives back shortened string
func (driver *DBClient) GenerateShortURL(w http.ResponseWriter, r *http.Request) {
var id int
var record Record
postBody, _ := ioutil.ReadAll(r.Body)
json.Unmarshal(postBody, &record)
err := driver.db.QueryRow("INSERT INTO web_url(url) VALUES($1) RETURNING id", record.URL).Scan(&id)
responseMap := map[string]interface{}{"encoded_string": base62.ToBase62(id)}
if err != nil {
w.Write([]byte(err.Error()))
} else {
w.Header().Set("Content-Type", "application/json")
response, _ := json.Marshal(responseMap)
w.Write(response)
}
}
func main() {
db, err := models.InitDB()
if err != nil {
panic(err)
}
dbclient := &DBClient{db: db}
if err != nil {
panic(err)
}
defer db.Close()
// Create a new router
r := mux.NewRouter()
// Attach an elegant path with handler
r.HandleFunc("/v1/short/{encoded_string:[a-zA-Z0-9]*}", dbclient.GetOriginalURL).Methods("GET")
r.HandleFunc("/v1/short", dbclient.GenerateShortURL).Methods("POST")
srv := &http.Server{
Handler: r,
Addr: "127.0.0.1:8000",
// Good practice: enforce timeouts for servers you create!
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Fatal(srv.ListenAndServe())
}