Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
olevitt committed Mar 21, 2024
1 parent dfa37ab commit de63452
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 37 deletions.
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
docs
config.local
config.local
onyxia-api
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
docs
config.local
config.local
onyxia-api
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ RUN swag init
RUN ls docs

# Build
RUN CGO_ENABLED=0 GOOS=linux go build -o /onyxia-admin
RUN CGO_ENABLED=0 GOOS=linux go build -o /onyxia-api

FROM alpine

EXPOSE 8080
ENV GIN_MODE release
COPY --from=0 /onyxia-admin /bin/onyxia-admin
CMD ["/bin/onyxia-admin"]
COPY --from=0 /onyxia-api /bin/onyxia-api
CMD ["/bin/onyxia-api"]
4 changes: 2 additions & 2 deletions cmd/mylab-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"io"
"net/http"

"github.com/inseefrlab/onyxia-admin/internal/helm"
"github.com/inseefrlab/onyxia-admin/internal/kubernetes"
"github.com/inseefrlab/onyxia-api/internal/helm"
"github.com/inseefrlab/onyxia-api/internal/kubernetes"

"github.com/gin-gonic/gin"
eventsv1 "k8s.io/api/events/v1"
Expand Down
32 changes: 32 additions & 0 deletions cmd/public-handler.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
package cmd

import (
"fmt"
"net/http"

"runtime/debug"

"github.com/gin-gonic/gin"
"github.com/inseefrlab/onyxia-api/internal/configuration"
)

type BuildInfo struct {
Commit string `json:"commit,omitempty"`
CommitDate string `json:"commitDate,omitempty"`
}
type PublicConfiguration struct {
Build BuildInfo `json:"build"`
Regions interface{} `json:"regions"`
}

// PingExample godoc
// @Summary Get your public IP address.
// @Schemes
Expand All @@ -24,7 +37,26 @@ func healthcheck(c *gin.Context) {
c.Status(http.StatusOK)
}

func getConfiguration(c *gin.Context) {
info, _ := debug.ReadBuildInfo()
var buildInfo = BuildInfo{}
for _, kv := range info.Settings {
fmt.Println(kv.Key)
if kv.Key == "vcs.time" {
buildInfo.CommitDate = kv.Value
}
if kv.Key == "vcs.revision" {
buildInfo.Commit = kv.Value
}
}
c.JSON(http.StatusOK, PublicConfiguration{
Build: buildInfo,
Regions: configuration.Config.Regions,
})
}

func registerPublicHandlers(r *gin.RouterGroup) {
r.GET("/ip", ip)
r.GET("/healthcheck", healthcheck)
r.GET("/configuration", getConfiguration)
}
11 changes: 0 additions & 11 deletions configuration.go

This file was deleted.

8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/inseefrlab/onyxia-admin
module github.com/inseefrlab/onyxia-api

go 1.22.1

Expand All @@ -11,6 +11,9 @@ require (
github.com/swaggo/swag v1.16.3
go.uber.org/zap v1.27.0
helm.sh/helm/v3 v3.14.3
k8s.io/api v0.29.3
k8s.io/apimachinery v0.29.3
k8s.io/client-go v0.29.3
)

require (
Expand Down Expand Up @@ -163,12 +166,9 @@ require (
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.29.3 // indirect
k8s.io/apiextensions-apiserver v0.29.3 // indirect
k8s.io/apimachinery v0.29.3 // indirect
k8s.io/apiserver v0.29.3 // indirect
k8s.io/cli-runtime v0.29.3 // indirect
k8s.io/client-go v0.29.3 // indirect
k8s.io/component-base v0.29.3 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
Expand Down
3 changes: 2 additions & 1 deletion config.yaml → internal/configuration/config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
authentication:
issuerURI:
audience:
rootPath: /api
rootPath: /api
regions:
12 changes: 12 additions & 0 deletions internal/configuration/configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package configuration

type Configuration struct {
Authentication Authentication
RootPath string
Regions interface{}
}

type Authentication struct {
IssuerURI string
Audience string
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package configuration

import (
_ "embed"
Expand All @@ -9,9 +9,9 @@ import (

//go:embed config.yaml
var s string
var config configuration
var Config Configuration

func loadConfiguration() {
func LoadConfiguration() {
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.SetConfigType("yaml")
viper.ReadConfig(strings.NewReader(s)) // Reading defaults
Expand All @@ -23,7 +23,7 @@ func loadConfiguration() {
viper.AutomaticEnv()
viper.MergeInConfig()

err := viper.Unmarshal(&config)
err := viper.Unmarshal(&Config)
if err != nil {
panic(err)
}
Expand Down
25 changes: 15 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,43 @@ import (

oidc "github.com/coreos/go-oidc/v3/oidc"
"github.com/gin-gonic/gin"
cmd "github.com/inseefrlab/onyxia-admin/cmd"
_ "github.com/inseefrlab/onyxia-admin/docs"
"github.com/inseefrlab/onyxia-admin/internal/kubernetes"
cmd "github.com/inseefrlab/onyxia-api/cmd"
_ "github.com/inseefrlab/onyxia-api/docs"
configuration "github.com/inseefrlab/onyxia-api/internal/configuration"
"github.com/inseefrlab/onyxia-api/internal/kubernetes"
swaggerfiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"go.uber.org/zap"
)

var (
Version = 2
)

// gin-swagger middleware
// swagger embed files

func main() {
loadConfiguration()
configuration.LoadConfiguration()
r := gin.Default()
baseRoutes := r.Group(config.RootPath)
baseRoutes := r.Group(configuration.Config.RootPath)
baseRoutes.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
privateRoutes := baseRoutes.Group("/")
publicRoutes := baseRoutes.Group("/public")

zap.ReplaceGlobals(zap.Must(zap.NewProduction()))

if config.Authentication.IssuerURI != "" {
fmt.Printf("Using authentication with issuer %s", config.Authentication.IssuerURI)
if configuration.Config.Authentication.IssuerURI != "" {
fmt.Printf("Using authentication with issuer %s", configuration.Config.Authentication.IssuerURI)
fmt.Println()
client := &http.Client{
Timeout: time.Duration(6000) * time.Second,
}
ctx := oidc.ClientContext(context.Background(), client)
provider, _ := oidc.NewProvider(ctx, config.Authentication.IssuerURI)
provider, _ := oidc.NewProvider(ctx, configuration.Config.Authentication.IssuerURI)
oidcConfig := &oidc.Config{}
if config.Authentication.Audience != "" {
oidcConfig.ClientID = config.Authentication.Audience
if configuration.Config.Authentication.Audience != "" {
oidcConfig.ClientID = configuration.Config.Authentication.Audience
} else {
zap.L().Warn("Token audience validation disabled")
oidcConfig.SkipClientIDCheck = true
Expand Down

0 comments on commit de63452

Please sign in to comment.