🔨 Refactor URL handling logic and remove unused variables

This commit is contained in:
Aykhan Shahsuvarov 2024-03-07 15:32:38 +04:00
parent f7c4239163
commit a947b98acf
5 changed files with 16 additions and 8 deletions

View File

@ -37,6 +37,14 @@ func GetAppConfig() *AppConfig {
} }
} }
func GetForwardDomain() string {
return GetEnvOrPanic("FORWARD_DOMAIN")
}
func GetCreateDomain() string {
return GetEnvOrPanic("CREATE_DOMAIN")
}
func GetPostgresConfig() *PostgresConfig { func GetPostgresConfig() *PostgresConfig {
return &PostgresConfig{ return &PostgresConfig{
USER: GetEnvOrPanic("POSTGRES_USER"), USER: GetEnvOrPanic("POSTGRES_USER"),

View File

@ -9,12 +9,10 @@ import (
type HandlerCreate struct { type HandlerCreate struct {
DB db.DB DB db.DB
ForwardDomain string
} }
type HandlerForward struct { type HandlerForward struct {
DB db.DB DB db.DB
CreateDomain string
} }
func FaviconHandler(w http.ResponseWriter, r *http.Request) { func FaviconHandler(w http.ResponseWriter, r *http.Request) {

View File

@ -2,6 +2,7 @@ package httpHandlers
import ( import (
"github.com/aykhans/oh-my-url/app/utils" "github.com/aykhans/oh-my-url/app/utils"
"github.com/aykhans/oh-my-url/app/config"
"html/template" "html/template"
"net/http" "net/http"
netUrl "net/url" netUrl "net/url"
@ -54,7 +55,7 @@ func (hl *HandlerCreate) UrlCreate(w http.ResponseWriter, r *http.Request) {
InternalServerError(w, err) InternalServerError(w, err)
return return
} }
shortedURL, err := netUrl.JoinPath(hl.ForwardDomain, key) shortedURL, err := netUrl.JoinPath(config.GetForwardDomain(), key)
if err != nil { if err != nil {
InternalServerError(w, err) InternalServerError(w, err)
return return

View File

@ -1,6 +1,7 @@
package httpHandlers package httpHandlers
import ( import (
"github.com/aykhans/oh-my-url/app/config"
"net/http" "net/http"
"strings" "strings"
) )
@ -12,7 +13,7 @@ func (hl *HandlerForward) UrlForward(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r) http.NotFound(w, r)
return return
} else if segments[1] == "" { } else if segments[1] == "" {
http.Redirect(w, r, hl.CreateDomain, http.StatusMovedPermanently) http.Redirect(w, r, config.GetCreateDomain(), http.StatusMovedPermanently)
return return
} }

View File

@ -12,13 +12,13 @@ func main() {
config := config.GetAppConfig() config := config.GetAppConfig()
dbCreate := db.GetDB() dbCreate := db.GetDB()
dbCreate.Init() dbCreate.Init()
handlerCreate := httpHandlers.HandlerCreate{DB: dbCreate, ForwardDomain: config.FORWARD_DOMAIN} handlerCreate := httpHandlers.HandlerCreate{DB: dbCreate}
urlCreateMux := http.NewServeMux() urlCreateMux := http.NewServeMux()
urlCreateMux.HandleFunc("/", handlerCreate.UrlCreate) urlCreateMux.HandleFunc("/", handlerCreate.UrlCreate)
urlCreateMux.HandleFunc("/favicon.ico", httpHandlers.FaviconHandler) urlCreateMux.HandleFunc("/favicon.ico", httpHandlers.FaviconHandler)
dbRead := db.GetDB() dbRead := db.GetDB()
handlerForward := httpHandlers.HandlerForward{DB: dbRead, CreateDomain: config.CREATE_DOMAIN} handlerForward := httpHandlers.HandlerForward{DB: dbRead}
urlReadMux := http.NewServeMux() urlReadMux := http.NewServeMux()
urlReadMux.HandleFunc("/", handlerForward.UrlForward) urlReadMux.HandleFunc("/", handlerForward.UrlForward)
urlReadMux.HandleFunc("/favicon.ico", httpHandlers.FaviconHandler) urlReadMux.HandleFunc("/favicon.ico", httpHandlers.FaviconHandler)