Skip to content
Snippets Groups Projects
Commit fef8678c authored by tgs266's avatar tgs266
Browse files

add new dependencies for common

parent c990fb74
No related branches found
No related tags found
No related merge requests found
package common
import (
"log"
"os"
"github.com/spf13/viper"
)
func GetConfig(configName string) {
viper.SetConfigName("local")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
viper.AddConfigPath("./config/")
viper.AutomaticEnv()
err := viper.ReadInConfig()
if err != nil {
log.Fatalln("Env local not found")
os.Exit(1)
}
viper.SetConfigName(configName)
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
viper.AddConfigPath("./config/")
viper.AutomaticEnv()
err = viper.MergeInConfig()
if err != nil {
log.Fatalln("Env " + configName + " not found")
os.Exit(1)
}
}
package common
import (
"github.com/gofiber/fiber/v2"
)
type DawnCtx struct {
FiberCtx *fiber.Ctx
}
func (ctx DawnCtx) INFO(message string) {
INFO(ctx.FiberCtx, message)
}
func (ctx DawnCtx) DEBUG(message string) {
DEBUG(ctx.FiberCtx, message)
}
func (ctx DawnCtx) TRACE(message string) {
TRACE(ctx.FiberCtx, message)
}
func BuildCtx(c *fiber.Ctx) DawnCtx {
return DawnCtx{
FiberCtx: c,
}
}
func (ctx DawnCtx) BodyParser(out interface{}) error {
return ctx.FiberCtx.BodyParser(out)
}
package common
import (
"encoding/json"
"fmt"
"os"
"strconv"
"github.com/gofiber/fiber/v2"
"github.com/spf13/viper"
)
type BaseError interface {
Error() string
}
type DawnError struct {
Name string `json:"name"`
Description string `json:"description"`
LogDetails string `json:"log_details"`
Code int `json:"code"`
Details map[string]string `json:"details"`
}
type StandardError struct {
Source string `json:"source"`
ErrorCode string `json:"errorCode"`
Description string `json:"description"`
Details map[string]string `json:"details"`
}
func (err *DawnError) Error() string {
str := err.Name + ": " + err.Description
if err.LogDetails != "" {
str += " - " + err.LogDetails
}
return str
}
func (err *DawnError) BuildStandardError(ctx *fiber.Ctx) StandardError {
requestId := ctx.Locals("requestId")
details := map[string]string{"RequestId": fmt.Sprintf("%s", requestId)}
for k, v := range err.Details {
details[k] = v
}
return StandardError{Source: viper.GetString("app.name"), ErrorCode: err.Name, Description: err.Description, Details: details}
}
func (err *DawnError) AddLogDetails(logDetails string) *DawnError {
err.LogDetails = logDetails
return err
}
func (err *DawnError) PutDetail(key string, value string) *DawnError {
err.Details[key] = value
return err
}
func Build(err error) *DawnError {
return &DawnError{
Name: "INTERNAL_SERVER_ERROR",
Description: err.Error(),
Code: 500,
}
}
func (err *DawnError) LogJson(c *fiber.Ctx) {
jsonErrBytes, _ := json.Marshal(err)
fmt.Println(string(jsonErrBytes))
}
func (err *DawnError) LogString(c *fiber.Ctx) {
requestId := c.Locals("requestId")
output := strconv.Itoa(os.Getpid()) + " " + fmt.Sprintf("%s", requestId) + " " + strconv.Itoa(err.Code) + " - " + c.Method() + " " + c.Route().Path + " - " + err.Error()
if err.LogDetails != "" {
output += " - " + err.LogDetails
}
fmt.Println(output)
}
package common
import (
"encoding/json"
"fmt"
"os"
"strconv"
"time"
"github.com/gofiber/fiber/v2"
"github.com/spf13/viper"
"github.com/valyala/fasthttp"
)
type RequestLog struct {
Date string
PID string
Level string
RequestId string
Error *DawnError
StatusCode string
Method string
Path string
}
type Request struct {
Headers fasthttp.RequestHeader
}
type MessageLog struct {
Date string
Level string
PID string
RequestId string
Message string
}
var LEVEL_FORMAT_STRING string = "%-5s"
func buildMessageLog(c *fiber.Ctx, message string) MessageLog {
const layout = "2006-01-02 03:04:05"
requestId := c.Locals("requestId")
messageLog := MessageLog{
Date: time.Now().UTC().Format(layout),
RequestId: fmt.Sprintf("%s", requestId),
PID: strconv.Itoa(os.Getpid()),
Message: message,
}
return messageLog
}
func cleanRequest(c *fiber.Ctx, r *fasthttp.Request) Request {
headers := fasthttp.AcquireRequest().Header
r.Header.CopyTo(&headers)
fmt.Println(headers.String())
return Request{
Headers: headers,
}
}
func BuildMessage(c *fiber.Ctx) RequestLog {
const layout = "2006-01-02 03:04:05"
requestId := c.Locals("requestId")
message := RequestLog{
Date: time.Now().UTC().Format(layout),
RequestId: fmt.Sprintf("%s", requestId),
Level: "INFO",
StatusCode: strconv.Itoa(c.Response().StatusCode()),
Method: c.Method(),
Path: c.Path(),
PID: strconv.Itoa(os.Getpid()),
}
return message
}
func LogRequest(message RequestLog) {
logString := ""
if message.Error != nil {
message.Level = "ERROR"
}
if viper.GetString("app.logType") == "json" {
tempLogString, _ := json.MarshalIndent(message, "", " ")
logString = string(tempLogString)
} else {
logString = fmt.Sprintf("[%s] %s %s %s %s - %s %s", fmt.Sprintf(LEVEL_FORMAT_STRING, message.Level), message.Date, message.PID, message.RequestId, message.StatusCode, message.Method, message.Path)
if message.Error != nil {
logString += " - " + message.Error.Error()
}
}
fmt.Println(logString)
}
func FiberLogger() fiber.Handler {
return func(c *fiber.Ctx) error {
errHandler := c.App().Config().ErrorHandler
chainErr := c.Next()
message := BuildMessage(c)
if chainErr != nil {
dawnError := ErrorHandler(c, chainErr)
message.Error = dawnError
}
LogRequest(message)
if chainErr != nil {
if err := errHandler(c, chainErr); err != nil {
_ = c.SendStatus(fiber.StatusInternalServerError)
}
}
return nil
}
}
func ErrorHandler(ctx *fiber.Ctx, err error) *DawnError {
var returnError *DawnError
if e, ok := err.(*DawnError); ok {
returnError = e
} else {
returnError = Build(err)
}
return returnError
}
/// LOG LEVELS
func stringToLevel(str string) int {
switch str {
case "TRACE":
return 1
case "DEBUG":
return 2
case "INFO":
return 3
}
return 1
}
func TRACE(c *fiber.Ctx, message string) {
if stringToLevel("TRACE") >= stringToLevel(viper.GetString("app.logLevel")) {
_log(c, "TRACE", message)
}
}
func DEBUG(c *fiber.Ctx, message string) {
if stringToLevel("DEBUG") >= stringToLevel(viper.GetString("app.logLevel")) {
_log(c, "DEBUG", message)
}
}
func INFO(c *fiber.Ctx, message string) {
if stringToLevel("INFO") >= stringToLevel(viper.GetString("app.logLevel")) {
_log(c, "INFO", message)
}
}
func _log(c *fiber.Ctx, level, message string) {
lg := buildMessageLog(c, message)
lg.Level = level
logString := ""
if viper.GetString("app.logType") == "json" {
tempLogString, _ := json.MarshalIndent(lg, "", " ")
logString = string(tempLogString)
} else {
logString = fmt.Sprintf("[%s] %s %s %s %s", fmt.Sprintf(LEVEL_FORMAT_STRING, lg.Level), lg.Date, lg.PID, lg.RequestId, lg.Message)
}
fmt.Println(logString)
}
package config
import "gitlab.cs.umd.edu/dawn/dawn-weather/common"
import "gitlab.cs.umd.edu/dawn/dawn-go-common/common"
func INVALID_PRODUCT(passedProduct string) *common.DawnError {
return &common.DawnError{
......
package controllers
import (
"gitlab.cs.umd.edu/dawn/dawn-weather/common"
"gitlab.cs.umd.edu/dawn/dawn-go-common/common"
"gitlab.cs.umd.edu/dawn/dawn-weather/models"
"gitlab.cs.umd.edu/dawn/dawn-weather/services"
......
package controllers
import (
"gitlab.cs.umd.edu/dawn/dawn-weather/common"
"gitlab.cs.umd.edu/dawn/dawn-go-common/common"
"gitlab.cs.umd.edu/dawn/dawn-weather/models"
"gitlab.cs.umd.edu/dawn/dawn-weather/services"
......
package controllers
import (
"gitlab.cs.umd.edu/dawn/dawn-weather/common"
"gitlab.cs.umd.edu/dawn/dawn-go-common/common"
"gitlab.cs.umd.edu/dawn/dawn-weather/models"
"gitlab.cs.umd.edu/dawn/dawn-weather/services"
......
......@@ -8,6 +8,7 @@ require (
github.com/ansrivas/fiberprometheus/v2 v2.1.2
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6 // indirect
github.com/arsmn/fiber-swagger/v2 v2.15.0
github.com/bketelsen/crypt v0.0.4 // indirect
github.com/bradfitz/slice v0.0.0-20180809154707-2b758aa73013
github.com/coreos/etcd v3.3.10+incompatible // indirect
github.com/coreos/go-etcd v2.0.0+incompatible // indirect
......@@ -17,19 +18,19 @@ require (
github.com/go-openapi/swag v0.19.15 // indirect
github.com/go-ozzo/ozzo-validation v3.6.0+incompatible
github.com/gofiber/adaptor/v2 v2.1.9 // indirect
github.com/gofiber/fiber/v2 v2.16.0
github.com/gofiber/fiber/v2 v2.19.0
github.com/google/uuid v1.3.0
github.com/klauspost/compress v1.13.3 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/prometheus/client_golang v1.11.0 // indirect
github.com/prometheus/common v0.30.0 // indirect
github.com/prometheus/procfs v0.7.2 // indirect
github.com/spf13/viper v1.8.1
github.com/spf13/viper v1.9.0
github.com/stretchr/testify v1.7.0 // indirect
github.com/swaggo/swag v1.7.1
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8 // indirect
github.com/valyala/fasthttp v1.28.0
github.com/valyala/fasthttp v1.30.0
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77 // indirect
gitlab.cs.umd.edu/dawn/dawn-go-common v0.0.0-20210927193837-6f19357fa765 // indirect
go.mongodb.org/mongo-driver v1.7.1
go4.org v0.0.0-20201209231011-d4a079459e60 // indirect
golang.org/x/net v0.0.0-20210917221730-978cfadd31cf // indirect
......
This diff is collapsed.
......@@ -4,7 +4,7 @@ import (
"flag"
"strconv"
"gitlab.cs.umd.edu/dawn/dawn-weather/common"
"gitlab.cs.umd.edu/dawn/dawn-go-common/common"
"gitlab.cs.umd.edu/dawn/dawn-weather/config"
"gitlab.cs.umd.edu/dawn/dawn-weather/controllers"
"gitlab.cs.umd.edu/dawn/dawn-weather/persistence"
......
......@@ -4,7 +4,7 @@ import (
"context"
"strings"
"gitlab.cs.umd.edu/dawn/dawn-weather/common"
"gitlab.cs.umd.edu/dawn/dawn-go-common/common"
"gitlab.cs.umd.edu/dawn/dawn-weather/config"
"gitlab.cs.umd.edu/dawn/dawn-weather/models"
"gitlab.cs.umd.edu/dawn/dawn-weather/persistence/entities"
......
......@@ -11,7 +11,7 @@ import (
"strings"
"time"
"gitlab.cs.umd.edu/dawn/dawn-weather/common"
"gitlab.cs.umd.edu/dawn/dawn-go-common/common"
"gitlab.cs.umd.edu/dawn/dawn-weather/config"
"gitlab.cs.umd.edu/dawn/dawn-weather/models"
"gitlab.cs.umd.edu/dawn/dawn-weather/models/enums"
......
......@@ -3,7 +3,7 @@ package services
import (
"time"
"gitlab.cs.umd.edu/dawn/dawn-weather/common"
"gitlab.cs.umd.edu/dawn/dawn-go-common/common"
"gitlab.cs.umd.edu/dawn/dawn-weather/models"
"gitlab.cs.umd.edu/dawn/dawn-weather/models/enums"
"gitlab.cs.umd.edu/dawn/dawn-weather/persistence"
......
......@@ -4,7 +4,7 @@ import (
"math"
"time"
"gitlab.cs.umd.edu/dawn/dawn-weather/common"
"gitlab.cs.umd.edu/dawn/dawn-go-common/common"
"gitlab.cs.umd.edu/dawn/dawn-weather/config"
"gitlab.cs.umd.edu/dawn/dawn-weather/models"
"gitlab.cs.umd.edu/dawn/dawn-weather/models/enums"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment