This commit is contained in:
2023-06-04 15:18:17 -04:00
parent 2a54b5eb65
commit 4cf5cf352f
9 changed files with 55 additions and 64 deletions

View File

@@ -34,6 +34,11 @@ spec:
ports:
- name: http
containerPort: {{ .Values.service.targetport }}
env:
- name: host
value: {{ .Values.ingress.host }}
- name: version
value: {{ default .Chart.Version .Values.deployment.tag }}
envFrom:
- secretRef:
name: {{ .Values.deployment.secretfile }}

View File

@@ -1,7 +1,7 @@
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: {{ .Chart.Name }}-ingress
name: "{{ .Chart.Name }}-ingress"
spec:
entryPoints:
- websecure
@@ -9,7 +9,7 @@ spec:
- match: Host("{{ .Values.ingress.host }}") && PathPrefix(`/`)
kind: Rule
services:
- name: {{ .Chart.Name }}-service
- name: "{{ .Chart.Name }}-service"
port: {{ .Values.service.port }}
tls:
secretName: {{ .Values.ingress.tls }}

View File

@@ -170,8 +170,8 @@ const docTemplate = `{
// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = &swag.Spec{
Version: "1.0",
Host: "durpapi.durp.info",
Version: "",
Host: "",
BasePath: "/api/v1",
Schemes: []string{},
Title: "DurpAPI",

View File

@@ -12,10 +12,8 @@
"license": {
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
},
"version": "1.0"
}
},
"host": "durpapi.durp.info",
"basePath": "/api/v1",
"paths": {
"/health/getHealth": {

View File

@@ -1,5 +1,4 @@
basePath: /api/v1
host: durpapi.durp.info
info:
contact:
email: support@swagger.io
@@ -11,7 +10,6 @@ info:
url: http://www.apache.org/licenses/LICENSE-2.0.html
termsOfService: http://swagger.io/terms/
title: DurpAPI
version: "1.0"
paths:
/health/getHealth:
get:

1
go.mod
View File

@@ -11,6 +11,7 @@ require (
)
require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/golang/protobuf v1.5.2 // indirect
google.golang.org/appengine v1.6.7 // indirect
)

2
go.sum
View File

@@ -14,6 +14,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/gin-contrib/gzip v0.0.6 h1:NjcunTcGAj5CO1gn4N8jHOSIeRFHIbn51z6K+xaN4d4=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=

78
main.go
View File

@@ -9,12 +9,30 @@ import (
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"gitlab.com/DeveloperDurp/DurpAPI/controller"
_ "gitlab.com/DeveloperDurp/DurpAPI/docs"
"gitlab.com/DeveloperDurp/DurpAPI/docs"
"gitlab.com/DeveloperDurp/DurpAPI/model"
"golang.org/x/oauth2"
)
var (
host = model.Host
version = model.Version
Conf = &oauth2.Config{
ClientID: model.ClientID,
ClientSecret: model.ClientSecret,
RedirectURL: model.RedirectURL,
Scopes: []string{
"email",
"groups",
},
Endpoint: oauth2.Endpoint{
AuthURL: model.AuthURL,
TokenURL: model.TokenURL,
},
}
)
// @title DurpAPI
// @version 1.0
// @description API for Durp's needs
// @termsOfService http://swagger.io/terms/
@@ -25,7 +43,6 @@ import (
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host durpapi.durp.info
// @BasePath /api/v1
// @securityDefinitions.apikey ApiKeyAuth
@@ -36,20 +53,8 @@ func main() {
r := gin.Default()
c := controller.NewController()
var groups []string
conf := &oauth2.Config{
ClientID: c.ClientID,
ClientSecret: c.ClientSecret,
RedirectURL: c.RedirectURL,
Scopes: []string{
"email",
"groups",
},
Endpoint: oauth2.Endpoint{
AuthURL: c.AuthURL,
TokenURL: c.TokenURL,
},
}
docs.SwaggerInfo.Host = host
docs.SwaggerInfo.Version = version
v1 := r.Group("/api/v1")
{
@@ -59,24 +64,24 @@ func main() {
}
token := v1.Group("/token")
{
token.GET("GenerateToken", c.GenerateToken(conf))
token.GET("GenerateToken", c.GenerateToken(Conf))
}
openai := v1.Group("/openai")
{
groups = []string{"openai"}
openai.Use(authMiddleware(groups))
//groups = []string{"openai"}
//openai.Use(authMiddleware())
openai.GET("general", c.GeneralOpenAI)
openai.GET("travelagent", c.TravelAgentOpenAI)
}
unraid := v1.Group("/unraid")
{
groups = []string{"unraid"}
unraid.Use(authMiddleware(groups))
//groups = []string{"unraid"}
//unraid.Use(authMiddleware())
unraid.GET("powerusage", c.UnraidPowerUsage)
}
}
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
r.GET("/callback", CallbackHandler(conf))
r.GET("/callback", CallbackHandler(Conf))
err := r.Run(":8080")
if err != nil {
@@ -84,33 +89,6 @@ func main() {
}
}
func authMiddleware(groups []string) gin.HandlerFunc {
return func(c *gin.Context) {
// Get the access token from the request header or query parameters
accessToken := c.GetHeader("Authorization")
if accessToken == "" {
accessToken = c.Query("access_token")
}
// Create an OAuth2 token from the access token
token := &oauth2.Token{AccessToken: accessToken}
// Validate the token
if !token.Valid() {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
return
}
// Add the token to the request context for later use
//ctx := context.WithValue(c.Request.Context(), "token", token)
//c.Request = c.Request.WithContext(ctx)
// Call the next handler
c.Next()
}
}
// CallbackHandler receives the authorization code and exchanges it for a token
func CallbackHandler(conf *oauth2.Config) gin.HandlerFunc {
return func(c *gin.Context) {

View File

@@ -1,7 +1,16 @@
package model
// Admin example
type Admin struct {
ID int `json:"id" example:"1"`
Name string `json:"name" example:"admin name"`
}
import "os"
var (
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")
Host = os.Getenv("host")
Version = os.Getenv("version")
)