primer intento de servicio para acortar urls
This commit is contained in:
39
main.go
Normal file
39
main.go
Normal 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())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user