-
-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
142 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package controllers | ||
|
||
import "github.com/gofiber/fiber/v2" | ||
|
||
type Controller struct { | ||
} | ||
|
||
func NewController() *Controller { | ||
return &Controller{} | ||
} | ||
|
||
func (c *Controller) Ping(ctx *fiber.Ctx) error { | ||
return ctx.SendString("Pong") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package server | ||
|
||
import ( | ||
"github.com/TimothyYe/godns/internal/server/controllers" | ||
"github.com/gofiber/fiber/v2" | ||
"github.com/gofiber/fiber/v2/middleware/cors" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type Server struct { | ||
addr string | ||
app *fiber.App | ||
controller *controllers.Controller | ||
} | ||
|
||
func (s *Server) SetAddress(addr string) *Server { | ||
s.addr = addr | ||
return s | ||
} | ||
|
||
func (s *Server) Build() { | ||
config := fiber.Config{} | ||
s.app = fiber.New(config) | ||
s.controller = controllers.NewController() | ||
} | ||
|
||
func (s *Server) Start() error { | ||
s.initRoutes() | ||
|
||
log.Infof("Server is listening on port: %s", s.addr) | ||
return s.app.Listen(s.addr) | ||
} | ||
|
||
func (s *Server) initRoutes() { | ||
// set cross domain access rules | ||
s.app.Use(cors.New(cors.Config{ | ||
AllowOrigins: "*", | ||
AllowMethods: "GET, POST ,PUT ,DELETE, OPTIONS", | ||
AllowHeaders: "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization", | ||
})) | ||
|
||
// Create routes group. | ||
route := s.app.Group("/api/v1") | ||
|
||
// authMiddleware := keyauth.New(keyauth.Config{ | ||
// Validator: func(c *fiber.Ctx, key string) (bool, error) { | ||
// hashedAPIKey := sha256.Sum256([]byte(apiKey)) | ||
// hashedKey := sha256.Sum256([]byte(key)) | ||
|
||
// if subtle.ConstantTimeCompare(hashedAPIKey[:], hashedKey[:]) == 1 { | ||
// return true, nil | ||
// } | ||
// return false, keyauth.ErrMissingOrMalformedAPIKey | ||
// }, | ||
// }) | ||
|
||
route.Get("/ping", s.controller.Ping) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters