Remove authorization from list route (#1)

This commit is contained in:
Jacob
2023-07-20 13:57:29 -05:00
committed by GitHub
parent 07cc9593cb
commit f925a29ac8
5 changed files with 29 additions and 22 deletions

View File

@@ -1,7 +1,6 @@
environment: development environment: development
host: 127.0.0.1 host: 127.0.0.1
port: 3000 port: 3000
auth_key: ""
redis: redis:
host: 127.0.0.1 host: 127.0.0.1
port: 6379 port: 6379

View File

@@ -13,7 +13,6 @@ var (
Environment: "development", Environment: "development",
Host: "127.0.0.1", Host: "127.0.0.1",
Port: 3001, Port: 3001,
AuthKey: "",
Redis: RedisConfig{ Redis: RedisConfig{
Host: "127.0.0.1", Host: "127.0.0.1",
Port: 6379, Port: 6379,
@@ -96,7 +95,6 @@ type Config struct {
Environment string `yaml:"environment"` Environment string `yaml:"environment"`
Host string `yaml:"host"` Host string `yaml:"host"`
Port uint16 `yaml:"port"` Port uint16 `yaml:"port"`
AuthKey string `yaml:"auth_key"`
Redis RedisConfig `yaml:"redis"` Redis RedisConfig `yaml:"redis"`
Routes RoutesConfig `yaml:"routes"` Routes RoutesConfig `yaml:"routes"`
Cache CacheConfig `yaml:"cache"` Cache CacheConfig `yaml:"cache"`

View File

@@ -1,9 +1,11 @@
package main package main
import ( import (
"fmt"
"log" "log"
"net/http" "net/http"
"os"
"os/signal"
"syscall"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors" "github.com/gofiber/fiber/v2/middleware/cors"
@@ -22,6 +24,7 @@ var (
}) })
r *Redis = &Redis{} r *Redis = &Redis{}
conf *Config = &Config{} conf *Config = &Config{}
instanceID uint16 = 0
) )
func init() { func init() {
@@ -51,17 +54,20 @@ func init() {
TimeFormat: "2006/01/02 15:04:05", TimeFormat: "2006/01/02 15:04:05",
})) }))
} }
if instanceID, err = GetInstanceID(); err != nil {
panic(err)
}
} }
func main() { func main() {
defer r.Close() defer r.Close()
instanceID, err := GetInstanceID() go ListenAndServe(conf.Host, conf.Port+instanceID)
if err != nil { defer app.Shutdown()
log.Fatal(err)
}
log.Printf("Listening on %s:%d\n", conf.Host, conf.Port+instanceID) s := make(chan os.Signal, 1)
log.Fatal(app.Listen(fmt.Sprintf("%s:%d", conf.Host, conf.Port+instanceID))) signal.Notify(s, os.Interrupt, syscall.SIGTERM)
<-s
} }

View File

@@ -31,16 +31,6 @@ func PingHandler(ctx *fiber.Ctx) error {
// ListHandler is the API handler used for the `/list` route. // ListHandler is the API handler used for the `/list` route.
func ListHandler(ctx *fiber.Ctx) error { 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) result := make([]string, 0)
var ( var (

14
src/server.go Normal file
View File

@@ -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)
}
}