Skip to content

Commit

Permalink
remove ssl
Browse files Browse the repository at this point in the history
  • Loading branch information
camw0 committed Jan 17, 2024
1 parent 7b68e12 commit 8da28b8
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 45 deletions.
13 changes: 0 additions & 13 deletions certs.go

This file was deleted.

14 changes: 0 additions & 14 deletions cors.go

This file was deleted.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.16.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MG
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
Expand Down
11 changes: 1 addition & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,8 @@ func main() {
}

r := setupRouter(config)
sslCertPath, sslKeyPath := getSSLCertPaths(config.CertDir, "./certs/")

if sslCertPath == "" || sslKeyPath == "" {
log.Println("Using HTTP (SSL certs do not exist)")
} else {
log.Println("Using HTTPS (certs found at path)")
}

SetupCORS(r, config.RemoteAddr)

startServer(config.ListenAddr, r, sslCertPath, sslKeyPath)
startServer(config.ListenAddr, r)

log.Printf("Started - Listening on %s for requests\n", config.ListenAddr)
}
44 changes: 36 additions & 8 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"net/http"
"time"

"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/mem"
Expand All @@ -16,10 +18,34 @@ import (
func setupRouter(config *Config) *gin.Engine {
r := gin.Default()

r.Use(cors.New(cors.Config{
AllowOrigins: []string{"http://localhost"},
AllowMethods: []string{"GET"},
AllowHeaders: []string{"Origin"},
ExposeHeaders: []string{"Content-Length"},
MaxAge: 12 * time.Hour,
}))

r.GET("/", func(c *gin.Context) {
log.Println("Serving request at root")

c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Headers", "Content-Type,access-control-allow-origin, access-control-allow-headers")

uuid := uuid.New()
startTime := time.Now()

cpuInfo, err := cpu.Info()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get CPU info"})
return
}

cpuTimes, err := cpu.Times(true)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get CPU times"})
return
}

cpuPercent, err := cpu.Percent(0, false)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get CPU usage"})
Expand All @@ -45,7 +71,13 @@ func setupRouter(config *Config) *gin.Engine {
}

response := gin.H{
"cpu": cpuPercent[0],
"version": "v2.0.0",
"uuid": uuid,
"cpu": gin.H{
"percent": cpuPercent[0],
"times": cpuTimes,
"info": cpuInfo,
},
"memory": gin.H{
"total": memInfo.Total,
"used": memInfo.Used,
Expand Down Expand Up @@ -83,10 +115,6 @@ func setupRouter(config *Config) *gin.Engine {
}

// startServer starts the HTTP server
func startServer(addr string, r *gin.Engine, certPath, keyPath string) {
if certPath != "" && keyPath != "" {
log.Fatal(http.ListenAndServeTLS(addr, certPath, keyPath, r))
} else {
log.Fatal(http.ListenAndServe(addr, r))
}
func startServer(addr string, r *gin.Engine) {
log.Fatal(http.ListenAndServe(addr, r))
}

0 comments on commit 8da28b8

Please sign in to comment.