This commit is contained in:
2023-06-11 12:40:53 -04:00
parent f9b6e2136b
commit 7fc4336397
14 changed files with 393 additions and 201 deletions

View File

@@ -39,7 +39,3 @@ func NewController() *Controller {
TokenURL: TokenURL,
}
}
type Message struct {
Message string `json:"message" example:"message"`
}

View File

@@ -12,8 +12,9 @@ import (
// @Description Get the health of the API
// @Tags health
// @Accept json
// @Produce json
// @Success 200 {string} json "response"
// @Produce application/json
// @Success 200 {object} model.Message "response"
// @failure 400 {object} model.Message "error"
// @Router /health/getHealth [get]
func (c *Controller) GetHealth(ctx *gin.Context) {
// Return the health in the response body

View File

@@ -15,9 +15,10 @@ import (
// @Description Ask ChatGPT a general question
// @Tags openai
// @Accept json
// @Produce plain
// @Produce application/json
// @Param message query string true "Ask ChatGPT a general question"
// @Success 200 {string} string "response"
// @Success 200 {object} model.Message "response"
// @failure 400 {object} model.Message "error"
// @Router /openai/general [get]
func (c *Controller) GeneralOpenAI(ctx *gin.Context) {
message := ctx.Query("message")
@@ -39,9 +40,10 @@ func (c *Controller) GeneralOpenAI(ctx *gin.Context) {
// @Description Ask ChatGPT for suggestions as if it was a travel agent
// @Tags openai
// @Accept json
// @Produce plain
// @Produce application/json
// @Param message query string true "Ask ChatGPT for suggestions as a travel agent"
// @Success 200 {string} string "response"
// @Success 200 {object} model.Message "response"
// @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")

View File

@@ -1,25 +0,0 @@
package controller
import (
"net/http"
"github.com/gin-gonic/gin"
"golang.org/x/oauth2"
)
// GenerateToken godoc
//
// @Summary Generate Health status
// @Description Get the health of the API
// @Tags token
// @Accept json
// @Produce json
// @Success 200 {string} json "response"
// @Router /token/GenerateToken [get]
func (c *Controller) GenerateToken(conf *oauth2.Config) gin.HandlerFunc {
return func(c *gin.Context) {
// Redirect user to the authorization URL
authURL := conf.AuthCodeURL("state", oauth2.AccessTypeOffline)
c.Redirect(http.StatusTemporaryRedirect, authURL)
}
}

View File

@@ -3,13 +3,18 @@ package controller
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"github.com/gin-gonic/gin"
"gitlab.com/DeveloperDurp/DurpAPI/model"
)
var (
unraidAPIKey = model.UnraidAPIKey
UnraidURI = model.UnraidURI
)
// UnraidPowerUsage godoc
@@ -19,29 +24,27 @@ import (
// @Tags unraid
// @Accept json
// @Produce json
// @Success 200 {string} string "response"
// @Success 200 {object} model.PowerSupply "response"
// @failure 412 {object} model.Message "error"
// @Router /unraid/powerusage [get]
func (c *Controller) UnraidPowerUsage(ctx *gin.Context) {
// Create a cookie jar to hold cookies for the session
jar, err := cookiejar.New(nil)
if err != nil {
fmt.Println(err)
return
}
// Create an HTTP client with the cookie jar
client := &http.Client{
Jar: jar,
}
form := url.Values{
"username": {"root"},
"password": {c.unraidAPIKey},
"password": {unraidAPIKey},
}
// Login to unraid
req, err := http.NewRequest("POST", "https://"+c.unraidURI+"/login", strings.NewReader(form.Encode()))
req, err := http.NewRequest("POST", "https://"+UnraidURI+"/login", strings.NewReader(form.Encode()))
if err != nil {
fmt.Println(err)
return
@@ -54,14 +57,12 @@ func (c *Controller) UnraidPowerUsage(ctx *gin.Context) {
}
defer resp.Body.Close()
// Check if the login was successful by inspecting the response body or headers
if resp.StatusCode != http.StatusOK {
fmt.Println("Login failed!")
return
}
// Now you can use the client to send authenticated requests to other endpoints
req, err = http.NewRequest("GET", "https://"+c.unraidURI+"/plugins/corsairpsu/status.php", nil)
req, err = http.NewRequest("GET", "https://"+UnraidURI+"/plugins/corsairpsu/status.php", nil)
if err != nil {
fmt.Println(err)
return
@@ -75,10 +76,11 @@ func (c *Controller) UnraidPowerUsage(ctx *gin.Context) {
defer resp.Body.Close()
// Convert the returned data to JSON
var responseJSON map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&responseJSON); err != nil {
log.Fatal(err)
fmt.Println(err)
ctx.JSON(http.StatusPreconditionFailed, gin.H{"message": "Bad Response from Unraid"})
return
}
ctx.JSON(http.StatusOK, responseJSON)