mirror of
https://github.com/aykhans/oh-my-url.git
synced 2025-09-05 04:43:35 +00:00
🎉 first commit
This commit is contained in:
27
app/http_handlers/base.go
Normal file
27
app/http_handlers/base.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package httpHandlers
|
||||
|
||||
import (
|
||||
"github.com/aykhans/oh-my-url/app/db"
|
||||
"github.com/aykhans/oh-my-url/app/utils"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type HandlerCreate struct {
|
||||
DB db.DB
|
||||
ForwardDomain string
|
||||
}
|
||||
|
||||
type HandlerForward struct {
|
||||
DB db.DB
|
||||
CreateDomain string
|
||||
}
|
||||
|
||||
func FaviconHandler(w http.ResponseWriter, r *http.Request) {
|
||||
http.ServeFile(w, r, utils.GetTemplatePaths("favicon.ico")[0])
|
||||
}
|
||||
|
||||
func InternalServerError(w http.ResponseWriter, err error) {
|
||||
log.Fatal(err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
}
|
75
app/http_handlers/url_create.go
Normal file
75
app/http_handlers/url_create.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package httpHandlers
|
||||
|
||||
import (
|
||||
"github.com/aykhans/oh-my-url/app/utils"
|
||||
"html/template"
|
||||
"net/http"
|
||||
netUrl "net/url"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
type CreateData struct {
|
||||
ShortedURL string
|
||||
MainURL string
|
||||
Error string
|
||||
}
|
||||
|
||||
func (hl *HandlerCreate) UrlCreate(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
tmpl, err := template.ParseFiles(utils.GetTemplatePaths("index.html")...)
|
||||
if err != nil {
|
||||
InternalServerError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
err = tmpl.Execute(w, nil)
|
||||
if err != nil {
|
||||
InternalServerError(w, err)
|
||||
return
|
||||
}
|
||||
case http.MethodPost:
|
||||
url := r.FormValue("url")
|
||||
urlRegex := regexp.MustCompile(`^(http|https)://[a-zA-Z0-9.-]+(?:\:[0-9]+)?(?:/[^\s]*)?$`)
|
||||
isValidUrl := urlRegex.MatchString(url)
|
||||
if !isValidUrl {
|
||||
data := CreateData{
|
||||
MainURL: url,
|
||||
Error: "Invalid URL",
|
||||
}
|
||||
err = tmpl.Execute(w, data)
|
||||
if err != nil {
|
||||
InternalServerError(w, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
key, err := hl.DB.CreateURL(url)
|
||||
if err != nil {
|
||||
InternalServerError(w, err)
|
||||
return
|
||||
}
|
||||
shortedURL, err := netUrl.JoinPath(hl.ForwardDomain, key)
|
||||
if err != nil {
|
||||
InternalServerError(w, err)
|
||||
return
|
||||
}
|
||||
data := CreateData{
|
||||
ShortedURL: shortedURL,
|
||||
MainURL: url,
|
||||
}
|
||||
err = tmpl.Execute(w, data)
|
||||
if err != nil {
|
||||
InternalServerError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
default:
|
||||
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
26
app/http_handlers/url_forward.go
Normal file
26
app/http_handlers/url_forward.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package httpHandlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (hl *HandlerForward) UrlForward(w http.ResponseWriter, r *http.Request) {
|
||||
path := r.URL.Path
|
||||
segments := strings.Split(path, "/")
|
||||
if len(segments) > 2 {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
} else if segments[1] == "" {
|
||||
http.Redirect(w, r, hl.CreateDomain, http.StatusMovedPermanently)
|
||||
return
|
||||
}
|
||||
|
||||
key := segments[1]
|
||||
url, err := hl.DB.GetURL(key)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, url, http.StatusMovedPermanently)
|
||||
}
|
Reference in New Issue
Block a user