2024-12-06 10:38:37 +01:00
package cmd
import (
2024-12-10 11:36:25 +01:00
"crypto/sha256"
2024-12-06 13:55:01 +01:00
"database/sql"
2024-12-10 11:36:25 +01:00
"encoding/hex"
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 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 {
2024-12-11 18:10:25 +01:00
log . Fatalf ( "Error happened trying to connect to database: %v" , err )
2024-12-06 13:55:01 +01:00
}
gin . SetMode ( gin . ReleaseMode )
2024-12-06 10:38:37 +01:00
r := gin . Default ( )
2024-12-12 12:34:54 +01:00
r . GET ( "/api/ping" , ping )
2024-12-12 14:30:58 +01:00
// CRUD for users
2024-12-10 11:36:25 +01:00
r . GET ( "/api/user" , getUsers )
2024-12-09 17:21:03 +01:00
r . GET ( "/api/user/:userid" , getUser )
2024-12-10 11:36:25 +01:00
r . POST ( "/api/user" , createUser )
2024-12-11 18:10:25 +01:00
r . DELETE ( "/api/user/:userid" , deleteUser )
2024-12-12 12:34:54 +01:00
r . PUT ( "/api/user/:userid" , modifyUser )
2024-12-06 10:38:37 +01:00
2024-12-12 14:30:58 +01:00
// TODO: Finish the following:
// CRUD for quizzes
// CRUD for quiz questions
// CRUD for question answers
// CRUD for quiz attempts
2024-12-12 11:25:22 +01:00
2024-12-12 14:30:58 +01:00
r . Run ( ":" + ListenPort )
2024-12-10 11:36:25 +01:00
}
2024-12-11 12:36:02 +01:00
func setResponse ( content any , success bool ) response {
msg := response { Contents : content , Success : success }
return msg
}
2024-12-12 11:29:46 +01:00
func ping ( c * gin . Context ) {
c . IndentedJSON ( http . StatusOK , setResponse ( "Pong!" , true ) )
2024-12-06 10:38:37 +01:00
}
2024-12-12 12:34:54 +01:00
func hashPW ( plain string ) string {
hashedPW := sha256 . New ( )
hashedPW . Write ( [ ] byte ( plain ) )
sha256hash := hex . EncodeToString ( hashedPW . Sum ( nil ) )
return sha256hash
}
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 ( ) 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 )
dba , err := sql . Open ( "postgres" , psqlconn )
if err != nil {
return err
}
db = dba
return nil
}
func setPort ( ) {
p := viper . GetString ( "Server.Port" )
if p != "" {
ListenPort = p
}
}