http server initial implementation

pull/1/head
Martin Boehm 2018-01-18 16:44:52 +01:00
parent 441c0f9024
commit bbcadfd646
1 changed files with 61 additions and 0 deletions

61
server/https.go 100644
View File

@ -0,0 +1,61 @@
package server
import (
"blockbook/db"
"encoding/json"
"fmt"
"net/http"
"os"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)
type server struct {
https *http.Server
db *db.RocksDB
}
func New(db *db.RocksDB) (*server, error) {
https := &http.Server{
Addr: ":8333",
}
s := &server{
https: https,
db: db,
}
r := mux.NewRouter()
r.HandleFunc("/", s.Info)
var h http.Handler = r
h = handlers.LoggingHandler(os.Stdout, h)
https.Handler = h
return s, nil
}
func (s *server) Run() error {
fmt.Printf("http server starting")
return s.https.ListenAndServe()
}
func (s *server) Close() error {
fmt.Printf("http server closing")
return s.https.Close()
}
func (s *server) Shutdown() error {
fmt.Printf("http server shutdown")
return s.https.Shutdown()
}
func (s *server) Info(w http.ResponseWriter, r *http.Request) {
type info struct {
Version string `json:"version"`
}
json.NewEncoder(w).Encode(info{
Version: "0.0.1",
})
}