Initial commit still with some hardcoded things
This commit is contained in:
+67
@@ -0,0 +1,67 @@
|
||||
// Package api will server the SW data via a JSON REST API.
|
||||
package api
|
||||
|
||||
// TODO: configure the logging
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
sw "github.com/ESilva15/PTInvCertSWDB/sw"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var ginMode = "debug"
|
||||
|
||||
func get(c *gin.Context) {
|
||||
param := c.Param("id")
|
||||
if param == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": "no certNo in request",
|
||||
"info": "user",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
certNo, err := strconv.Atoi(param)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": "certNo is not an integer",
|
||||
"info": "use an integer in the request",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
sw, err := sw.Get(certNo)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, sw)
|
||||
}
|
||||
|
||||
func RunServer() error {
|
||||
gin.SetMode(ginMode)
|
||||
router := gin.Default()
|
||||
|
||||
authGroup := router.Group("/")
|
||||
// authGroup.Use(APIAuthMiddleWare())
|
||||
authGroup.GET("/sw/:id", get)
|
||||
|
||||
// Star the SW service
|
||||
err := sw.StartSWService(sw.FILEDB)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = router.Run(":8086")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
)
|
||||
|
||||
type TokenData struct {
|
||||
Token string `json:"JWTToken"`
|
||||
}
|
||||
|
||||
const key = "stronk"
|
||||
|
||||
func validateToken(tStr string) (bool, error) {
|
||||
token, err := jwt.Parse(tStr, func(token *jwt.Token) (any, error) {
|
||||
return []byte(key), nil
|
||||
}, jwt.WithValidMethods([]string{jwt.SigningMethodHS256.Alg()}))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if _, ok := token.Claims.(jwt.MapClaims); ok {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func APIAuthMiddleWare() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
auth := c.GetHeader("Authorization")
|
||||
if !strings.HasPrefix(auth, "Bearer ") {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "missing token"})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
tokenStr := strings.TrimPrefix(auth, "Bearer ")
|
||||
validateToken, err := validateToken(tokenStr)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
if !validateToken {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "token is not valid"})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user