package main import ( "encoding/json" "fmt" "log" "net/http" ) type HttpServer struct { Port string ovpn *OpenVpnMgt } func (h *HttpServer) handler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:]) } func (h *HttpServer) versionHandler(w http.ResponseWriter, r *http.Request) { err, message := h.ovpn.Version() if err != nil { fmt.Fprintf(w, "Error : %s", err) } jsonStr, err := json.Marshal(message) if err != nil { fmt.Fprintf(w, "Error : %s", err) } fmt.Fprintf(w, "%s", jsonStr) } func (h *HttpServer) helpHandler(w http.ResponseWriter, r *http.Request) { err, message := h.ovpn.Help() if err != nil { fmt.Fprintf(w, "Error : %s", err) } else { fmt.Fprintf(w, "%s", message) } } func NewHTTPServer(port string, s *OpenVpnMgt) { h := &HttpServer{ Port: port, ovpn: s, } http.HandleFunc("/help", h.helpHandler) http.HandleFunc("/version", h.versionHandler) http.HandleFunc("/", h.handler) log.Fatal(http.ListenAndServe(port, nil)) }