From 70419c178ef012450149468ce5c871be9b089fe4 Mon Sep 17 00:00:00 2001 From: Jacob Gunther Date: Sun, 23 Apr 2023 21:05:09 -0500 Subject: [PATCH] Clean up --- config.example.yml | 1 + go.mod | 3 +-- go.sum | 6 ++---- src/config.go | 16 +++++++++------- src/main.go | 22 ++++++++++++++-------- 5 files changed, 27 insertions(+), 21 deletions(-) diff --git a/config.example.yml b/config.example.yml index a8dac31..0504a93 100644 --- a/config.example.yml +++ b/config.example.yml @@ -1,3 +1,4 @@ +environment: development host: 127.0.0.1 port: 3000 redis: diff --git a/go.mod b/go.mod index 8ec93a4..39a4d54 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,6 @@ go 1.18 require ( github.com/go-redis/redis/v8 v8.11.5 github.com/gofiber/fiber/v2 v2.44.0 - github.com/joho/godotenv v1.5.1 github.com/mineatar-io/skin-render v1.0.2 gopkg.in/yaml.v3 v3.0.1 ) @@ -26,7 +25,7 @@ require ( github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee // indirect github.com/tinylib/msgp v1.1.8 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect - github.com/valyala/fasthttp v1.45.0 // indirect + github.com/valyala/fasthttp v1.46.0 // indirect github.com/valyala/tcplisten v1.0.0 // indirect golang.org/x/image v0.7.0 // indirect golang.org/x/sys v0.7.0 // indirect diff --git a/go.sum b/go.sum index e5225ec..5f80d60 100644 --- a/go.sum +++ b/go.sum @@ -12,8 +12,6 @@ github.com/gofiber/fiber/v2 v2.44.0 h1:Z90bEvPcJM5GFJnu1py0E1ojoerkyew3iiNJ78MQC github.com/gofiber/fiber/v2 v2.44.0/go.mod h1:VTMtb/au8g01iqvHyaCzftuM/xmZgKOZCtFzz6CdV9w= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= -github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/klauspost/compress v1.16.5 h1:IFV2oUNUzZaz+XyusxpLzpzS8Pt5rh0Z16For/djlyI= github.com/klauspost/compress v1.16.5/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= @@ -54,8 +52,8 @@ github.com/tinylib/msgp v1.1.8 h1:FCXC1xanKO4I8plpHGH2P7koL/RzZs12l/+r7vakfm0= github.com/tinylib/msgp v1.1.8/go.mod h1:qkpG+2ldGg4xRFmx+jfTvZPxfGFhi64BcnL9vkCm/Tw= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= -github.com/valyala/fasthttp v1.45.0 h1:zPkkzpIn8tdHZUrVa6PzYd0i5verqiPSkgTd3bSUcpA= -github.com/valyala/fasthttp v1.45.0/go.mod h1:k2zXd82h/7UZc3VOdJ2WaUqt1uZ/XpXAfE9i+HBC3lA= +github.com/valyala/fasthttp v1.46.0 h1:6ZRhrFg8zBXTRYY6vdzbFhqsBd7FVv123pV2m9V87U4= +github.com/valyala/fasthttp v1.46.0/go.mod h1:k2zXd82h/7UZc3VOdJ2WaUqt1uZ/XpXAfE9i+HBC3lA= github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= diff --git a/src/config.go b/src/config.go index fa9762d..c37b799 100644 --- a/src/config.go +++ b/src/config.go @@ -10,8 +10,9 @@ import ( var ( // DefaultConfig is the default configuration values used by the application. DefaultConfig *Config = &Config{ - Host: "127.0.0.1", - Port: 3001, + Environment: "development", + Host: "127.0.0.1", + Port: 3001, Redis: RedisConfig{ Host: "127.0.0.1", Port: 6379, @@ -91,11 +92,12 @@ var ( // Config is the root configuration object for the application. type Config struct { - Host string `yaml:"host"` - Port uint16 `yaml:"port"` - Redis RedisConfig `yaml:"redis"` - Routes RoutesConfig `yaml:"routes"` - Cache CacheConfig `yaml:"cache"` + Environment string `yaml:"environment"` + Host string `yaml:"host"` + Port uint16 `yaml:"port"` + Redis RedisConfig `yaml:"redis"` + Routes RoutesConfig `yaml:"routes"` + Cache CacheConfig `yaml:"cache"` } // RoutesConfig is the configuration data of all API routes. diff --git a/src/main.go b/src/main.go index e79ccca..4336a70 100644 --- a/src/main.go +++ b/src/main.go @@ -7,8 +7,8 @@ import ( "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/cors" + "github.com/gofiber/fiber/v2/middleware/logger" "github.com/gofiber/fiber/v2/middleware/recover" - "github.com/joho/godotenv" ) var ( @@ -27,8 +27,6 @@ var ( func init() { var err error - godotenv.Load() - if err = conf.ReadFile("config.yml"); err != nil { log.Fatal(err) } @@ -40,11 +38,19 @@ func init() { log.Println("Successfully connected to Redis") app.Use(recover.New()) - app.Use(cors.New(cors.Config{ - AllowOrigins: "*", - AllowMethods: "HEAD,OPTIONS,GET", - ExposeHeaders: "Content-Type,Content-Disposition,X-Cache-Hit", - })) + + if conf.Environment == "development" { + app.Use(cors.New(cors.Config{ + AllowOrigins: "*", + AllowMethods: "HEAD,OPTIONS,GET", + ExposeHeaders: "X-Cache-Hit,X-Cache-Time-Remaining", + })) + + app.Use(logger.New(logger.Config{ + Format: "${time} ${ip}:${port} -> ${status}: ${method} ${path} (${latency})\n", + TimeFormat: "2006/01/02 15:04:05", + })) + } } func main() {