Start router
parent
368c25d03f
commit
7fced5532d
|
@ -0,0 +1,13 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"WorstCaptcha"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func main() {
|
||||
router := WorstCaptcha.NewRouter(WorstCaptcha.NewDb())
|
||||
|
||||
log.Fatal(http.ListenAndServe(":8080", router))
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
package WorstCaptcha
|
||||
|
||||
import (
|
||||
"github.com/gorilla/mux"
|
||||
"net/http"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
var GlobalDb Db
|
||||
|
||||
type Route struct {
|
||||
Name string
|
||||
Method string
|
||||
Pattern string
|
||||
Handler http.HandlerFunc
|
||||
}
|
||||
|
||||
type Routes []Route
|
||||
|
||||
func NewRouter(db Db) *mux.Router {
|
||||
router := mux.NewRouter().StrictSlash(true)
|
||||
|
||||
GlobalDb = db
|
||||
|
||||
for _, route := range routes {
|
||||
router.
|
||||
Methods(route.Method).
|
||||
Path(route.Pattern).
|
||||
Name(route.Name).
|
||||
Handler(route.Handler)
|
||||
}
|
||||
|
||||
return router
|
||||
}
|
||||
|
||||
var routes = Routes{
|
||||
Route{
|
||||
Name: "RequestCaptcha",
|
||||
Method: "GET",
|
||||
Pattern: "/v1/captcha/",
|
||||
Handler: RequestCaptcha,
|
||||
},
|
||||
}
|
||||
|
||||
type ResponseCaptcha struct {
|
||||
ImageLocation string `json:"image_location"`
|
||||
ResponseLocation string `json:"response_location"`
|
||||
}
|
||||
|
||||
func RequestCaptcha(w http.ResponseWriter, r *http.Request) {
|
||||
response := ResponseCaptcha{"aoeu", "asdf"}
|
||||
|
||||
// fmt.Println(GlobalDb)
|
||||
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
Loading…
Reference in New Issue