turned on simple http server
This commit is contained in:
@@ -0,0 +1,38 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type JsonHandler func(http.ResponseWriter, *http.Request)
|
||||||
|
|
||||||
|
func (h JsonHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
h(w, req)
|
||||||
|
}
|
||||||
|
|
||||||
|
type JSONError struct {
|
||||||
|
Reason string `json:"reason"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type JSONMessage struct {
|
||||||
|
Message string `json:"message"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func apiInfo(w http.ResponseWriter, req *http.Request) {
|
||||||
|
version := struct {
|
||||||
|
Version string `json:"version"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Routes []string `json:"routes"`
|
||||||
|
}{
|
||||||
|
Version: "0.1",
|
||||||
|
Name: "itslog",
|
||||||
|
Routes: []string{
|
||||||
|
"/api/v0/info/",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
if err := json.NewEncoder(w).Encode(version); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/kelseyhightower/envconfig"
|
"github.com/kelseyhightower/envconfig"
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -16,4 +18,11 @@ func main() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
log.Printf("%+v", config)
|
log.Printf("%+v", config)
|
||||||
|
|
||||||
|
http.Handle("/api/v0/info/", JsonHandler(apiInfo))
|
||||||
|
|
||||||
|
err = http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user