mirror of
https://github.com/4rkal/shortr.git
synced 2025-01-28 18:46:28 +02:00
minor fixed for postgres
This commit is contained in:
parent
3968e10d20
commit
37d5e4bb80
17
cmd/main.go
17
cmd/main.go
@ -11,6 +11,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/4rkal/shortr/models"
|
"github.com/4rkal/shortr/models"
|
||||||
@ -32,6 +33,7 @@ type StatsFormData struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var linkMap = map[string]*models.Link{}
|
var linkMap = map[string]*models.Link{}
|
||||||
|
var linkMapMutex sync.RWMutex
|
||||||
|
|
||||||
var baseurl *string
|
var baseurl *string
|
||||||
var db *sql.DB
|
var db *sql.DB
|
||||||
@ -46,10 +48,6 @@ func init() {
|
|||||||
panic(fmt.Sprintf("Failed to connect to database: %v", err))
|
panic(fmt.Sprintf("Failed to connect to database: %v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := db.Ping(); err != nil {
|
|
||||||
panic(fmt.Sprintf("Failed to ping database: %v", err))
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := loadLinksIntoMemory(); err != nil {
|
if err := loadLinksIntoMemory(); err != nil {
|
||||||
panic(fmt.Sprintf("Failed to load links into memory: %v", err))
|
panic(fmt.Sprintf("Failed to load links into memory: %v", err))
|
||||||
}
|
}
|
||||||
@ -64,10 +62,6 @@ func init() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error creating table: ", err)
|
log.Fatal("Error creating table: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := loadLinksIntoMemory(); err != nil {
|
|
||||||
log.Fatalf("Failed to load links into memory: %v", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -90,7 +84,9 @@ func main() {
|
|||||||
func RedirectHandler(c echo.Context) error {
|
func RedirectHandler(c echo.Context) error {
|
||||||
id := c.Param("id")
|
id := c.Param("id")
|
||||||
|
|
||||||
|
linkMapMutex.RLock()
|
||||||
link, found := linkMap[id]
|
link, found := linkMap[id]
|
||||||
|
linkMapMutex.RUnlock()
|
||||||
if !found {
|
if !found {
|
||||||
return c.String(http.StatusNotFound, "Link not found")
|
return c.String(http.StatusNotFound, "Link not found")
|
||||||
}
|
}
|
||||||
@ -134,7 +130,9 @@ func SubmitHandler(c echo.Context) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
linkMapMutex.Lock()
|
||||||
linkMap[id] = &models.Link{Id: id, Url: data.Url}
|
linkMap[id] = &models.Link{Id: id, Url: data.Url}
|
||||||
|
linkMapMutex.Unlock()
|
||||||
|
|
||||||
_, err := db.Exec("INSERT INTO links (id, url, clicks) VALUES ($1, $2, $3)", id, data.Url, 0)
|
_, err := db.Exec("INSERT INTO links (id, url, clicks) VALUES ($1, $2, $3)", id, data.Url, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -202,6 +200,9 @@ func loadLinksIntoMemory() error {
|
|||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
|
linkMapMutex.Lock()
|
||||||
|
defer linkMapMutex.Unlock()
|
||||||
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var id, url string
|
var id, url string
|
||||||
var clicks int
|
var clicks int
|
||||||
|
@ -22,9 +22,10 @@ services:
|
|||||||
POSTGRES_DB: shortr
|
POSTGRES_DB: shortr
|
||||||
ports:
|
ports:
|
||||||
- "5432:5432"
|
- "5432:5432"
|
||||||
|
restart: unless-stopped
|
||||||
volumes:
|
volumes:
|
||||||
- pgdata:/var/lib/postgresql/data
|
- pgdata:/var/lib/postgresql/data
|
||||||
|
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
pgdata:
|
pgdata:
|
||||||
|
driver: local
|
||||||
|
Loading…
Reference in New Issue
Block a user