mirror of
https://github.com/aykhans/oh-my-url.git
synced 2025-04-19 03:09:42 +00:00
🐛 Fix error handling and logging
This commit is contained in:
parent
6265458bc3
commit
5af2714abb
@ -1,10 +1,10 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"log"
|
"log"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/aykhans/oh-my-url/app/errors"
|
||||||
"github.com/aykhans/oh-my-url/app/utils"
|
"github.com/aykhans/oh-my-url/app/utils"
|
||||||
"github.com/gocql/gocql"
|
"github.com/gocql/gocql"
|
||||||
)
|
)
|
||||||
@ -58,11 +58,11 @@ func (c *Cassandra) CreateURL(url string) (string, error) {
|
|||||||
applied, err := c.db.Query(query, id, key, url, 0).Consistency(gocql.All).MapScanCAS(m)
|
applied, err := c.db.Query(query, id, key, url, 0).Consistency(gocql.All).MapScanCAS(m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return "", err
|
return "", errors.ErrDBCreateURL
|
||||||
}
|
}
|
||||||
if !applied {
|
if !applied {
|
||||||
log.Println("Failed to insert unique key")
|
log.Println("Failed to insert unique key")
|
||||||
return "", errors.New("an error occurred, please try again later")
|
return "", errors.ErrDBCreateURL
|
||||||
}
|
}
|
||||||
c.currentID.ID = id
|
c.currentID.ID = id
|
||||||
|
|
||||||
@ -76,7 +76,11 @@ func (c *Cassandra) GetURL(key string) (string, error) {
|
|||||||
Consistency(gocql.One).
|
Consistency(gocql.One).
|
||||||
Scan(&url)
|
Scan(&url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
log.Println(err)
|
||||||
|
if err == gocql.ErrNotFound {
|
||||||
|
return "", errors.ErrDBURLNotFound
|
||||||
|
}
|
||||||
|
return "", errors.ErrDBGetURL
|
||||||
}
|
}
|
||||||
return url, nil
|
return url, nil
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/aykhans/oh-my-url/app/errors"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -9,10 +12,10 @@ type Postgres struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type url struct {
|
type url struct {
|
||||||
ID uint `gorm:"primaryKey"`
|
ID uint `gorm:"primaryKey"`
|
||||||
Key string `gorm:"unique;not null;size:15;default:null"`
|
Key string `gorm:"unique;not null;size:15;default:null"`
|
||||||
URL string `gorm:"not null;default:null"`
|
URL string `gorm:"not null;default:null"`
|
||||||
Count int `gorm:"not null;default:0"`
|
Count int `gorm:"not null;default:0"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Postgres) Init() {
|
func (p *Postgres) Init() {
|
||||||
@ -30,7 +33,8 @@ func (p *Postgres) CreateURL(mainUrl string) (string, error) {
|
|||||||
url := url{URL: mainUrl}
|
url := url{URL: mainUrl}
|
||||||
tx := p.gormDB.Create(&url)
|
tx := p.gormDB.Create(&url)
|
||||||
if tx.Error != nil {
|
if tx.Error != nil {
|
||||||
return "", tx.Error
|
log.Println(tx.Error)
|
||||||
|
return "", errors.ErrDBCreateURL
|
||||||
}
|
}
|
||||||
return url.Key, nil
|
return url.Key, nil
|
||||||
}
|
}
|
||||||
@ -39,7 +43,11 @@ func (p *Postgres) GetURL(key string) (string, error) {
|
|||||||
var result url
|
var result url
|
||||||
tx := p.gormDB.Where("key = ?", key).First(&result)
|
tx := p.gormDB.Where("key = ?", key).First(&result)
|
||||||
if tx.Error != nil {
|
if tx.Error != nil {
|
||||||
return "", tx.Error
|
log.Println(tx.Error)
|
||||||
|
if tx.Error == gorm.ErrRecordNotFound {
|
||||||
|
return "", errors.ErrDBURLNotFound
|
||||||
|
}
|
||||||
|
return "", errors.ErrDBGetURL
|
||||||
}
|
}
|
||||||
result.Count++
|
result.Count++
|
||||||
p.gormDB.Save(&result)
|
p.gormDB.Save(&result)
|
||||||
|
9
app/errors/api.go
Normal file
9
app/errors/api.go
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package errors
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrAPITemplateParsing = errors.New("an error occured while parsing template please try again later")
|
||||||
|
ErrAPIInvalidURL = errors.New("invalid URL")
|
||||||
|
ErrAPI503 = errors.New("service unavailable please try again later")
|
||||||
|
)
|
9
app/errors/db.go
Normal file
9
app/errors/db.go
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package errors
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrDBCreateURL = errors.New("an error occured please try again later")
|
||||||
|
ErrDBGetURL = errors.New("an error occured please try again later")
|
||||||
|
ErrDBURLNotFound = errors.New("url not found")
|
||||||
|
)
|
@ -23,3 +23,8 @@ func InternalServerError(w http.ResponseWriter, err error) {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Error503(w http.ResponseWriter, err error) {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, "Service Unavailable", http.StatusServiceUnavailable)
|
||||||
|
}
|
||||||
|
@ -1,18 +1,21 @@
|
|||||||
package httpHandlers
|
package httpHandlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/aykhans/oh-my-url/app/utils"
|
|
||||||
"github.com/aykhans/oh-my-url/app/config"
|
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
netUrl "net/url"
|
netUrl "net/url"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
|
"github.com/aykhans/oh-my-url/app/config"
|
||||||
|
"github.com/aykhans/oh-my-url/app/errors"
|
||||||
|
"github.com/aykhans/oh-my-url/app/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CreateData struct {
|
type CreateData struct {
|
||||||
ShortedURL string
|
ShortedURL string
|
||||||
MainURL string
|
MainURL string
|
||||||
Error string
|
Error error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hl *HandlerCreate) UrlCreate(w http.ResponseWriter, r *http.Request) {
|
func (hl *HandlerCreate) UrlCreate(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -23,7 +26,8 @@ func (hl *HandlerCreate) UrlCreate(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
tmpl, err := template.ParseFiles(utils.GetTemplatePaths("index.html")...)
|
tmpl, err := template.ParseFiles(utils.GetTemplatePaths("index.html")...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
InternalServerError(w, err)
|
log.Println(err)
|
||||||
|
InternalServerError(w, errors.ErrAPITemplateParsing)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -31,7 +35,8 @@ func (hl *HandlerCreate) UrlCreate(w http.ResponseWriter, r *http.Request) {
|
|||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
err = tmpl.Execute(w, nil)
|
err = tmpl.Execute(w, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
InternalServerError(w, err)
|
log.Println(err)
|
||||||
|
InternalServerError(w, errors.ErrAPITemplateParsing)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
case http.MethodPost:
|
case http.MethodPost:
|
||||||
@ -41,23 +46,33 @@ func (hl *HandlerCreate) UrlCreate(w http.ResponseWriter, r *http.Request) {
|
|||||||
if !isValidUrl {
|
if !isValidUrl {
|
||||||
data := CreateData{
|
data := CreateData{
|
||||||
MainURL: url,
|
MainURL: url,
|
||||||
Error: "Invalid URL",
|
Error: errors.ErrAPIInvalidURL,
|
||||||
}
|
}
|
||||||
err = tmpl.Execute(w, data)
|
err = tmpl.Execute(w, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
InternalServerError(w, err)
|
log.Println(err)
|
||||||
|
InternalServerError(w, errors.ErrAPITemplateParsing)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
key, err := hl.DB.CreateURL(url)
|
key, err := hl.DB.CreateURL(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
InternalServerError(w, err)
|
data := CreateData{
|
||||||
|
MainURL: url,
|
||||||
|
Error: errors.ErrAPI503,
|
||||||
|
}
|
||||||
|
err = tmpl.Execute(w, data)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
InternalServerError(w, errors.ErrAPITemplateParsing)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
shortedURL, err := netUrl.JoinPath(config.GetForwardDomain(), key)
|
shortedURL, err := netUrl.JoinPath(config.GetForwardDomain(), key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
InternalServerError(w, err)
|
log.Println(err)
|
||||||
|
InternalServerError(w, errors.ErrAPITemplateParsing)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
data := CreateData{
|
data := CreateData{
|
||||||
@ -66,7 +81,8 @@ func (hl *HandlerCreate) UrlCreate(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
err = tmpl.Execute(w, data)
|
err = tmpl.Execute(w, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
InternalServerError(w, err)
|
log.Println(err)
|
||||||
|
InternalServerError(w, errors.ErrAPITemplateParsing)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
24
app/main.go
24
app/main.go
@ -1,11 +1,13 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/aykhans/oh-my-url/app/config"
|
"github.com/aykhans/oh-my-url/app/config"
|
||||||
"github.com/aykhans/oh-my-url/app/db"
|
"github.com/aykhans/oh-my-url/app/db"
|
||||||
"github.com/aykhans/oh-my-url/app/http_handlers"
|
"github.com/aykhans/oh-my-url/app/http_handlers"
|
||||||
"net/http"
|
|
||||||
"sync"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -27,11 +29,25 @@ func main() {
|
|||||||
wg.Add(2)
|
wg.Add(2)
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
panic(http.ListenAndServe(":"+appConfig.LISTEN_PORT_CREATE, urlCreateMux))
|
for {
|
||||||
|
err := http.ListenAndServe(":"+appConfig.LISTEN_PORT_CREATE, urlCreateMux)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
panic(http.ListenAndServe(":"+appConfig.LISTEN_PORT_FORWARD, urlReadMux))
|
for {
|
||||||
|
err := http.ListenAndServe(":"+appConfig.LISTEN_PORT_FORWARD, urlReadMux)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
}
|
}
|
||||||
|
@ -93,7 +93,7 @@
|
|||||||
<h1>Oh My URL!</h1>
|
<h1>Oh My URL!</h1>
|
||||||
<form action="/" method="post">
|
<form action="/" method="post">
|
||||||
{{ if .Error }}
|
{{ if .Error }}
|
||||||
<label for="urlInput" style="color: red;">{{ .Error }}</label>
|
<label for="urlInput" style="color: red;">{{ .Error.Error }}</label>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
<input type="url" id="urlInput" name="url" placeholder="https://example.com/" value="{{ if .MainURL }}{{ .MainURL }}{{ end }}" required>
|
<input type="url" id="urlInput" name="url" placeholder="https://example.com/" value="{{ if .MainURL }}{{ .MainURL }}{{ end }}" required>
|
||||||
{{ if .ShortedURL }}
|
{{ if .ShortedURL }}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user