diff --git a/config.example.yml b/config.example.yml index 9292d51..0504a93 100644 --- a/config.example.yml +++ b/config.example.yml @@ -1,7 +1,6 @@ environment: development host: 127.0.0.1 port: 3000 -auth_key: "" redis: host: 127.0.0.1 port: 6379 diff --git a/src/config.go b/src/config.go index b87b3af..c37b799 100644 --- a/src/config.go +++ b/src/config.go @@ -13,7 +13,6 @@ var ( Environment: "development", Host: "127.0.0.1", Port: 3001, - AuthKey: "", Redis: RedisConfig{ Host: "127.0.0.1", Port: 6379, @@ -96,7 +95,6 @@ type Config struct { Environment string `yaml:"environment"` Host string `yaml:"host"` Port uint16 `yaml:"port"` - AuthKey string `yaml:"auth_key"` Redis RedisConfig `yaml:"redis"` Routes RoutesConfig `yaml:"routes"` Cache CacheConfig `yaml:"cache"` diff --git a/src/main.go b/src/main.go index 4336a70..fa817d4 100644 --- a/src/main.go +++ b/src/main.go @@ -1,9 +1,11 @@ package main import ( - "fmt" "log" "net/http" + "os" + "os/signal" + "syscall" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/cors" @@ -20,8 +22,9 @@ var ( return ctx.SendStatus(http.StatusInternalServerError) }, }) - r *Redis = &Redis{} - conf *Config = &Config{} + r *Redis = &Redis{} + conf *Config = &Config{} + instanceID uint16 = 0 ) func init() { @@ -51,17 +54,20 @@ func init() { TimeFormat: "2006/01/02 15:04:05", })) } + + if instanceID, err = GetInstanceID(); err != nil { + panic(err) + } } func main() { defer r.Close() - instanceID, err := GetInstanceID() + go ListenAndServe(conf.Host, conf.Port+instanceID) - if err != nil { - log.Fatal(err) - } + defer app.Shutdown() - log.Printf("Listening on %s:%d\n", conf.Host, conf.Port+instanceID) - log.Fatal(app.Listen(fmt.Sprintf("%s:%d", conf.Host, conf.Port+instanceID))) + s := make(chan os.Signal, 1) + signal.Notify(s, os.Interrupt, syscall.SIGTERM) + <-s } diff --git a/src/routes.go b/src/routes.go index 7208bf4..9609795 100644 --- a/src/routes.go +++ b/src/routes.go @@ -31,16 +31,6 @@ func PingHandler(ctx *fiber.Ctx) error { // ListHandler is the API handler used for the `/list` route. func ListHandler(ctx *fiber.Ctx) error { - authKey := ctx.Get("Authorization") - - if len(authKey) < 1 { - return ctx.SendStatus(http.StatusUnauthorized) - } - - if authKey != conf.AuthKey { - return ctx.SendStatus(http.StatusForbidden) - } - result := make([]string, 0) var ( diff --git a/src/server.go b/src/server.go new file mode 100644 index 0000000..932af9f --- /dev/null +++ b/src/server.go @@ -0,0 +1,14 @@ +package main + +import ( + "fmt" + "log" +) + +func ListenAndServe(host string, port uint16) { + log.Printf("Listening on %s:%d\n", host, port) + + if err := app.Listen(fmt.Sprintf("%s:%d", host, port)); err != nil { + panic(err) + } +}