Delete main.go
This commit is contained in:
@@ -1,68 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"math/rand"
|
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
m := map[string]string{}
|
|
||||||
s := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
||||||
|
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if r.URL.Path == "/" && r.Method == "GET" {
|
|
||||||
fmt.Fprintln(w, "<h1>shortener</h1>")
|
|
||||||
fmt.Fprintln(w, "<form method='POST'>")
|
|
||||||
fmt.Fprintln(w, "<input name='url' placeholder='https://example.com'>")
|
|
||||||
fmt.Fprintln(w, "<button>ok</button>")
|
|
||||||
fmt.Fprintln(w, "</form>")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if r.URL.Path == "/" && r.Method == "POST" {
|
|
||||||
a := r.FormValue("url")
|
|
||||||
|
|
||||||
if a == "" {
|
|
||||||
fmt.Fprintln(w, "empty")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
u, err := url.Parse(a)
|
|
||||||
if err != nil || u.Scheme == "" || u.Host == "" {
|
|
||||||
fmt.Fprintln(w, "bad url")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
b := ""
|
|
||||||
for j := 0; j < 6; j++ {
|
|
||||||
b = b + string(s[rand.Intn(len(s))])
|
|
||||||
}
|
|
||||||
|
|
||||||
for m[b] != "" {
|
|
||||||
b = ""
|
|
||||||
for j := 0; j < 6; j++ {
|
|
||||||
b = b + string(s[rand.Intn(len(s))])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
m[b] = a
|
|
||||||
fmt.Fprintln(w, "short url: http://localhost:8080/"+b)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
x := r.URL.Path[1:]
|
|
||||||
y := m[x]
|
|
||||||
|
|
||||||
if y == "" {
|
|
||||||
fmt.Fprintln(w, "not found")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
http.Redirect(w, r, y, 302)
|
|
||||||
})
|
|
||||||
|
|
||||||
fmt.Println("start http://localhost:8080")
|
|
||||||
http.ListenAndServe(":8080", nil)
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user