2022-01-15 17:40:57 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-01-16 16:32:20 +00:00
|
|
|
"flag"
|
2022-01-15 17:40:57 +00:00
|
|
|
"fmt"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StandardResponse struct {
|
|
|
|
Code int `json:"code"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
Handler string `json:"handler"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleIndex(writer http.ResponseWriter, _ *http.Request) {
|
|
|
|
resp := StandardResponse{
|
|
|
|
Code: 200,
|
|
|
|
Status: "OK",
|
|
|
|
Handler: "index",
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := json.Marshal(resp)
|
|
|
|
if err != nil {
|
|
|
|
writer.WriteHeader(500)
|
|
|
|
_, _ = writer.Write([]byte("Internal server error"))
|
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "Internal server error while marshalling response")
|
2022-01-15 18:20:10 +00:00
|
|
|
return
|
2022-01-15 17:40:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
writer.Header().Set("Content-Type", "application/json")
|
|
|
|
writer.WriteHeader(200)
|
|
|
|
|
|
|
|
_, err = writer.Write(p)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
_, _ = writer.Write([]byte("Internal server error"))
|
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "Internal server error while sending response")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleInfo(writer http.ResponseWriter, _ *http.Request) {
|
|
|
|
resp := StandardResponse{
|
|
|
|
Code: 200,
|
|
|
|
Status: "OK",
|
|
|
|
Handler: "info",
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := json.Marshal(resp)
|
|
|
|
if err != nil {
|
|
|
|
writer.WriteHeader(500)
|
|
|
|
_, _ = writer.Write([]byte("Internal server error"))
|
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "Internal server error while marshalling response")
|
2022-01-15 18:20:10 +00:00
|
|
|
return
|
2022-01-15 17:40:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
writer.Header().Set("Content-Type", "application/json")
|
|
|
|
writer.WriteHeader(200)
|
|
|
|
|
|
|
|
_, err = writer.Write(p)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
_, _ = writer.Write([]byte("Internal server error"))
|
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "Internal server error while sending response")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleDefault(writer http.ResponseWriter, _ *http.Request) {
|
|
|
|
resp := StandardResponse{
|
|
|
|
Code: 404,
|
|
|
|
Status: "Not found",
|
|
|
|
Handler: "default",
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := json.Marshal(resp)
|
|
|
|
if err != nil {
|
|
|
|
writer.WriteHeader(500)
|
|
|
|
_, _ = writer.Write([]byte("Internal server error"))
|
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "Internal server error while marshalling response")
|
2022-01-15 18:20:10 +00:00
|
|
|
return
|
2022-01-15 17:40:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
writer.Header().Set("Content-Type", "application/json")
|
|
|
|
writer.WriteHeader(404)
|
|
|
|
|
|
|
|
_, err = writer.Write(p)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
_, _ = writer.Write([]byte("Internal server error"))
|
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "Internal server error while sending response")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2022-01-16 16:32:20 +00:00
|
|
|
var port int
|
|
|
|
flag.IntVar(&port, "p", 8080, "Server port")
|
|
|
|
flag.IntVar(&port, "port", 8080, "Server port")
|
|
|
|
flag.Parse()
|
2022-01-15 18:54:10 +00:00
|
|
|
|
|
|
|
fmt.Printf("Starting new server on port %v...", port)
|
2022-01-15 17:40:57 +00:00
|
|
|
r := mux.NewRouter()
|
|
|
|
|
|
|
|
r.HandleFunc("/", handleIndex)
|
|
|
|
r.HandleFunc("/info", handleInfo)
|
|
|
|
r.NotFoundHandler = http.HandlerFunc(handleDefault)
|
|
|
|
|
|
|
|
server := http.Server{
|
2022-01-15 18:54:10 +00:00
|
|
|
Addr: fmt.Sprintf(":%d", port),
|
2022-01-15 17:40:57 +00:00
|
|
|
Handler: r,
|
|
|
|
}
|
|
|
|
|
2022-01-15 18:54:10 +00:00
|
|
|
if err := server.ListenAndServe(); err != nil {
|
|
|
|
panic(fmt.Errorf("got error while starting server %v", err))
|
|
|
|
}
|
2022-01-15 17:40:57 +00:00
|
|
|
}
|