Browse Source

Add moc-server termination

New `/exit` endpoint, GET or POST request will terminate moc-server.
main
Дмитрий 3 years ago
parent
commit
064155f5d5
  1. 14
      Makefile
  2. 40
      internal/moc_server.go

14
Makefile

@ -21,19 +21,19 @@ build:
go build -o bin/simple-choose-ad cmd/main.go
start-moc-server:
@echo "[!] Starting moc server on 127.0.0.1:5059..."
@nohup go run internal/moc_server.go -l $(moc_server_address)> /dev/null 2>&1 & echo $! > run.pid
@echo "[!] Starting moc server on $(moc_server_address) ..."
@go run internal/moc_server.go -l $(moc_server_address) &
stop-moc-server:
@echo "[!] Killing moc server"
@cat run.pid | xargs kill -9
@rm run.pid
@curl -s -o /dev/null "$(moc_server_address)/exit" &
test-server:
@echo "Testing server..."
@$(MAKE) start-moc-server
@cd "cmd/client_server/"; \
go test
tests: start-moc-server
@$(MAKE) test-server
@$(MAKE) stop-moc-server
tests: test-server

40
internal/moc_server.go

@ -2,29 +2,16 @@ package main
import (
"flag"
"io/ioutil"
"log"
"net/http"
"os"
)
func main() {
vres := `{
"id": "123",
"imp": [{
"id": 123,
"width": 144,
"height": 122,
"title": "Title1",
"url": "example.com",
"price": 123.5
},{
"id": 123,
"width": 155,
"height": 133,
"title": "Title2",
"url": "upachka.com",
"price": 143.5
}]}`
file, err := os.ReadFile("internal/json/valid_response.json")
if err != nil {
log.Fatalln(err)
}
addr := flag.String("l", "", "-l 127.0.0.1:5059")
flag.Parse()
@ -32,12 +19,19 @@ func main() {
log.Fatalln("Error: listening address is required!")
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
b, _ := ioutil.ReadAll(r.Body)
log.Println(string(b))
http.HandleFunc("/bid_request", func(w http.ResponseWriter, r *http.Request) {
// b, _ := ioutil.ReadAll(r.Body)
// log.Println(string(b))
w.Header().Add("Content-Type", "application/json")
w.Write([]byte(vres))
w.Write(file)
})
// endpoint: /exit
// Terminate server with code 0.
http.HandleFunc("/exit", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
os.Exit(0)
})
log.Fatal(http.ListenAndServe(*addr, nil))
}

Loading…
Cancel
Save