This commit is contained in:
2023-06-23 12:21:35 -04:00
parent 612feee721
commit b01b26b12d
13 changed files with 290 additions and 144 deletions

View File

@@ -1,41 +1,85 @@
package controller
import (
"os"
"log"
"net/http"
"strings"
openai "github.com/sashabaranov/go-openai"
"github.com/caarlos0/env/v6"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/sashabaranov/go-openai"
"gitlab.com/DeveloperDurp/DurpAPI/model"
)
type Controller struct {
openaiClient *openai.Client
unraidAPIKey string
unraidURI string
ClientID string
ClientSecret string
RedirectURL string
AuthURL string
TokenURL string
Cfg model.Config
dbcfg model.DBConfig
}
func NewController() *Controller {
err := godotenv.Load(".env")
if err != nil {
log.Fatalf("unable to load file: %e", err)
}
controller := &Controller{}
controller.Cfg = model.Config{}
controller.dbcfg = model.DBConfig{}
openaiApiKey := os.Getenv("OPENAI_API_KEY")
unraidAPIKey := os.Getenv("UNRAID_API_KEY")
unraidURI := os.Getenv("UNRAID_URI")
ClientID := os.Getenv("ClientID")
ClientSecret := os.Getenv("ClientSecret")
RedirectURL := os.Getenv("RedirectURL")
AuthURL := os.Getenv("AuthURL")
TokenURL := os.Getenv("TokenURL")
err = env.Parse(&controller.Cfg)
if err != nil {
log.Fatalf("unable to parse environment variables: %e", err)
}
err = env.Parse(&controller.dbcfg)
if err != nil {
log.Fatalf("unable to parse database variables: %e", err)
}
controller.Cfg.OpenaiClient = *openai.NewClient(controller.Cfg.OpenaiApiKey)
return controller
}
return &Controller{
openaiClient: openai.NewClient(openaiApiKey),
unraidAPIKey: unraidAPIKey,
unraidURI: unraidURI,
ClientID: ClientID,
ClientSecret: ClientSecret,
RedirectURL: RedirectURL,
AuthURL: AuthURL,
TokenURL: TokenURL,
func (c *Controller) AuthMiddleware(
allowedGroups []string,
currentGroups string,
) gin.HandlerFunc {
return func(c *gin.Context) {
var groups []string
if currentGroups != "" {
groups = strings.Split(currentGroups, ",")
} else {
// Get the user groups from the request headers
groupsHeader := c.GetHeader("X-authentik-groups")
// Split the groups header value into individual groups
groups = strings.Split(groupsHeader, "|")
}
// Check if the user belongs to any of the allowed groups
isAllowed := false
for _, allowedGroup := range allowedGroups {
for _, group := range groups {
if group == allowedGroup {
isAllowed = true
break
}
}
if isAllowed {
break
}
}
// If the user is not in any of the allowed groups, respond with unauthorized access
if !isAllowed {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
"message": "Unauthorized access",
"groups": groups,
})
return
}
// Call the next handler
c.Next()
}
}

33
controller/dadjoke.go Normal file
View File

@@ -0,0 +1,33 @@
package controller
import (
"net/http"
"github.com/gin-gonic/gin"
"gitlab.com/DeveloperDurp/DurpAPI/model"
)
// GetDadJoke godoc
//
// @Summary Generate dadjoke
// @Description get a dad joke
// @Tags DadJoke
// @Accept json
// @Produce application/json
// @Success 200 {object} model.Message "response"
// @failure 400 {object} model.Message "error"
// @Router /jokes/dadjoke [get]
func (c *Controller) GetDadJoke(ctx *gin.Context) {
var req model.DadJoke
if err := ctx.ShouldBindJSON(&req); err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"message": err})
}
message := "test"
// message, err := service.GetDadJoke(req)
// if err != nil {
// ctx.JSON(http.StatusInternalServerError, gin.H{"message": err})
// }
ctx.JSON(http.StatusOK, gin.H{"message": message})
}

View File

