-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
67 lines (61 loc) · 1.23 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
package main
import (
"fmt"
"net/http"
"strings"
)
func ParseURL(url string) string {
_url := strings.Split(url, "://")[0]
_parsed := strings.Split(url, "://")[1]
if _url == "http" {
return _parsed
}
if _url == "https" {
return _parsed
} else {
return "https://" + _parsed
}
}
func handleConnection(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
url := r.FormValue("url")
short := GetShort(url)
if short == "" {
short = GenerateShort(url)
}
parsed := ParseURL(url)
_, err := http.Get(parsed)
if err != nil {
w.Write([]byte("Invalid URL"))
return
}
w.Write([]byte("http://45.142.114.111/" + short))
return
} else if r.Method == "GET" {
short := r.FormValue("short")
if short != "" {
url := GetURL(short)
if url != "" {
http.Redirect(w, r, url, http.StatusFound)
return
}
}
http.ServeFile(w, r, "index.html")
return
}
}
func HandleCode(w http.ResponseWriter, r *http.Request) {
url := GetURL(r.URL.Path[1:])
http.Redirect(w, r, url, http.StatusFound)
}
func main() {
CreateDB()
CreateTables()
Startup()
fmt.Println("Server started")
http.HandleFunc("/", handleConnection)
err := http.ListenAndServe("0.0.0.0:80", nil)
if err != nil {
panic(err)
}
}