Bookholder-API/server/server.go

92 lines
2.0 KiB
Go
Raw Permalink Normal View History

2024-12-14 01:12:47 +00:00
package server
2024-12-20 00:52:27 +00:00
import (
2024-12-20 14:19:33 +00:00
"database/sql"
2024-12-20 00:52:27 +00:00
"net/http"
"github.com/LeRoid-hub/Bookholder-API/database"
"github.com/gin-gonic/gin"
)
/*
GET /Account/:id
GET /Transaction/:TransactionID
GET /Transactions/:AccountID/:year
Get /Transactions/:AccountID/:year/:month
GET /User/:UserID
POST /NewAccount
POST /NewTransaction
POST /NewUser
PUT /UpdateAccount/:AccountID
PUT /UpdateTransaction/:TransactionID
PUT /UpdateUser/:UserID
DELETE /DeleteAccount/:AccountID
DELETE /DeleteTransaction/:TransactionID
DELETE /DeleteUser/:UserID
*/
2024-12-20 14:19:33 +00:00
var (
Database *sql.DB
Env map[string]string
)
2024-12-20 13:28:54 +00:00
func Run(env map[string]string, db *database.DB) {
2024-12-20 14:19:33 +00:00
dbase, err := database.New()
if err != nil {
panic(err)
}
Database = dbase
2024-12-20 00:52:27 +00:00
r := gin.Default()
// Index
r.GET("/", welcome)
2024-12-20 00:52:27 +00:00
v1 := r.Group("/v1")
{
//Welcome
v1.GET("/", welcome)
2024-12-20 00:52:27 +00:00
//Account
2024-12-20 14:19:33 +00:00
v1.GET("/Account/:AccountID", checkAuth, getAccount)
v1.POST("/NewAccount", checkAuth, newAccount)
v1.PUT("/UpdateAccount", checkAuth, updateAccount)
2024-12-20 14:19:33 +00:00
v1.DELETE("/DeleteAccount/:AccountID", checkAuth, deleteAccount)
2024-12-20 00:52:27 +00:00
//Transaction
2024-12-20 14:19:33 +00:00
v1.GET("/Transaction/:TransactionID", checkAuth, getTransaction)
v1.GET("/Transactions/:AccountID/:year", checkAuth, getTransactions)
v1.GET("/Transactions/:AccountID/:year/:month", checkAuth, getTransactions)
v1.POST("/NewTransaction", checkAuth, newTransaction)
v1.PUT("/UpdateTransaction/:TransactionID", checkAuth, updateTransaction)
v1.DELETE("/DeleteTransaction/:TransactionID", checkAuth, deleteTransaction)
2024-12-20 00:52:27 +00:00
//User
2024-12-20 14:19:33 +00:00
v1.GET("/User/", checkAuth, getUserProfile)
2024-12-20 14:30:37 +00:00
v1.POST("/NewUser", createUser)
2024-12-20 14:19:33 +00:00
v1.POST("/AuthenticateUser", authenticateUser)
2024-12-20 14:30:37 +00:00
v1.PUT("/UpdateUser/:UserID", checkAuth)
v1.DELETE("/DeleteUser/:UserID", checkAuth)
2024-12-20 00:52:27 +00:00
}
r.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})
})
if port, ok := env["PORT"]; ok {
r.Run(":" + port)
} else {
r.Run(":8080")
}
}
func welcome(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "Welcome to Bookholder API",
})
}