@ -1,6 +1,8 @@ |
|||||
{ |
{ |
||||
"ip": "127.0.0.1", |
"ip": "127.0.0.1", |
||||
"port": "3001", |
|
||||
|
"port": "3007", |
||||
|
"restip": "127.0.0.1", |
||||
|
"restport": "3008", |
||||
"serverip": "127.0.0.1", |
"serverip": "127.0.0.1", |
||||
"serverport": "3000" |
"serverport": "3000" |
||||
} |
} |
@ -0,0 +1,20 @@ |
|||||
|
package main |
||||
|
|
||||
|
import ( |
||||
|
"log" |
||||
|
"net/http" |
||||
|
|
||||
|
"github.com/gorilla/handlers" |
||||
|
) |
||||
|
|
||||
|
func runRestServer() { |
||||
|
//run API
|
||||
|
log.Println("server running") |
||||
|
log.Print("port: ") |
||||
|
log.Println(config.RestPort) |
||||
|
router := NewRouter() |
||||
|
headersOk := handlers.AllowedHeaders([]string{"X-Requested-With", "Access-Control-Allow-Origin"}) |
||||
|
originsOk := handlers.AllowedOrigins([]string{"*"}) |
||||
|
methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"}) |
||||
|
log.Fatal(http.ListenAndServe(":"+config.RestPort, handlers.CORS(originsOk, headersOk, methodsOk)(router))) |
||||
|
} |
@ -0,0 +1,47 @@ |
|||||
|
package main |
||||
|
|
||||
|
import ( |
||||
|
"log" |
||||
|
"net/http" |
||||
|
"time" |
||||
|
|
||||
|
"github.com/gorilla/mux" |
||||
|
) |
||||
|
|
||||
|
type Route struct { |
||||
|
Name string |
||||
|
Method string |
||||
|
Pattern string |
||||
|
HandlerFunc http.HandlerFunc |
||||
|
} |
||||
|
|
||||
|
func Logger(inner http.Handler, name string) http.Handler { |
||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
||||
|
start := time.Now() |
||||
|
|
||||
|
inner.ServeHTTP(w, r) |
||||
|
|
||||
|
log.Printf( |
||||
|
"%s\t%s\t%s\t%s", |
||||
|
r.Method, |
||||
|
r.RequestURI, |
||||
|
name, |
||||
|
time.Since(start), |
||||
|
) |
||||
|
}) |
||||
|
} |
||||
|
func NewRouter() *mux.Router { |
||||
|
router := mux.NewRouter().StrictSlash(true) |
||||
|
for _, route := range routes { |
||||
|
var handler http.Handler |
||||
|
handler = route.HandlerFunc |
||||
|
handler = Logger(handler, route.Name) |
||||
|
|
||||
|
router. |
||||
|
Methods(route.Method). |
||||
|
Path(route.Pattern). |
||||
|
Name(route.Name). |
||||
|
Handler(handler) |
||||
|
} |
||||
|
return router |
||||
|
} |
@ -0,0 +1,22 @@ |
|||||
|
package main |
||||
|
|
||||
|
import ( |
||||
|
"fmt" |
||||
|
"net/http" |
||||
|
) |
||||
|
|
||||
|
type Routes []Route |
||||
|
|
||||
|
var routes = Routes{ |
||||
|
Route{ |
||||
|
"Index", |
||||
|
"GET", |
||||
|
"/", |
||||
|
Index, |
||||
|
}, |
||||
|
} |
||||
|
|
||||
|
func Index(w http.ResponseWriter, r *http.Request) { |
||||
|
//ipFilter(w, r)
|
||||
|
fmt.Fprintln(w, runningPeer.ID) |
||||
|
} |