Configure Gin and tinker with DB comms
Added a handler for adding new users to Postgres through a POST request
This commit is contained in:
parent
4d5c5d9c93
commit
699547584d
|
@ -1,31 +1,124 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var (
|
||||
ListenPort = "8080"
|
||||
|
||||
DB_Host string
|
||||
DB_Port string
|
||||
DB_User string
|
||||
DB_Pass string
|
||||
DB_Name string
|
||||
)
|
||||
|
||||
func server() {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
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
|
||||
}
|
||||
|
||||
func openDB() (*sql.DB, error) {
|
||||
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)
|
||||
db, err := sql.Open("postgres", psqlconn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func setPort() {
|
||||
p := viper.GetString("Server.Port")
|
||||
if p != "" {
|
||||
ListenPort = p
|
||||
}
|
||||
}
|
||||
|
||||
func server() {
|
||||
log.SetPrefix("[DRAHOOT] ")
|
||||
setPort()
|
||||
if err := getDBInfo(); err != nil {
|
||||
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", err)
|
||||
}
|
||||
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
r := gin.Default()
|
||||
r.GET("/", helloWorld)
|
||||
r.POST("/api/user", createUser)
|
||||
|
||||
r.Run(":" + ListenPort)
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
func helloWorld(c *gin.Context) {
|
||||
ua := c.Request.UserAgent()
|
||||
message := fmt.Sprintf("Hello %v!\n", ua)
|
||||
|
|
|
@ -10,4 +10,3 @@ services:
|
|||
volumes:
|
||||
- ./db-config/db-data/:/var/lib/postgresql/data/
|
||||
- ./db-config/init.sql:/docker-entrypoint-initdb.d/init.sql
|
||||
user: "1000:1000"
|
||||
|
|
1
go.mod
1
go.mod
|
@ -4,6 +4,7 @@ go 1.23.3
|
|||
|
||||
require (
|
||||
github.com/gin-gonic/gin v1.10.0
|
||||
github.com/lib/pq v1.10.9
|
||||
github.com/spf13/cobra v1.8.1
|
||||
github.com/spf13/viper v1.19.0
|
||||
)
|
||||
|
|
2
go.sum
2
go.sum
|
@ -50,6 +50,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
|||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
||||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
|
||||
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
|
|
Loading…
Reference in New Issue