feat(authentication): Added authentication controller & JWTMiddleware
This commit is contained in:
+13
-43
@@ -1,72 +1,42 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"medad.fun/internal/database"
|
||||
controller "medad.fun/internal/api/v1"
|
||||
"medad.fun/internal/metrics"
|
||||
)
|
||||
|
||||
// SetupRoutes registers the /v1/ endpoints with the http package.
|
||||
func (handler *V1Handler) SetupRoutes(rootPath string, register func(string, string, http.HandlerFunc)) {
|
||||
func (handler *V1Handler) SetupRoutes(rootPath string, register func(string, string, http.HandlerFunc), protectedRegister func(string, string, http.HandlerFunc)) {
|
||||
v1 := path.Join(rootPath, "v1")
|
||||
|
||||
// Public Metrics
|
||||
metrics.SetupRoute(func(metricsHandler http.HandlerFunc) {
|
||||
register("GET", path.Join(v1, "metrics"), metricsHandler)
|
||||
})
|
||||
|
||||
// Public Stats
|
||||
register("GET", path.Join(v1, "stats"), handler.getStats)
|
||||
|
||||
// These exist only for the public API. We version them in order to ensure
|
||||
// backwards compatibility as far as possible.
|
||||
// Public Lobby Endpoints
|
||||
register("GET", path.Join(v1, "lobby"), handler.getLobbies)
|
||||
register("POST", path.Join(v1, "lobby"), handler.postLobby)
|
||||
|
||||
protectedRegister("POST", path.Join(v1, "lobby"), handler.postLobby)
|
||||
register("PATCH", path.Join(v1, "lobby", "{lobby_id}"), handler.patchLobby)
|
||||
// We support both path parameter and cookie.
|
||||
register("PATCH", path.Join(v1, "lobby"), handler.patchLobby)
|
||||
|
||||
// The websocket is shared between the public API and the official client
|
||||
// WebSocket is usually shared, but if you want to secure it, use protectedRegister
|
||||
register("GET", path.Join(v1, "lobby", "{lobby_id}", "ws"), handler.websocketUpgrade)
|
||||
// We support both path parameter and cookie.
|
||||
register("GET", path.Join(v1, "lobby", "ws"), handler.websocketUpgrade)
|
||||
|
||||
register("POST", path.Join(v1, "lobby", "{lobby_id}", "player"), handler.postPlayer)
|
||||
// Protected Player Action
|
||||
protectedRegister("POST", path.Join(v1, "lobby", "{lobby_id}", "player"), handler.postPlayer)
|
||||
|
||||
register("POST", path.Join(v1, "s1mple"), func(w http.ResponseWriter, r *http.Request) {
|
||||
rows, err := database.Pool.Query(r.Context(), "SELECT id, name FROM my_table")
|
||||
if err != nil {
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
log.Println(rows)
|
||||
|
||||
var data []map[string]interface{}
|
||||
for rows.Next() {
|
||||
var id int
|
||||
var name string
|
||||
if err := rows.Scan(&id, &name); err != nil {
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
data = append(data, map[string]interface{}{
|
||||
"id": id,
|
||||
"name": name,
|
||||
})
|
||||
}
|
||||
|
||||
log.Println(data)
|
||||
if started, err := marshalToHTTPWriter(data, w); err != nil {
|
||||
if !started {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
return
|
||||
}
|
||||
})
|
||||
// Auth Endpoints (Register/Login) are Public
|
||||
register("POST", path.Join(v1, "auth", "register"), controller.Register)
|
||||
register("POST", path.Join(v1, "auth", "login"), controller.Login)
|
||||
}
|
||||
|
||||
// remoteAddressToSimpleIP removes unnecessary clutter from the input,
|
||||
|
||||
Reference in New Issue
Block a user