Skip to content

Commit

Permalink
Feature/skelton (#2)
Browse files Browse the repository at this point in the history
* Fix #1
  • Loading branch information
Clivern committed Aug 3, 2020
1 parent 718f6e2 commit 493a7ac
Show file tree
Hide file tree
Showing 22 changed files with 592 additions and 107 deletions.
Binary file removed assets/gopher3.png
Binary file not shown.
172 changes: 169 additions & 3 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,186 @@
package cmd

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/clivern/penguin/core/controller"
"github.com/clivern/penguin/core/middleware"
"github.com/clivern/penguin/core/util"

"github.com/drone/envsubst"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var config string

var runCmd = &cobra.Command{
Use: "run",
Short: "Run statistics listener",
Short: "Run penguin server",
Run: func(cmd *cobra.Command, args []string) {
if Verbose {
log.SetLevel(log.DebugLevel)
var runerr error

configUnparsed, err := ioutil.ReadFile(config)

if err != nil {
panic(fmt.Sprintf(
"Error while reading config file [%s]: %s",
config,
err.Error(),
))
}

configParsed, err := envsubst.EvalEnv(string(configUnparsed))

if err != nil {
panic(fmt.Sprintf(
"Error while parsing config file [%s]: %s",
config,
err.Error(),
))
}

viper.SetConfigType("yaml")
err = viper.ReadConfig(bytes.NewBuffer([]byte(configParsed)))

if err != nil {
panic(fmt.Sprintf(
"Error while loading configs [%s]: %s",
config,
err.Error(),
))
}

if viper.GetString("log.output") != "stdout" {
dir, _ := filepath.Split(viper.GetString("log.output"))

if !util.DirExists(dir) {
if _, err := util.EnsureDir(dir, 777); err != nil {
panic(fmt.Sprintf(
"Directory [%s] creation failed with error: %s",
dir,
err.Error(),
))
}
}

if !util.FileExists(viper.GetString("log.output")) {
f, err := os.Create(viper.GetString("log.output"))
if err != nil {
panic(fmt.Sprintf(
"Error while creating log file [%s]: %s",
viper.GetString("log.output"),
err.Error(),
))
}
defer f.Close()
}
}

if viper.GetString("log.output") == "stdout" {
gin.DefaultWriter = os.Stdout
log.SetOutput(os.Stdout)
} else {
f, _ := os.Create(viper.GetString("log.output"))
gin.DefaultWriter = io.MultiWriter(f)
log.SetOutput(f)
}

lvl := strings.ToLower(viper.GetString("log.level"))
level, err := log.ParseLevel(lvl)

if err != nil {
level = log.InfoLevel
}

log.SetLevel(level)

if viper.GetString("app.mode") == "prod" {
gin.SetMode(gin.ReleaseMode)
gin.DefaultWriter = ioutil.Discard
gin.DisableConsoleColor()
}

if viper.GetString("log.format") == "json" {
log.SetFormatter(&log.JSONFormatter{})
} else {
log.SetFormatter(&log.TextFormatter{})
}

messages := make(chan string, 5000)

go controller.Daemon(messages)

if !viper.GetBool("inputs.http.enabled") {
controller.Watcher(messages)
return
} else {
go controller.Watcher(messages)
}

r := gin.Default()

r.Use(middleware.Correlation())
r.Use(middleware.Auth())
r.Use(middleware.Logger())

if viper.GetBool("output.prometheus.enabled") {
r.Use(middleware.Metric())
}

r.GET("/favicon.ico", func(c *gin.Context) {
c.String(http.StatusNoContent, "")
})

r.GET("/_health", controller.HealthCheck)

r.POST(viper.GetString("inputs.http.configs.path"), func(c *gin.Context) {
controller.Listener(c, messages)
})

if viper.GetBool("output.prometheus.enabled") {
r.GET(
viper.GetString("output.prometheus.endpoint"),
gin.WrapH(controller.Metrics()),
)
}

if viper.GetBool("inputs.http.configs.tls.status") {
runerr = r.RunTLS(
fmt.Sprintf(":%s", strconv.Itoa(viper.GetInt("inputs.http.configs.port"))),
viper.GetString("inputs.http.configs.tls.pemPath"),
viper.GetString("inputs.http.configs.tls.keyPath"),
)
} else {
runerr = r.Run(
fmt.Sprintf(":%s", strconv.Itoa(viper.GetInt("inputs.http.configs.port"))),
)
}

if runerr != nil {
panic(runerr.Error())
}
},
}

func init() {
runCmd.Flags().StringVarP(
&config,
"config",
"c",
"config.prod.yml",
"Absolute path to config file (required)",
)
runCmd.MarkFlagRequired("config")
rootCmd.AddCommand(runCmd)
}
24 changes: 0 additions & 24 deletions cmd/watch.go

This file was deleted.

46 changes: 46 additions & 0 deletions config.dist.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Metrics Input
inputs:
http:
enabled: on
configs:
mode: dev
port: 8080
tls:
status: off
pemPath: cert/server.pem
keyPath: cert/server.key
path: /
api_key: ""

log:
enabled: on
paths:
- /var/log/*.log

# Metrics Cache Driver
cache:
type: memory

# Metrics Output
output:
console:
enabled: on

prometheus:
enabled: on
endpoint: /metrics

graphite:
enabled: off

penguin:
enabled: off

# Log configs
log:
# Log level, it can be debug, info, warn, error, panic, fatal
level: info
# output can be stdout or abs path to log file /var/logs/beetle.log
output: stdout
# Format can be json
format: json
5 changes: 5 additions & 0 deletions core/backend/graphite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright 2020 Clivern. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

package backend
5 changes: 5 additions & 0 deletions core/backend/penguin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright 2020 Clivern. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

package backend
5 changes: 5 additions & 0 deletions core/backend/prometheus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright 2020 Clivern. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

package backend
16 changes: 16 additions & 0 deletions core/controller/daemon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2020 Clivern. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

package controller

import (
log "github.com/sirupsen/logrus"
)

// Daemon function
func Daemon(messages <-chan string) {
for message := range messages {
log.Info(message)
}
}
26 changes: 26 additions & 0 deletions core/controller/health_check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2020 Clivern. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

package controller

import (
"net/http"

"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)

// HealthCheck controller
func HealthCheck(c *gin.Context) {
status := "ok"

log.WithFields(log.Fields{
"correlation_id": c.Request.Header.Get("X-Correlation-ID"),
"status": status,
}).Info(`Health check`)

c.JSON(http.StatusOK, gin.H{
"status": status,
})
}
19 changes: 19 additions & 0 deletions core/controller/listener.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2020 Clivern. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

package controller

import (
"net/http"

"github.com/gin-gonic/gin"
)

// Listener controller
func Listener(c *gin.Context, messages chan<- string) {
messages <- "wip1"

c.Status(http.StatusAccepted)
return
}
16 changes: 16 additions & 0 deletions core/controller/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2020 Clivern. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

package controller

import (
"net/http"

"github.com/prometheus/client_golang/prometheus/promhttp"
)

// Metrics controller
func Metrics() http.Handler {
return promhttp.Handler()
}
17 changes: 17 additions & 0 deletions core/controller/watcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2020 Clivern. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

package controller

import (
"time"
)

// Watcher function
func Watcher(messages chan<- string) {
for {
messages <- "wip2"
time.Sleep(1 * time.Second)
}
}
Loading

0 comments on commit 493a7ac

Please sign in to comment.