-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
221 lines (181 loc) · 4.86 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"bufio"
"crypto/rand"
"encoding/base64"
"fmt"
"log"
"net/http"
"net/url"
"os"
"strings"
"sync"
"github.com/go-ini/ini"
)
var (
mu sync.Mutex
urlMap = make(map[string]string)
config *Config
)
type Config struct {
URLsFile string
BaseURL string
Path string
Password string
Port string
}
func main() {
var err error
config, err = loadConfig("config.ini")
if err != nil {
log.Fatalf("Error loading configuration: %v\n", err)
}
if _, err := os.Stat(config.URLsFile); os.IsNotExist(err) {
file, err := os.Create(config.URLsFile)
if err != nil {
log.Fatalf("Could not create file: %v\n", err)
}
file.Close()
log.Printf("File %s created successfully\n", config.URLsFile)
}
loadUrls()
http.HandleFunc(config.Path+"/shorten", passwordProtected(shortenHandler))
http.HandleFunc(config.Path+"/", redirectHandler)
log.Printf("Starting server on :%s...", config.Port)
log.Fatal(http.ListenAndServe(":"+config.Port, nil))
}
func loadConfig(filename string) (*Config, error) {
cfg, err := ini.Load(filename)
if err != nil {
return nil, err
}
return &Config{
URLsFile: cfg.Section("server").Key("urlsfile").String(),
BaseURL: cfg.Section("server").Key("baseurl").String(),
Path: cfg.Section("server").Key("path").String(),
Password: cfg.Section("server").Key("password").String(),
Port: cfg.Section("server").Key("port").String(),
}, nil
}
func shortenHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}
err := r.ParseForm()
if err != nil {
http.Error(w, "Failed to parse form", http.StatusBadRequest)
return
}
password := r.FormValue("password")
originalURL := r.FormValue("url")
log.Printf("Received URL: '%s'", originalURL)
if password != config.Password {
log.Println("Unauthorized access attempt")
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
if originalURL == "" || !isValidURL(originalURL) {
log.Println("Invalid URL")
http.Error(w, "Invalid URL", http.StatusBadRequest)
return
}
shortURL := generateShortURL(originalURL)
mu.Lock()
urlMap[shortURL] = originalURL
saveUrl(shortURL, originalURL)
mu.Unlock()
log.Printf("Short URL created: https://%s%s/%s", config.BaseURL, config.Path, shortURL)
fmt.Fprintf(w, "https://%s%s/%s\n", config.BaseURL, config.Path, shortURL)
}
func redirectHandler(w http.ResponseWriter, r *http.Request) {
shortURL := strings.TrimPrefix(r.URL.Path, config.Path+"/")
mu.Lock()
originalURL, exists := urlMap[shortURL]
mu.Unlock()
if !exists {
http.Error(w, "URL not found", http.StatusNotFound)
return
}
http.Redirect(w, r, originalURL, http.StatusFound)
}
func generateShortURL(url string) string {
const maxAttempts = 1000
attempts := 0
for attempts < maxAttempts {
b := make([]byte, 5)
if _, err := rand.Read(b); err != nil {
log.Fatal(err)
}
shortURL := base64.URLEncoding.EncodeToString(b)[:5]
mu.Lock()
_, exists := urlMap[shortURL]
mu.Unlock()
if !exists {
return shortURL
}
attempts++
}
log.Fatalf("Unable to generate unique short URL after %d attempts", maxAttempts)
return ""
}
func loadUrls() {
log.Printf("Attempting to open file: %s\n", config.URLsFile)
file, err := os.Open(config.URLsFile)
if err != nil {
log.Printf("Could not open file: %v\n", err)
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
parts := strings.SplitN(line, " ", 2)
if len(parts) == 2 {
urlMap[parts[0]] = parts[1]
log.Printf("Loaded URL mapping: %s -> %s\n", parts[0], parts[1])
} else {
log.Printf("Skipping invalid line: %s\n", line)
}
}
if err := scanner.Err(); err != nil {
log.Printf("Error reading file: %v\n", err)
}
}
func saveUrl(shortURL, originalURL string) {
file, err := os.OpenFile(config.URLsFile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
log.Printf("Could not open file: %v\n", err)
return
}
defer file.Close()
_, err = fmt.Fprintf(file, "%s %s\n", shortURL, originalURL)
if err != nil {
log.Printf("Could not write to file: %v\n", err)
} else {
log.Printf("Saved URL mapping: %s -> %s\n", shortURL, originalURL)
}
}
func isValidURL(urlStr string) bool {
_, err := url.ParseRequestURI(urlStr)
return err == nil
}
func passwordProtected(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}
err := r.ParseForm()
if err != nil {
http.Error(w, "Failed to parse form", http.StatusBadRequest)
return
}
password := r.FormValue("password")
if password != config.Password {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
next.ServeHTTP(w, r)
}
}