-
Notifications
You must be signed in to change notification settings - Fork 4
/
serve.go
144 lines (124 loc) · 3.85 KB
/
serve.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// +build !darwin
// +build !windows
// Copyright © 2017 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"path"
"strings"
"github.com/asaskevich/govalidator"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/rs/cors"
"github.com/spf13/cobra"
)
var (
dbPath string
)
// serveCmd represents the serve command
var serveCmd = &cobra.Command{
Use: "serve",
Short: "Run the CDA URL Shortening service",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
serve()
},
}
func init() {
RootCmd.AddCommand(serveCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// serveCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// serveCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
func serve() {
if os.Getenv("BASE_URL") == "" {
baseURL = "cda.ms"
}
if os.Getenv("DB_PATH") == "" {
var err error
dbPath, err = os.Getwd()
if err != nil {
log.Fatal("Unable to get executable path", err)
}
}
fmt.Println(path.Join(dbPath, "db.sqlite"))
db := sqlite{Path: path.Join(dbPath, "db.sqlite")}
db.Init()
if !strings.HasSuffix(baseURL, "/") {
baseURL += "/"
}
r := mux.NewRouter()
r.HandleFunc("/save",
func(response http.ResponseWriter, request *http.Request) {
encodeHandler(response, request, db, baseURL)
}).Methods("POST")
r.HandleFunc("/{shortcode}", func(response http.ResponseWriter, request *http.Request) {
decodeHandler(response, request, db)
})
r.PathPrefix("/").Handler(http.FileServer(http.Dir("public")))
h := cors.Default().Handler(r)
log.Println("Starting server on port :1337")
log.Fatal(http.ListenAndServe(":1337", handlers.LoggingHandler(os.Stdout, h)))
}
func decodeHandler(response http.ResponseWriter, request *http.Request, db Database) {
shortcode := mux.Vars(request)["shortcode"]
url, err := db.Get(shortcode)
if err != nil {
http.Error(response, `{"error": "No such URL"}`, http.StatusNotFound)
return
}
http.Redirect(response, request, url, 301)
}
func encodeHandler(response http.ResponseWriter, request *http.Request, db Database, baseURL string) {
decoder := json.NewDecoder(request.Body)
var data struct {
URL string `json:"url"`
ShortCode string `json:"short_code"`
}
err := decoder.Decode(&data)
if err != nil {
http.Error(response, `{"error": "Unable to parse json"}`, http.StatusBadRequest)
return
}
if !govalidator.IsURL(data.URL) {
http.Error(response, `{"error": "Not a valid URL"}`, http.StatusBadRequest)
return
}
if data.ShortCode == "" {
http.Error(response, `{"error": "Not a valid short code"}`, http.StatusBadRequest)
return
}
shortcode, err := db.Save(data.ShortCode, data.URL)
if err != nil {
log.Println(err)
if strings.Contains(err.Error(), "constraint") {
http.Error(response, `{"error": "duplicate short code"}`, http.StatusBadRequest)
return
}
http.Error(response, `{"error": "unable to save"}`, http.StatusInternalServerError)
return
}
resp := map[string]string{"url": baseURL + shortcode, "short_code": shortcode, "error": ""}
jsonData, _ := json.Marshal(resp)
response.Write(jsonData)
}