patterngoMinor
Simple HTTP server that performs a regex test
Viewed 0 times
simpleperformshttpthattestserverregex
Problem
I'm sure that my server can work with better performance, but it's just a dream.
Parsing a very long strings is not so obvious task for me.
I have a server that handles a lot of clients requests and finds a substring of a string.
Server
Controller
Test
``
watching in person and on television and expecting great
things from him — at least one more gold medal for America,
if not another world record — during this, his fourth and
surely his last appearance in the World Olympics, and realizing
that his legs could no longer carry him down the runway with
the same blazing speed and confidence in making a huge,
eye-popping leap that they were capable of a few years ago
when he set world records in the 100-meter dash and in the
Parsing a very long strings is not so obvious task for me.
I have a server that handles a lot of clients requests and finds a substring of a string.
Server
package api
import (
"net/http"
"stackexchange/api/controllers"
)
func start() {
mux := http.NewServeMux()
mux.HandleFunc("/messages", Messages)
}
func Messages(res http.ResponseWriter, req *http.Request) {
mc := controllers.NewMessageController()
switch req.Method {
case "POST":
mc.ActionPost(res, req)
}
}Controller
package controllers
import (
"net/http"
"regexp"
)
type MessageController struct {
}
func NewMessageController() *MessageController {
return &MessageController{}
}
func (mc *MessageController) ActionPost(res http.ResponseWriter, req *http.Request) {
msgBody := req.URL.Query().Get("body")
msgPattern := req.URL.Query().Get("pattern")
isPatternFind := regexp.MatchString(msgPattern, msgBody)
if isPatternFind {
res.Write([]byte("find"))
} else {
res.Write([]byte("not find"))
}
}Test
``
package tests
import (
"fmt"
"log"
"net/http"
"net/http/httptest"
"stackexchange/api"
"testing"
)
var longString = Knowing that millions of people around the world would bewatching in person and on television and expecting great
things from him — at least one more gold medal for America,
if not another world record — during this, his fourth and
surely his last appearance in the World Olympics, and realizing
that his legs could no longer carry him down the runway with
the same blazing speed and confidence in making a huge,
eye-popping leap that they were capable of a few years ago
when he set world records in the 100-meter dash and in the
Solution
The call to
Some comments on style:
-
Build strings in a consistent manner:
regexp.MatchString is the only really expensive part of your code. Do you need full regexp support? If you just want to find substrings, you should use strings.Contains instead of the regexp package. Otherwise, there's no way around it. It might be worth looking at what each request looks like: you could call strings.Contains for the simple ones (that don't use any regexp features) and only use regexp.MatchString when needed.Some comments on style:
- Your
NewMessageControllermethod is useless. Just have your callers usenew(controllers.MessageController).
- Use
t.Fatalfinstead oflog.Fatalfin your tests.
-
Build strings in a consistent manner:
var (
longString = "your long string"
subString = "home"
host = "http://localhost:8080"
)
func TestPostMessage(t *testing.T) {
url := fmt.Sprintf("%s/messages?body=%s&pattern=%s", host, longString, subString)
…
}Code Snippets
var (
longString = "your long string"
subString = "home"
host = "http://localhost:8080"
)
func TestPostMessage(t *testing.T) {
url := fmt.Sprintf("%s/messages?body=%s&pattern=%s", host, longString, subString)
…
}Context
StackExchange Code Review Q#130003, answer score: 3
Revisions (0)
No revisions yet.