Ejemplo n.º 1
0
func setSwaggerHost(swspec *spec.Swagger, lines []string) error {
	lns := lines
	if len(lns) == 0 || (len(lines) == 1 && len(lines[0]) == 0) {
		lns = []string{"localhost"}
	}
	swspec.Host = lns[0]
	return nil
}
Ejemplo n.º 2
0
func (g *Generator) generateSwagger() (*spec.Swagger, error) {
	contact := &spec.ContactInfo{
		Name:  "API Support",
		URL:   "api-suport.url",
		Email: "*****@*****.**",
	}
	license := &spec.License{
		Name: "APACHE-2.0",
		URL:  "url://to/license",
	}

	info := new(spec.Info)
	info.Description = "API description"
	info.Title = "API title"
	info.TermsOfService = "url://to/tos"
	info.Contact = contact
	info.License = license
	info.Version = "v1"

	sw := new(spec.Swagger)
	sw.Swagger = "2.0"
	sw.Info = info
	// sw.Consumes = []string{"application/x-www-form-urlencoded", "application/json", "application/x-protobuf"}
	// sw.Produces = []string{"application/json", "application/x-protobuf"}
	// TODO(ceram1): Make this configurable.
	sw.BasePath = "/api/"
	sw.Host = "authagain.appspot.com"
	sw.Paths = new(spec.Paths)
	sw.Paths.Paths = make(map[string]spec.PathItem)
	sw.Definitions = make(map[string]spec.Schema)

	sw.Schemes = []string{"https", "http"}

	for _, svc := range g.file.Services {
		info.Title = svc.GetName() + " API"
		if cmt := g.file.GetCommentText(svc.CommentPath); cmt != nil {
			info.Description = *cmt
		}

		for _, mtd := range svc.Methods {
			for _, b := range mtd.Bindings {
				var p *spec.PathItem
				path := buildPath(b.PathTmpl)

				if pi, ok := sw.Paths.Paths[path]; !ok {
					p = new(spec.PathItem)
				} else {
					p = &pi
				}

				op := g.generateOperation(b)

				if b.HTTPMethod == "GET" {
					p.Get = op
				} else if b.HTTPMethod == "POST" {
					p.Post = op
				} else if b.HTTPMethod == "PUT" {
					p.Put = op
				} else if b.HTTPMethod == "DELETE" {
					p.Delete = op
				} else {
					return nil, fmt.Errorf("Unknown http method `%v`", b.HTTPMethod)
				}

				sw.Paths.Paths[path] = *p
			}
		}
	}

	for _, msg := range g.file.Messages {
		def := g.generateDefinition(msg)
		sw.Definitions[def.Title] = *def
	}
	sw.Info = info

	return sw, nil
}