diff --git a/main.go b/main.go index 1751b43..92ea0fe 100644 --- a/main.go +++ b/main.go @@ -29,18 +29,8 @@ const ( letterIdxMask = 1< 65535 || port < 1 { + if *port > 65535 || *port < 1 { } - if port != 80 && proto == "http" { - hostSuf = ":" + strconv.Itoa(port) + "/" - } else if port != 443 && proto == "https" { - hostSuf = ":" + strconv.Itoa(port) + "/" - } else if port == 443 || port == 80 { + if *path != "" && !strings.HasSuffix(*path, "/") { + *path = *path + "/" + } + if *port != 80 && *proto == "http" { + hostSuf = ":" + strconv.Itoa(*port) + "/" + } else if *port != 443 && *proto == "https" { + hostSuf = ":" + strconv.Itoa(*port) + "/" + } else if *port == 443 || *port == 80 { hostSuf = "/" } - ip := net.ParseIP(addr) + ip := net.ParseIP(*addr) if ip != nil { - listenAddr = ip.String() + ":" + strconv.Itoa(port) + listenAddr = ip.String() + ":" + strconv.Itoa(*port) } else { - if govalidator.IsDNSName(addr) { - listenAddr = addr + ":" + strconv.Itoa(port) + if govalidator.IsDNSName(*addr) { + listenAddr = *addr + ":" + strconv.Itoa(*port) } else { log.Fatalln("Invalid ip address") } } - if !govalidator.IsDNSName(domain) { + if !govalidator.IsDNSName(*domain) { log.Fatalln("Invalid domain address") } r := mux.NewRouter() r.HandleFunc("/", index).Methods("GET") - r.HandleFunc("/", shortner).Methods("POST") - r.HandleFunc("/{key}", redirect).Methods("GET") + r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + shortner(w, r, *proto, *domain, hostSuf, *path, *urlSize) + }).Methods("POST") + r.HandleFunc("/{key}", func(w http.ResponseWriter, r *http.Request) { + redirect(w, r, *path) + }).Methods("GET") r.HandleFunc("/v1/count", itemsCount).Methods("GET") r.HandleFunc("/v1/dump", itemsDump).Methods("GET") - r.HandleFunc("/v1/dumpToFile", itemsDumpToFile).Methods("GET") - r.HandleFunc("/v1/fromFile", itemsFromFile).Methods("GET") + r.HandleFunc("/v1/dumpToFile", func(w http.ResponseWriter, r *http.Request) { + itemsDumpToFile(w, r, *dumpFile) + }).Methods("GET") + r.HandleFunc("/v1/fromFile", func(w http.ResponseWriter, r *http.Request) { + itemsFromFile(w, r, *dumpFile) + }).Methods("GET") r.HandleFunc("/v1/fromPost", itemsFromPost).Methods("POST") - log.Printf("Domain: %s, URL Proto: %s\n", domain, proto) + log.Printf("Domain: %s, URL Proto: %s, Listen Address: %s\n", *domain, *proto, *addr) log.Fatal(http.ListenAndServe(listenAddr, handlers.CombinedLoggingHandler(os.Stdout, r))) }