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: ports:
- name: http - name: http
containerPort: {{ .Values.service.targetport }} containerPort: {{ .Values.service.targetport }}
env:
- name: host
value: {{ .Values.ingress.host }}
- name: version
value: {{ default .Chart.Version .Values.deployment.tag }}
envFrom: envFrom:
- secretRef: - secretRef:
name: {{ .Values.deployment.secretfile }} name: {{ .Values.deployment.secretfile }}

View File

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

View File

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

View File

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

View File

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

1
go.mod
View File

@@ -11,6 +11,7 @@ require (
) )
require ( require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/golang/protobuf v1.5.2 // indirect github.com/golang/protobuf v1.5.2 // indirect
google.golang.org/appengine v1.6.7 // 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.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 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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/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 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= 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" swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger" ginSwagger "github.com/swaggo/gin-swagger"
"gitlab.com/DeveloperDurp/DurpAPI/controller" "gitlab.com/DeveloperDurp/DurpAPI/controller"
_ "gitlab.com/DeveloperDurp/DurpAPI/docs" "gitlab.com/DeveloperDurp/DurpAPI/docs"
"gitlab.com/DeveloperDurp/DurpAPI/model"
"golang.org/x/oauth2" "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 // @title DurpAPI
// @version 1.0
// @description API for Durp's needs // @description API for Durp's needs
// @termsOfService http://swagger.io/terms/ // @termsOfService http://swagger.io/terms/
@@ -25,7 +43,6 @@ import (
// @license.name Apache 2.0 // @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html // @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host durpapi.durp.info
// @BasePath /api/v1 // @BasePath /api/v1
// @securityDefinitions.apikey ApiKeyAuth // @securityDefinitions.apikey ApiKeyAuth
@@ -36,20 +53,8 @@ func main() {
r := gin.Default() r := gin.Default()
c := controller.NewController() c := controller.NewController()
var groups []string docs.SwaggerInfo.Host = host
conf := &oauth2.Config{ docs.SwaggerInfo.Version = version
ClientID: c.ClientID,
ClientSecret: c.ClientSecret,
RedirectURL: c.RedirectURL,
Scopes: []string{
"email",
"groups",
},
Endpoint: oauth2.Endpoint{
AuthURL: c.AuthURL,
TokenURL: c.TokenURL,
},
}
v1 := r.Group("/api/v1") v1 := r.Group("/api/v1")
{ {
@@ -59,24 +64,24 @@ func main() {
} }
token := v1.Group("/token") token := v1.Group("/token")
{ {
token.GET("GenerateToken", c.GenerateToken(conf)) token.GET("GenerateToken", c.GenerateToken(Conf))
} }
openai := v1.Group("/openai") openai := v1.Group("/openai")
{ {
groups = []string{"openai"} //groups = []string{"openai"}
openai.Use(authMiddleware(groups)) //openai.Use(authMiddleware())
openai.GET("general", c.GeneralOpenAI) openai.GET("general", c.GeneralOpenAI)
openai.GET("travelagent", c.TravelAgentOpenAI) openai.GET("travelagent", c.TravelAgentOpenAI)
} }
unraid := v1.Group("/unraid") unraid := v1.Group("/unraid")
{ {
groups = []string{"unraid"} //groups = []string{"unraid"}
unraid.Use(authMiddleware(groups)) //unraid.Use(authMiddleware())
unraid.GET("powerusage", c.UnraidPowerUsage) unraid.GET("powerusage", c.UnraidPowerUsage)
} }
} }
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
r.GET("/callback", CallbackHandler(conf)) r.GET("/callback", CallbackHandler(Conf))
err := r.Run(":8080") err := r.Run(":8080")
if err != nil { 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 // CallbackHandler receives the authorization code and exchanges it for a token
func CallbackHandler(conf *oauth2.Config) gin.HandlerFunc { func CallbackHandler(conf *oauth2.Config) gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {

View File

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