func main() { vertex.ReadConfigs() logging.SetMinimalLevelByName(vertex.Config.Server.LoggingLevel) srv := vertex.NewServer(vertex.Config.Server.ListenAddr) srv.InitAPIs() if err := srv.Run(); err != nil { panic(err) } }
func TestSwagger(t *testing.T) { //t.SkipNow() a := &vertex.API{ Name: "testung", Version: "1.0", Doc: "Our fancy testung API", Title: "Testung API!", Middleware: middleware.DefaultMiddleware, Renderer: vertex.JSONRenderer{}, AllowInsecure: true, Routes: vertex.Routes{ { Path: "/user/{id}", Description: "Get User Info by id or name", Handler: UserHandler{}, Methods: vertex.GET, }, { Path: "/func/handler", Description: "Test handling by a pure func", Methods: vertex.POST, Handler: vertex.HandlerFunc(func(w http.ResponseWriter, r *vertex.Request) (interface{}, error) { return "WAT WAT", nil }), }, }, } srv := vertex.NewServer(":9947") srv.AddAPI(a) s := httptest.NewServer(srv.Handler()) defer s.Close() u := fmt.Sprintf("http://%s%s", s.Listener.Addr().String(), a.FullPath("/swagger")) t.Log(u) res, err := http.Get(u) if err != nil { t.Errorf("Could not get swagger data") } defer res.Body.Close() // b, err := ioutil.ReadAll(res.Body) // fmt.Println(string(b)) var sw swagger.API dec := json.NewDecoder(res.Body) if err = dec.Decode(&sw); err != nil { t.Errorf("Could not decode swagger def: %s", err) } swexp := a.ToSwagger(s.Listener.Addr().String()) assertEqual(t, sw.Basepath, swexp.Basepath) assertEqual(t, sw.Consumes, swexp.Consumes) assertEqual(t, sw.Host, swexp.Host) assertEqual(t, sw.Info, swexp.Info) assertEqual(t, sw.Produces, swexp.Produces) assertEqual(t, sw.Schemes, swexp.Schemes) assertEqual(t, sw.SwaggerVersion, swexp.SwaggerVersion) for k, v := range swexp.Paths { v2 := sw.Paths[k] assertEqual(t, v, v2, "Path mismatch \n%#v\n%#v") } //fmt.Println(sw) }