Skip to content

Commit

Permalink
feat: init api & models
Browse files Browse the repository at this point in the history
  • Loading branch information
sundowndev committed Aug 12, 2020
1 parent e6cac63 commit cd0ed6b
Show file tree
Hide file tree
Showing 33 changed files with 5,288 additions and 3 deletions.
60 changes: 60 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//go:generate go run github.com/swaggo/swag/cmd/swag init -g ./api.go
package api

import (
"context"
"fmt"
"github.com/dreamvo/gilfoyle/api/db"
"github.com/dreamvo/gilfoyle/api/docs"
"github.com/dreamvo/gilfoyle/api/v1"
"github.com/gin-gonic/gin"
_ "github.com/mattn/go-sqlite3"
"github.com/swaggo/files"
"github.com/swaggo/gin-swagger"
"log"
)

// @license.name GNU General Public License v3.0
// @license.url https://github.com/dreamvo/gilfoyle/blob/master/LICENSE

// Serve runs a REST API web server
func Serve(port int) {
client, err := db.NewClient()
if err != nil {
log.Fatalf("failed opening connection to sqlite: %v", err)
}
defer client.Close()

// run the auto migration tool.
if err := client.Schema.Create(context.Background()); err != nil {
log.Fatalf("failed creating schema resources: %v", err)
}

docs.SwaggerInfo.Title = "Gilfoyle server"
docs.SwaggerInfo.Description = " Video streaming server backed by decentralized filesystem."
docs.SwaggerInfo.Version = "0.1"
docs.SwaggerInfo.Host = fmt.Sprintf("localhost:%d", port)
docs.SwaggerInfo.BasePath = "/"
docs.SwaggerInfo.Schemes = []string{"http", "https"}

r := gin.Default()

r.GET("/healthcheck", healthcheckHandler)

v1.Router(r)

// register swagger docs handler
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

// launch web server
_ = r.Run(fmt.Sprintf(":%d", port))
}

// Healthcheck godoc
// @Summary Check service status
// @Description get string by ID
// @Success 200
// @Router /healthcheck [get]
func healthcheckHandler(ctx *gin.Context) {
ctx.Status(200)
}
11 changes: 11 additions & 0 deletions api/db/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package db

import (
"github.com/dreamvo/gilfoyle/ent"
)

var Client *ent.Client

func NewClient() (*ent.Client, error) {
return ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
}
168 changes: 168 additions & 0 deletions api/docs/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// This file was generated by swaggo/swag

package docs

import (
"bytes"
"encoding/json"
"strings"

"github.com/alecthomas/template"
"github.com/swaggo/swag"
)

var doc = `{
"schemes": {{ marshal .Schemes }},
"swagger": "2.0",
"info": {
"description": "{{.Description}}",
"title": "{{.Title}}",
"contact": {},
"license": {
"name": "GNU General Public License v3.0",
"url": "https://github.com/dreamvo/gilfoyle/blob/master/LICENSE"
},
"version": "{{.Version}}"
},
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/healthcheck": {
"get": {
"description": "get string by ID",
"summary": "Check service status",
"responses": {
"200": {}
}
}
},
"/v1/videos": {
"get": {
"description": "get latest videos",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"summary": "Query videos",
"responses": {
"200": {
"description": "OK",
"schema": {
"allOf": [
{
"$ref": "#/definitions/v1.JSONResult"
},
{
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/ent.Video"
}
}
}
}
]
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/v1.JSONResult"
}
}
}
}
}
},
"definitions": {
"ent.Video": {
"type": "object",
"properties": {
"created_at": {
"type": "string"
},
"deleted_at": {
"type": "string"
},
"id": {
"type": "integer"
},
"status": {
"type": "string"
},
"title": {
"type": "string"
},
"updated_at": {
"type": "string"
},
"uuid": {
"type": "string"
}
}
},
"v1.JSONResult": {
"type": "object",
"properties": {
"data": {
"type": "object"
},
"message": {
"type": "string"
}
}
}
}
}`

type swaggerInfo struct {
Version string
Host string
BasePath string
Schemes []string
Title string
Description string
}

// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = swaggerInfo{
Version: "",
Host: "",
BasePath: "",
Schemes: []string{},
Title: "",
Description: "",
}

type s struct{}

func (s *s) ReadDoc() string {
sInfo := SwaggerInfo
sInfo.Description = strings.Replace(sInfo.Description, "\n", "\\n", -1)

t, err := template.New("swagger_info").Funcs(template.FuncMap{
"marshal": func(v interface{}) string {
a, _ := json.Marshal(v)
return string(a)
},
}).Parse(doc)
if err != nil {
return doc
}

var tpl bytes.Buffer
if err := t.Execute(&tpl, sInfo); err != nil {
return doc
}

return tpl.String()
}

func init() {
swag.Register(swag.Name, &s{})
}
101 changes: 101 additions & 0 deletions api/docs/swagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
{
"swagger": "2.0",
"info": {
"contact": {},
"license": {
"name": "GNU General Public License v3.0",
"url": "https://github.com/dreamvo/gilfoyle/blob/master/LICENSE"
}
},
"paths": {
"/healthcheck": {
"get": {
"description": "get string by ID",
"summary": "Check service status",
"responses": {
"200": {}
}
}
},
"/v1/videos": {
"get": {
"description": "get latest videos",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"summary": "Query videos",
"responses": {
"200": {
"description": "OK",
"schema": {
"allOf": [
{
"$ref": "#/definitions/v1.JSONResult"
},
{
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/ent.Video"
}
}
}
}
]
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/v1.JSONResult"
}
}
}
}
}
},
"definitions": {
"ent.Video": {
"type": "object",
"properties": {
"created_at": {
"type": "string"
},
"deleted_at": {
"type": "string"
},
"id": {
"type": "integer"
},
"status": {
"type": "string"
},
"title": {
"type": "string"
},
"updated_at": {
"type": "string"
},
"uuid": {
"type": "string"
}
}
},
"v1.JSONResult": {
"type": "object",
"properties": {
"data": {
"type": "object"
},
"message": {
"type": "string"
}
}
}
}
}
Loading

0 comments on commit cd0ed6b

Please sign in to comment.