updates
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,3 +2,4 @@ __debug_bin
|
||||
docs
|
||||
.vscode
|
||||
.env
|
||||
.idea
|
||||
@@ -1,15 +1,38 @@
|
||||
package controller
|
||||
|
||||
// Controller example
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
openai "github.com/sashabaranov/go-openai"
|
||||
)
|
||||
|
||||
type Controller struct {
|
||||
openaiClient *openai.Client
|
||||
unraidAPIKey string
|
||||
unraidURI string
|
||||
}
|
||||
|
||||
// NewController example
|
||||
func NewController() *Controller {
|
||||
return &Controller{}
|
||||
err := godotenv.Load(".env")
|
||||
|
||||
openaiApiKey := os.Getenv("OPENAI_API_KEY")
|
||||
openaiClient := openai.NewClient(openaiApiKey)
|
||||
unraidAPIKey := os.Getenv("UNRAID_API_KEY")
|
||||
UNRAID_URI := os.Getenv("UNRAID_URI")
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
//return err
|
||||
}
|
||||
return &Controller{
|
||||
openaiClient: openaiClient,
|
||||
unraidAPIKey: unraidAPIKey,
|
||||
unraidURI: UNRAID_URI,
|
||||
}
|
||||
}
|
||||
|
||||
// Message example
|
||||
type Message struct {
|
||||
Message string `json:"message" example:"message"`
|
||||
}
|
||||
|
||||
@@ -2,38 +2,12 @@ package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/joho/godotenv"
|
||||
openai "github.com/sashabaranov/go-openai"
|
||||
)
|
||||
|
||||
var (
|
||||
// Declare global variables
|
||||
apiKey string
|
||||
|
||||
config *configStruct
|
||||
)
|
||||
|
||||
type configStruct struct {
|
||||
apiKey string `json : "API_KEY"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
// Load values for global variables from environment variables
|
||||
err := godotenv.Load(".env")
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
//return err
|
||||
}
|
||||
apiKey = os.Getenv("API_KEY")
|
||||
|
||||
}
|
||||
|
||||
// GeneralOpenAI godoc
|
||||
//
|
||||
// @Summary Gerneral ChatGPT
|
||||
@@ -47,7 +21,7 @@ func init() {
|
||||
func (c *Controller) GeneralOpenAI(ctx *gin.Context) {
|
||||
message := ctx.Query("message")
|
||||
|
||||
result, err := createChatCompletion(message)
|
||||
result, err := createChatCompletion(c, message)
|
||||
if err != nil {
|
||||
ctx.AbortWithError(http.StatusInternalServerError, err)
|
||||
}
|
||||
@@ -68,7 +42,7 @@ func (c *Controller) GeneralOpenAI(ctx *gin.Context) {
|
||||
func (c *Controller) TravelAgentOpenAI(ctx *gin.Context) {
|
||||
message := "want you to act as a travel guide. I will give you my location and you will give me suggestions " + ctx.Query("message")
|
||||
|
||||
result, err := createChatCompletion(message)
|
||||
result, err := createChatCompletion(c, message)
|
||||
if err != nil {
|
||||
ctx.AbortWithError(http.StatusInternalServerError, err)
|
||||
}
|
||||
@@ -76,9 +50,9 @@ func (c *Controller) TravelAgentOpenAI(ctx *gin.Context) {
|
||||
ctx.String(http.StatusOK, result)
|
||||
}
|
||||
|
||||
func createChatCompletion(message string) (string, error) {
|
||||
func createChatCompletion(c *Controller, message string) (string, error) {
|
||||
|
||||
var client = openai.NewClient(apiKey)
|
||||
var client = c.openaiClient
|
||||
resp, err := client.CreateChatCompletion(
|
||||
context.Background(),
|
||||
openai.ChatCompletionRequest{
|
||||
|
||||
85
controller/unraid.go
Normal file
85
controller/unraid.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// UnraidPowerUsage godoc
|
||||
//
|
||||
// @Summary Unraid PSU Stats
|
||||
// @Description Gets the PSU Data from unraid
|
||||
// @Tags unraid
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {string} string "response"
|
||||
// @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},
|
||||
}
|
||||
|
||||
// Login to unraid
|
||||
req, err := http.NewRequest("POST", "https://"+c.unraidURI+"/login", strings.NewReader(form.Encode()))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
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)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
resp, err = client.Do(req)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, responseJSON)
|
||||
}
|
||||
6
main.go
6
main.go
@@ -13,7 +13,7 @@ import (
|
||||
ginSwagger "github.com/swaggo/gin-swagger"
|
||||
)
|
||||
|
||||
// @title Swagger Example API
|
||||
// @title DurpAPI
|
||||
// @version 1.0
|
||||
// @description This is a sample server celler server.
|
||||
// @termsOfService http://swagger.io/terms/
|
||||
@@ -69,6 +69,10 @@ func main() {
|
||||
openai.GET("general", c.GeneralOpenAI)
|
||||
openai.GET("travelagent", c.TravelAgentOpenAI)
|
||||
}
|
||||
unraid := v1.Group("/unraid")
|
||||
{
|
||||
unraid.GET("powerusage", c.UnraidPowerUsage)
|
||||
}
|
||||
}
|
||||
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
||||
r.Run(":8080")
|
||||
|
||||
Reference in New Issue
Block a user