Small refactoring into modules.

master
Tomasz Polgrabia 2024-07-06 15:39:34 +02:00
parent 6b50fca7e6
commit 24db5aa3a6
3 changed files with 82 additions and 74 deletions

View File

@ -0,0 +1,62 @@
package handlers
import (
"encoding/json"
"fmt"
"net/http"
"time"
)
var startTime = time.Now()
func GreetingHandler(writer http.ResponseWriter, request *http.Request) {
decoder := json.NewDecoder(request.Body)
decoder.DisallowUnknownFields()
var p = PersonRequest{}
errDecode := decoder.Decode(&p)
if errDecode != nil {
println("Error: ", errDecode)
writer.WriteHeader(400)
_, _ = writer.Write([]byte("Bad request data"))
return
}
r := PersonResponse{
Greeting: fmt.Sprintf("Hey %s. Your age is %d", p.Name, p.Age),
Errors: "",
}
writer.Header().Add("Content-Type", "application/json")
encoder := produceEncoder(writer)
errEncode := encoder.Encode(&r)
if errEncode != nil {
writer.WriteHeader(500)
println("Error: ", errEncode)
return
}
writer.WriteHeader(200)
}
func produceEncoder(writer http.ResponseWriter) *json.Encoder {
encoder := json.NewEncoder(writer)
return encoder
}
func StatusHandler(writer http.ResponseWriter, _ *http.Request) {
upTime := time.Now().Unix() - startTime.Unix()
status := StatusResponse{
Status: "Ok",
StartDateTime: startTime.Format("2006-01-02 15:04:05.000-0700"),
UpTime: fmt.Sprintf("%d", upTime),
}
writer.Header().Add("Content-Type", "application/json")
encoder := produceEncoder(writer)
errEncode := encoder.Encode(&status)
if errEncode != nil {
writer.WriteHeader(500)
println("Error: ", errEncode)
return
}
writer.WriteHeader(200)
}

View File

@ -0,0 +1,17 @@
package handlers
type PersonRequest struct {
Name string
Age int
}
type PersonResponse struct {
Greeting string
Errors string
}
type StatusResponse struct {
StartDateTime string
UpTime string
Status string
}

View File

@ -1,31 +1,12 @@
package main package main
import ( import (
"encoding/json"
"flag" "flag"
"fmt" "fmt"
"golang_demo1/handlers"
"net/http" "net/http"
"time"
) )
type PersonRequest struct {
Name string
Age int
}
type PersonResponse struct {
Greeting string
Errors string
}
type StatusResponse struct {
StartDateTime string
UpTime string
Status string
}
var startTime = time.Now()
func main() { func main() {
portPtr := flag.Int("port", 8080, "port for webserver") portPtr := flag.Int("port", 8080, "port for webserver")
flag.Parse() flag.Parse()
@ -34,8 +15,8 @@ func main() {
fmt.Printf("Server is listening on port: %d", port) fmt.Printf("Server is listening on port: %d", port)
server := http.NewServeMux() server := http.NewServeMux()
server.HandleFunc("/status", statusHandler) server.HandleFunc("/status", handlers.StatusHandler)
server.HandleFunc("/greeting", greetingHandler) server.HandleFunc("/greeting", handlers.GreetingHandler)
err := http.ListenAndServe(fmt.Sprintf("localhost:%d", port), server) err := http.ListenAndServe(fmt.Sprintf("localhost:%d", port), server)
if err != nil { if err != nil {
@ -43,55 +24,3 @@ func main() {
return return
} }
} }
func greetingHandler(writer http.ResponseWriter, request *http.Request) {
decoder := json.NewDecoder(request.Body)
decoder.DisallowUnknownFields()
var p = PersonRequest{}
errDecode := decoder.Decode(&p)
if errDecode != nil {
println("Error: ", errDecode)
writer.WriteHeader(400)
_, _ = writer.Write([]byte("Bad request data"))
return
}
r := PersonResponse{
Greeting: fmt.Sprintf("Hey %s. Your age is %d", p.Name, p.Age),
Errors: "",
}
writer.Header().Add("Content-Type", "application/json")
encoder := produceEncoder(writer)
errEncode := encoder.Encode(&r)
if errEncode != nil {
writer.WriteHeader(500)
println("Error: ", errEncode)
return
}
writer.WriteHeader(200)
}
func produceEncoder(writer http.ResponseWriter) *json.Encoder {
encoder := json.NewEncoder(writer)
return encoder
}
func statusHandler(writer http.ResponseWriter, _ *http.Request) {
upTime := time.Now().Unix() - startTime.Unix()
status := StatusResponse{
Status: "Ok",
StartDateTime: startTime.Format("2006-01-02 15:04:05.000-0700"),
UpTime: fmt.Sprintf("%d", upTime),
}
writer.Header().Add("Content-Type", "application/json")
encoder := produceEncoder(writer)
errEncode := encoder.Encode(&status)
if errEncode != nil {
writer.WriteHeader(500)
println("Error: ", errEncode)
return
}
writer.WriteHeader(200)
}