2024-12-06 10:38:37 +01:00
package cmd
import (
2024-12-06 13:55:01 +01:00
"database/sql"
2024-12-06 10:38:37 +01:00
"fmt"
2024-12-06 13:55:01 +01:00
"log"
2024-12-06 10:38:37 +01:00
"net/http"
"github.com/gin-gonic/gin"
2024-12-06 13:55:01 +01:00
_ "github.com/lib/pq"
2024-12-06 10:38:37 +01:00
"github.com/spf13/viper"
)
var (
ListenPort = "8080"
2024-12-06 13:55:01 +01:00
2024-12-09 10:42:31 +01:00
db * sql . DB
2024-12-06 13:55:01 +01:00
DB_Host string
DB_Port string
DB_User string
DB_Pass string
DB_Name string
2024-12-06 10:38:37 +01:00
)
2024-12-06 13:55:01 +01:00
func getDBInfo ( ) error {
dbhost := viper . GetString ( "Server.DB_Host" )
if dbhost == "" {
e := fmt . Errorf ( "No database IP address present in config file!\n" )
return e
} else {
DB_Host = dbhost
}
dbport := viper . GetString ( "Server.DB_Port" )
if dbport == "" {
e := fmt . Errorf ( "No database port present in config file!\n" )
return e
} else {
DB_Port = dbport
}
dbuser := viper . GetString ( "Server.DB_User" )
if dbuser == "" {
e := fmt . Errorf ( "No database username present in config file!\n" )
return e
} else {
DB_User = dbuser
}
dbpass := viper . GetString ( "Server.DB_Pass" )
if dbpass == "" {
e := fmt . Errorf ( "No database password present in config file!\n" )
return e
} else {
DB_Pass = dbpass
}
dbname := viper . GetString ( "Server.DB_Name" )
if dbname == "" {
e := fmt . Errorf ( "No database name present in config file!\n" )
return e
} else {
DB_Name = dbname
}
return nil
}
2024-12-09 10:42:31 +01:00
func openDB ( ) error {
2024-12-06 13:55:01 +01:00
psqlconn := fmt . Sprintf ( "host=%s port=%s user=%s password=%s dbname=%s sslmode=disable" , DB_Host , DB_Port , DB_User , DB_Pass , DB_Name )
2024-12-09 10:42:31 +01:00
dba , err := sql . Open ( "postgres" , psqlconn )
2024-12-06 13:55:01 +01:00
if err != nil {
2024-12-09 10:42:31 +01:00
return err
2024-12-06 13:55:01 +01:00
}
2024-12-09 10:42:31 +01:00
db = dba
return nil
2024-12-06 13:55:01 +01:00
}
2024-12-06 10:38:37 +01:00
2024-12-06 13:55:01 +01:00
func setPort ( ) {
2024-12-06 10:38:37 +01:00
p := viper . GetString ( "Server.Port" )
if p != "" {
ListenPort = p
}
2024-12-06 13:55:01 +01:00
}
2024-12-06 10:38:37 +01:00
2024-12-06 13:55:01 +01:00
func server ( ) {
log . SetPrefix ( "[DRAHOOT] " )
setPort ( )
if err := getDBInfo ( ) ; err != nil {
2024-12-09 10:42:31 +01:00
log . Fatalf ( "INVALID DB INFO: %v\nPlease refer to the example configuration file in the repo at https://git.bulgariu.xyz/raul/drahoot/src/branch/main/sample-config (default config path = ~/.config/drahoot/drahoot.toml)" , err )
}
if err := openDB ( ) ; err != nil {
log . Fatalf ( "Database machine broke: %v\n" , err )
2024-12-06 13:55:01 +01:00
}
gin . SetMode ( gin . ReleaseMode )
2024-12-06 10:38:37 +01:00
r := gin . Default ( )
r . GET ( "/" , helloWorld )
2024-12-09 10:42:31 +01:00
// TODO: Have fun creating new endpoints
r . GET ( "/api/users" , getUsers )
2024-12-06 10:38:37 +01:00
r . Run ( ":" + ListenPort )
}
2024-12-09 10:42:31 +01:00
func getUsers ( c * gin . Context ) {
users := [ ] user { }
rows , err := db . Query ( "SELECT * FROM alumnos" )
2024-12-06 13:55:01 +01:00
if err != nil {
2024-12-09 10:42:31 +01:00
e := fmt . Sprintf ( "SOMETHING BAD HAPPENED QUERYING THE DATABASE: %v\n" , err )
log . Print ( e )
2024-12-06 13:55:01 +01:00
c . String ( http . StatusInternalServerError , e )
return
}
2024-12-09 10:42:31 +01:00
defer rows . Close ( )
for rows . Next ( ) {
user := user { }
err = rows . Scan ( & user . Id , & user . Name , & user . Surname1 , & user . Surname2 , & user . Email )
if err != nil {
e := fmt . Sprintf ( "SOMETHING BAD HAPPENED SCANNING THE ROWS: %v\n" , err )
log . Print ( e )
c . String ( http . StatusInternalServerError , e )
return
}
users = append ( users , user )
2024-12-06 13:55:01 +01:00
}
2024-12-09 10:42:31 +01:00
c . JSON ( http . StatusOK , users )
2024-12-06 13:55:01 +01:00
}
2024-12-09 10:42:31 +01:00
// func createUser(c *gin.Context) {
// var newUser user
// if err := c.BindJSON(&newUser); err != nil {
// return
// }
// dynStmt := `INSERT INTO "alumnos"("nombre", "apellido1", "apellido2", "email") values($1, $2, $3, $4)`
// db, err := openDB()
// if err != nil {
// e := fmt.Sprintf("We couldn't complete your request because the database server exploded, more at 11: %v\n", err)
// c.String(http.StatusInternalServerError, e)
// return
// }
//
// _, e := db.Exec(dynStmt, newUser.Name, newUser.Surname1, newUser.Surname2, newUser.Email)
// if e != nil {
// e := fmt.Sprintf("We couldn't complete your request because the database server imploded, more at 11: %v\n", err)
// c.String(http.StatusInternalServerError, e)
// return
// }
//
// fmt.Println(newUser)
// c.String(http.StatusOK, "Everything's fine!\n")
// }
2024-12-06 10:38:37 +01:00
func helloWorld ( c * gin . Context ) {
ua := c . Request . UserAgent ( )
message := fmt . Sprintf ( "Hello %v!\n" , ua )
c . String ( http . StatusOK , message )
}