package main

import (
  "encoding/json"
  "fmt"
  "github.com/gwwfps/lolconf-probe/display"
  "log"
  "net/http"
)

type handler func(http.ResponseWriter, *http.Request)
type inner func() (interface{}, error)

func wrapHandler(h inner) handler {
  return func(w http.ResponseWriter, r *http.Request) {
    result, handlerError := h()
    if handlerError != nil {
      writeError(w, handlerError)
      return
    }

    serialized, marshalError := json.Marshal(result)
    if marshalError != nil {
      writeError(w, marshalError)
      return
    }

    w.Header().Set("Content-Type", "application/json; charset=utf-8")
    fmt.Fprint(w, string(serialized))
  }
}

func writeError(w http.ResponseWriter, e error) {
  w.WriteHeader(http.StatusInternalServerError)
  fmt.Fprint(w, e.Error())
}

func main() {
  http.HandleFunc("/resolutions", wrapHandler(display.ListAvailableResolutions))
  e := http.ListenAndServe("127.0.0.1:5532", nil)
  if e != nil {
    log.Fatal("ListenAndServe: ", e)
  }
}

package main

import (
  "encoding/json"
  "fmt"
  "github.com/gwwfps/lolconf-probe/display"
  "github.com/natefinch/npipe"
  "log"
  "net/http"
)

type handler func(http.ResponseWriter, *http.Request)
type inner func() (interface{}, error)

func wrapHandler(h inner) handler {
  return func(w http.ResponseWriter, r *http.Request) {
    result, handlerError := h()
    if handlerError != nil {
      writeError(w, handlerError)
      return
    }

    serialized, marshalError := json.Marshal(result)
    if marshalError != nil {
      writeError(w, marshalError)
      return
    }

    w.Header().Set("Content-Type", "application/json; charset=utf-8")
    fmt.Fprint(w, string(serialized))
  }
}

func writeError(w http.ResponseWriter, e error) {
  w.WriteHeader(http.StatusInternalServerError)
  fmt.Fprint(w, e.Error())
}

func main() {
  http.HandleFunc("/resolutions", wrapHandler(display.ListAvailableResolutions))
  l, e := npipe.Listen(`\\.\pipe\lolconf`)
  if e != nil {
    log.Fatal("Error listening on pipe: ", e)
  }
  serveError := http.Serve(l, nil)
  if serveError != nil {
    log.Fatal("Http serve error: ", serveError)
  }
}

--- 
+++ 
@@ -4,7 +4,6 @@
   "encoding/json"
   "fmt"
   "github.com/gwwfps/lolconf-probe/display"
-  "github.com/natefinch/npipe"
   "log"
   "net/http"
 )
@@ -38,12 +37,8 @@
 
 func main() {
   http.HandleFunc("/resolutions", wrapHandler(display.ListAvailableResolutions))
-  l, e := npipe.Listen(`\\.\pipe\lolconf`)
+  e := http.ListenAndServe("127.0.0.1:5532", nil)
   if e != nil {
-    log.Fatal("Error listening on pipe: ", e)
-  }
-  serveError := http.Serve(l, nil)
-  if serveError != nil {
-    log.Fatal("Http serve error: ", serveError)
+    log.Fatal("ListenAndServe: ", e)
   }
 }
Revert "switched to using npipe instead of socket"

This reverts commit d115fc9c0f2c97e46c216179aea4f4252e7a08bb.
