HTTP server and handlers were restructurised.

This commit is contained in:
Alexander Andreev 2023-01-08 17:49:03 +04:00
parent 49a64f553e
commit ea9877f6c4
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F
3 changed files with 9 additions and 11 deletions

View File

@ -3,8 +3,7 @@ package main
import (
"flag"
"fmt"
"justcaptcha/internal/handlers"
"justcaptcha/internal/server"
"justcaptcha/internal/http"
"log"
"net/netip"
"os"
@ -29,8 +28,8 @@ func main() {
return
}
hand := handlers.New(*captchaExpiry)
srv := server.NewHttpServer()
hand := http.NewCaptchaHandlers(*captchaExpiry)
srv := http.NewHttpServer()
srv.POST("/", hand.New)
srv.POST("/:captcha", hand.Solve)

View File

@ -1,9 +1,8 @@
package handlers
package http
import (
"fmt"
"image/jpeg"
"justcaptcha/internal/server"
"justcaptcha/pkg/captcha"
"justcaptcha/pkg/captcha/inmemdb"
"justcaptcha/pkg/dwcaptcha"
@ -18,7 +17,7 @@ type CaptchaHandlers struct {
expiry time.Duration
}
func New(expiry time.Duration) *CaptchaHandlers {
func NewCaptchaHandlers(expiry time.Duration) *CaptchaHandlers {
inmemdb.SetExpiry(expiry)
return &CaptchaHandlers{expiry: expiry}
}
@ -31,7 +30,7 @@ func (h *CaptchaHandlers) New(w http.ResponseWriter, r *http.Request) {
}
func (h *CaptchaHandlers) Image(w http.ResponseWriter, r *http.Request) {
captchaID := captcha.ID(server.GetURLParam(r, "captcha"))
captchaID := captcha.ID(GetURLParam(r, "captcha"))
captchaStyle := r.URL.Query().Get("style")
captchaImage := inmemdb.Image(captchaID, captchaStyle)
@ -46,7 +45,7 @@ func (h *CaptchaHandlers) Image(w http.ResponseWriter, r *http.Request) {
}
func (h *CaptchaHandlers) Solve(w http.ResponseWriter, r *http.Request) {
captchaID := captcha.ID(server.GetURLParam(r, "captcha"))
captchaID := captcha.ID(GetURLParam(r, "captcha"))
r.ParseForm()
answer := captcha.Answer(r.FormValue("answer"))
@ -60,7 +59,7 @@ func (h *CaptchaHandlers) Solve(w http.ResponseWriter, r *http.Request) {
}
func (h *CaptchaHandlers) IsSolved(w http.ResponseWriter, r *http.Request) {
captchaID := captcha.ID(server.GetURLParam(r, "captcha"))
captchaID := captcha.ID(GetURLParam(r, "captcha"))
isJustRemove := r.URL.Query().Has("remove")
if isJustRemove {

View File

@ -1,4 +1,4 @@
package server
package http
import (
"context"