init commit
This commit is contained in:
+51
-5
@@ -1,24 +1,70 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"encoding/base64"
|
||||
"github.com/gin-gonic/gin"
|
||||
"log"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type SignReq struct {
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
type User struct {
|
||||
Id string `json:"id"`
|
||||
DisplayName string `json:"displayName"`
|
||||
PhotoURL string `json:"photoURL"`
|
||||
PhoneNumber string `json:"phoneNumber"`
|
||||
Country string `json:"country"`
|
||||
Address string `json:"address"`
|
||||
State string `json:"state"`
|
||||
City string `json:"city"`
|
||||
ZipCode string `json:"zipCode"`
|
||||
About string `json:"about"`
|
||||
Role string `json:"role"`
|
||||
IsPublic bool `json:"isPublic"`
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
func Sign(c *gin.Context) {
|
||||
var req SignReq
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
}
|
||||
jwt.
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"data": "hello world",
|
||||
token, _ := GenToken(req.Email)
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"accessToken": token,
|
||||
"user": User{},
|
||||
})
|
||||
log.Println(req)
|
||||
}
|
||||
|
||||
type Token struct {
|
||||
Token string `json:"token"`
|
||||
AccessToken string `json:"access_token"`
|
||||
ExpiresIn int `json:"expires_in"`
|
||||
IssuedAt string `json:"issued_at"`
|
||||
}
|
||||
|
||||
func DefaultToken(ip string) Token {
|
||||
acc := sha1.Sum([]byte(ip))
|
||||
access := base64.StdEncoding.EncodeToString(acc[:])
|
||||
return Token{
|
||||
Token: access,
|
||||
AccessToken: access,
|
||||
ExpiresIn: 300,
|
||||
IssuedAt: time.Now().Format("2006-01-02T15:04:05.999999999Z"),
|
||||
}
|
||||
}
|
||||
func GenToken(u string) (string, error) {
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
||||
"user": u,
|
||||
"nbf": time.Now().Unix(),
|
||||
"exp": time.Now().Add(time.Minute * 300).Unix(),
|
||||
})
|
||||
|
||||
return token.SignedString([]byte("fasthub-center"))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package containers
|
||||
|
||||
import (
|
||||
"floares/lib/http/containers"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func ListContainers(c *gin.Context) {
|
||||
container := containers.List()
|
||||
c.JSON(200, gin.H{
|
||||
"data": container,
|
||||
})
|
||||
}
|
||||
|
||||
func CreateContainer(c *gin.Context) {
|
||||
var create containers.Create
|
||||
if err := c.BindJSON(&create); err != nil {
|
||||
c.JSON(200, gin.H{
|
||||
"data": nil,
|
||||
"error": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
id, err := create.Create()
|
||||
if err != nil {
|
||||
c.JSON(200, gin.H{
|
||||
"data": nil,
|
||||
"error": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
c.JSON(200, gin.H{
|
||||
"data": id,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package containers
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func RegisterRouter(r *gin.RouterGroup) {
|
||||
r.GET("/all", ListContainers)
|
||||
r.POST("/create", CreateContainer)
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package images
|
||||
|
||||
import (
|
||||
"floares/lib/http/images"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func ListImages(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"data": images.List(),
|
||||
"code": http.StatusOK,
|
||||
})
|
||||
}
|
||||
func PullImage(c *gin.Context) {
|
||||
type Ref struct {
|
||||
Ref string `json:"ref"`
|
||||
}
|
||||
var ref Ref
|
||||
if err := c.ShouldBind(&ref); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": http.StatusBadRequest,
|
||||
"data": gin.H{
|
||||
"err": err.Error(),
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"data": images.Pull(ref.Ref),
|
||||
"code": http.StatusOK,
|
||||
})
|
||||
}
|
||||
|
||||
func Delete(c *gin.Context) {
|
||||
result := images.Delete(c.Param("id"))
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": http.StatusOK,
|
||||
"err": result.Error,
|
||||
})
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package images
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func RegisterRouter(c *gin.RouterGroup) {
|
||||
c.GET("/", ListImages)
|
||||
c.POST("/pull", PullImage)
|
||||
c.DELETE("/:id", Delete)
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package network
|
||||
|
||||
import (
|
||||
"floares/lib/http/network"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func ListNetwork(c *gin.Context) {
|
||||
net := network.ListNetwork()
|
||||
c.JSON(200, gin.H{
|
||||
"data": net,
|
||||
"code": 200,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package network
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func RegisterRouter(r *gin.RouterGroup) {
|
||||
r.GET("/", ListNetwork)
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"floares/lib/http/network"
|
||||
"floares/lib/http/system"
|
||||
"floares/lib/http/volume"
|
||||
"floares/lib/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func BasicInfo(c *gin.Context) {
|
||||
bas := system.Info()
|
||||
version := system.Version()
|
||||
c.JSON(200, model.BasicInfo{
|
||||
Uptime: bas.Uptime,
|
||||
DefaultRuntime: bas.DefaultRuntime,
|
||||
NCPU: bas.NCPU,
|
||||
MemTotal: bas.MemTotal,
|
||||
OperatingSystem: bas.OperatingSystem,
|
||||
Images: bas.Images,
|
||||
Containers: bas.Containers,
|
||||
Platform: version.Platform,
|
||||
Volumes: len(volume.ListJsons().Volumes),
|
||||
Networks: len(network.ListJsons()),
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package system
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func RegisterRouter(r *gin.RouterGroup) {
|
||||
r.GET("/ping", BasicInfo)
|
||||
r.GET("/basic", BasicInfo)
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package volume
|
||||
|
||||
import (
|
||||
"floares/lib/http/volume"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func List(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"data": volume.ListJsons(),
|
||||
"code": http.StatusOK,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package volume
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func RegisterRouter(c *gin.RouterGroup) {
|
||||
c.GET("/", List)
|
||||
}
|
||||
Reference in New Issue
Block a user