Files
url-short/main.go

40 lines
760 B
Go

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())
}
}