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

View File

@ -1,9 +1,8 @@
package handlers package http
import ( import (
"fmt" "fmt"
"image/jpeg" "image/jpeg"
"justcaptcha/internal/server"
"justcaptcha/pkg/captcha" "justcaptcha/pkg/captcha"
"justcaptcha/pkg/captcha/inmemdb" "justcaptcha/pkg/captcha/inmemdb"
"justcaptcha/pkg/dwcaptcha" "justcaptcha/pkg/dwcaptcha"
@ -18,7 +17,7 @@ type CaptchaHandlers struct {
expiry time.Duration expiry time.Duration
} }
func New(expiry time.Duration) *CaptchaHandlers { func NewCaptchaHandlers(expiry time.Duration) *CaptchaHandlers {
inmemdb.SetExpiry(expiry) inmemdb.SetExpiry(expiry)
return &CaptchaHandlers{expiry: 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) { 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") captchaStyle := r.URL.Query().Get("style")
captchaImage := inmemdb.Image(captchaID, captchaStyle) 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) { 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() r.ParseForm()
answer := captcha.Answer(r.FormValue("answer")) 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) { 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") isJustRemove := r.URL.Query().Has("remove")
if isJustRemove { if isJustRemove {

View File

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