@@ -17,7 +17,5 @@ import (
// @failure 400 {object} model.Message "error"
// @Router /health/getHealth [get]
func (c *Controller) GetHealth(ctx *gin.Context) {
// Return the health in the response body
ctx.JSON(http.StatusOK, gin.H{"message": "OK"})
ctx.JSON(http.StatusOK, gin.H{"message": "OK"})
}

View File

@@ -10,7 +10,7 @@ import (
)
type ChatRequest struct {
Message string `json:"message"`
Message string `json:"message"`
}
// GeneralOpenAI godoc
@@ -22,17 +22,18 @@ type ChatRequest struct {
// @Produce application/json
// @Param message query string true "Ask ChatGPT a general question"
// @Success 200 {object} model.Message "response"
//@failure 400 {object} model.Message "error"
//
// @failure 400 {object} model.Message "error"
//
// @Router /openai/general [get]
func (c *Controller) GeneralOpenAI(ctx *gin.Context) {
var req ChatRequest
var req ChatRequest
if err := ctx.ShouldBindJSON(&req); err != nil {
req.Message = ctx.Query("message")
}
if err := ctx.ShouldBindJSON(&req); err != nil {
req.Message = ctx.Query("message")
}
result, err := createChatCompletion(c, req.Message)
result, err := c.createChatCompletion(req.Message)
if err != nil {
err := ctx.AbortWithError(http.StatusInternalServerError, err)
if err != nil {
@@ -55,9 +56,14 @@ func (c *Controller) GeneralOpenAI(ctx *gin.Context) {
// @failure 400 {object} model.Message "error"
// @Router /openai/travelagent [get]
func (c *Controller) TravelAgentOpenAI(ctx *gin.Context) {
message := "I want you to act as a travel guide. I will give you my location and you will give me suggestions. " + ctx.Query("message")
var req ChatRequest
if err := ctx.ShouldBindJSON(&req); err != nil {
req.Message = ctx.Query("message")
}
result, err := createChatCompletion(c, message)
req.Message = "I want you to act as a travel guide. I will give you my location and you will give me suggestions. " + req.Message
result, err := c.createChatCompletion(req.Message)
if err != nil {
err := ctx.AbortWithError(http.StatusInternalServerError, err)
if err != nil {
@@ -68,9 +74,8 @@ func (c *Controller) TravelAgentOpenAI(ctx *gin.Context) {
ctx.JSON(http.StatusOK, gin.H{"message": result})
}
func createChatCompletion(c *Controller, message string) (string, error) {
var client = c.openaiClient
func (c *Controller) createChatCompletion(message string) (string, error) {
client := c.Cfg.OpenaiClient
resp, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{

View File

@@ -9,12 +9,6 @@ import (
"strings"
"github.com/gin-gonic/gin"
"gitlab.com/DeveloperDurp/DurpAPI/model"
)
var (
unraidAPIKey = model.UnraidAPIKey
UnraidURI = model.UnraidURI
)
// UnraidPowerUsage godoc
@@ -28,7 +22,6 @@ var (
// @failure 412 {object} model.Message "error"
// @Router /unraid/powerusage [get]
func (c *Controller) UnraidPowerUsage(ctx *gin.Context) {
jar, err := cookiejar.New(nil)
if err != nil {
fmt.Println(err)
@@ -41,10 +34,14 @@ func (c *Controller) UnraidPowerUsage(ctx *gin.Context) {
form := url.Values{
"username": {"root"},
"password": {unraidAPIKey},
"password": {c.Cfg.UnraidAPIKey},
}
req, err := http.NewRequest("POST", "https://"+UnraidURI+"/login", strings.NewReader(form.Encode()))
req, err := http.NewRequest(
"POST",
"https://"+c.Cfg.UnraidURI+"/login",
strings.NewReader(form.Encode()),
)
if err != nil {
fmt.Println(err)
return
@@ -62,7 +59,11 @@ func (c *Controller) UnraidPowerUsage(ctx *gin.Context) {
return
}
req, err = http.NewRequest("GET", "https://"+UnraidURI+"/plugins/corsairpsu/status.php", nil)
req, err = http.NewRequest(
"GET",
"https://"+c.Cfg.UnraidURI+"/plugins/corsairpsu/status.php",
nil,
)
if err != nil {
fmt.Println(err)
return