Moved to go 1.22 net/http

This commit is contained in:
2024-04-02 20:34:59 -05:00
parent 97b4c60096
commit 7c1a319b55
13 changed files with 239 additions and 446 deletions

View File

@@ -1,10 +1,9 @@
package controller
import (
"encoding/json"
"net/http"
"github.com/gin-gonic/gin"
"gitlab.com/DeveloperDurp/DurpAPI/model"
"gitlab.com/DeveloperDurp/DurpAPI/service"
)
@@ -19,14 +18,20 @@ import (
// @Success 200 {object} model.Message "response"
// @failure 500 {object} model.Message "error"
// @Router /jokes/dadjoke [get]
func (c *Controller) GetDadJoke(ctx *gin.Context) {
func (c *Controller) GetDadJoke(w http.ResponseWriter, r *http.Request) {
joke, err := service.GetRandomDadJoke(c.Db.DB)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"message": err})
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
return
}
ctx.JSON(http.StatusOK, gin.H{"message": joke})
message := model.Message{
Message: joke,
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(message)
}
// PostDadJoke godoc
@@ -40,19 +45,35 @@ func (c *Controller) GetDadJoke(ctx *gin.Context) {
// @Success 200 {object} model.Message "response"
// @failure 500 {object} model.Message "error"
// @Router /jokes/dadjoke [post]
func (c *Controller) PostDadJoke(ctx *gin.Context) {
func (c *Controller) PostDadJoke(w http.ResponseWriter, r *http.Request) {
contentType := r.Header.Get("Content-Type")
var req model.DadJoke
if err := ctx.ShouldBindJSON(&req); err != nil {
req.JOKE = ctx.Query("joke")
if contentType == "application/json" {
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
return
}
} else {
queryParams := r.URL.Query()
req.JOKE = queryParams.Get("joke")
}
err := service.PostDadJoke(c.Db.DB, req)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
return
}
ctx.JSON(http.StatusOK, gin.H{"message": "OK"})
message := model.Message{
Message: "OK",
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(message)
}
// DeleteDadJoke godoc
@@ -66,17 +87,33 @@ func (c *Controller) PostDadJoke(ctx *gin.Context) {
// @Success 200 {object} model.Message "response"
// @failure 500 {object} model.Message "error"
// @Router /jokes/dadjoke [delete]
func (c *Controller) DeleteDadJoke(ctx *gin.Context) {
func (c *Controller) DeleteDadJoke(w http.ResponseWriter, r *http.Request) {
contentType := r.Header.Get("Content-Type")
var req model.DadJoke
if err := ctx.ShouldBindJSON(&req); err != nil {
req.JOKE = ctx.Query("joke")
if contentType == "application/json" {
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
return
}
} else {
queryParams := r.URL.Query()
req.JOKE = queryParams.Get("joke")
}
err := service.DeleteDadJoke(c.Db.DB, req)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
return
}
ctx.JSON(http.StatusOK, gin.H{"message": "OK"})
message := model.Message{
Message: "OK",
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(message)
}

View File

@@ -1,9 +1,10 @@
package controller
import (
"encoding/json"
"net/http"
"github.com/gin-gonic/gin"
"gitlab.com/DeveloperDurp/DurpAPI/model"
)
// getHealth godoc
@@ -14,8 +15,16 @@ import (
// @Accept json
// @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) {
ctx.JSON(http.StatusOK, gin.H{"message": "OK"})
// @failure 500 {object} model.Message "error"
//
// @Security Authorization
//
// @Router /health/gethealth [get]
func (c *Controller) GetHealth(w http.ResponseWriter, r *http.Request) {
message := model.Message{
Message: "OK",
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(message)
}

View File

@@ -7,7 +7,7 @@ import (
"io"
"net/http"
"github.com/gin-gonic/gin"
"gitlab.com/DeveloperDurp/DurpAPI/model"
)
type ChatRequest struct {
@@ -32,22 +32,35 @@ type Response struct {
// @failure 400 {object} model.Message "error"
//
// @Router /openai/general [get]
func (c *Controller) GeneralOpenAI(ctx *gin.Context) {
func (c *Controller) GeneralOpenAI(w http.ResponseWriter, r *http.Request) {
contentType := r.Header.Get("Content-Type")
var req ChatRequest
if err := ctx.ShouldBindJSON(&req); err != nil {
req.Message = ctx.Query("message")
if contentType == "application/json" {
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
return
}
} else {
queryParams := r.URL.Query()
req.Message = queryParams.Get("message")
}
result, err := c.createChatCompletion(req.Message, "mistral:instruct")
if err != nil {
err := ctx.AbortWithError(http.StatusInternalServerError, err)
if err != nil {
fmt.Println("Failed to send message")
}
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
return
}
ctx.JSON(http.StatusOK, gin.H{"message": result})
message := model.Message{
Message: result,
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(message)
}
// TravelAgentOpenAI godoc
@@ -61,23 +74,37 @@ func (c *Controller) GeneralOpenAI(ctx *gin.Context) {
// @Success 200 {object} model.Message "response"
// @failure 400 {object} model.Message "error"
// @Router /openai/travelagent [get]
func (c *Controller) TravelAgentOpenAI(ctx *gin.Context) {
func (c *Controller) TravelAgentOpenAI(w http.ResponseWriter, r *http.Request) {
contentType := r.Header.Get("Content-Type")
var req ChatRequest
if err := ctx.ShouldBindJSON(&req); err != nil {
req.Message = ctx.Query("message")
if contentType == "application/json" {
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
return
}
} else {
queryParams := r.URL.Query()
req.Message = queryParams.Get("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, "openchat")
result, err := c.createChatCompletion(req.Message, "mistral:instruct")
if err != nil {
err := ctx.AbortWithError(http.StatusInternalServerError, err)
if err != nil {
fmt.Println("Failed to send message")
}
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
return
}
ctx.JSON(http.StatusOK, gin.H{"message": result})
message := model.Message{
Message: result,
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(message)
}
func (c *Controller) createChatCompletion(message string, model string) (string, error) {