Extracted server instance.

master
Tomasz Półgrabia 2022-01-14 21:36:01 +01:00
parent 121ff25839
commit 8bd3d7a27a
1 changed files with 20 additions and 15 deletions

View File

@ -2,22 +2,27 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt"
"net/http" "net/http"
) )
func main() { type StandardResponse struct {
fmt.Println("Hello World!!!") result int
f := func(writer http.ResponseWriter, req *http.Request) { status string
writer.Header().Set("Content-Type", "application/json")
m := map[string]string{
"result": "ok",
}
p, _ := json.Marshal(m)
writer.WriteHeader(201)
_, _ = writer.Write(p)
} }
http.HandleFunc("/", f) func main() {
_ = http.ListenAndServe(":8080", nil) hf := http.HandlerFunc(func(writer http.ResponseWriter, req *http.Request) {
writer.Header().Set("Content-Type", "application/json")
m := StandardResponse{
result: 200,
status: "OK",
}
p, _ := json.Marshal(m)
writer.WriteHeader(200)
_, _ = writer.Write(p)
})
s := http.Server{Addr: ":8080", Handler: hf}
_ = s.ListenAndServe()
} }