primer intento de servicio para acortar urls

This commit is contained in:
2026-03-12 00:13:46 -03:00
commit 1b3ae43d61
9 changed files with 161 additions and 0 deletions

39
main.go Normal file
View File

@@ -0,0 +1,39 @@
package main
import (
"fmt"
"net/http"
"os"
"url-short/models"
"url-short/routes"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func main() {
fmt.Println("Iniciando url-short")
db, err := gorm.Open(sqlite.Open("shorturls.db"), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
fmt.Println("Se conecto a la base de datos")
db.AutoMigrate(&models.Url{})
http.HandleFunc("/url", func(w http.ResponseWriter, r *http.Request){
routes.Url(w, r, db)
})
port := ":8080"
if envPort := os.Getenv("PORT"); envPort != "" {
port = ":" + envPort
}
fmt.Println("Servidor escuchando en puerto: http://localhost"+ port)
err = http.ListenAndServe(port, nil)
if err != nil {
panic("Error starting server: " + err.Error())
}
}