Skip to content

Instantly share code, notes, and snippets.

@YagmurOzden
Created December 1, 2022 06:03
Show Gist options
  • Save YagmurOzden/333b79e5edfb39463fd21b7e1d72aea9 to your computer and use it in GitHub Desktop.
Save YagmurOzden/333b79e5edfb39463fd21b7e1d72aea9 to your computer and use it in GitHub Desktop.
API controller using via fiber and GOLang
package controller
import (
model "Read-From-Github/model"
"bytes"
"encoding/json"
"io"
"log"
"os"
"net/http"
"time"
"github.com/gofiber/fiber/v2"
"github.com/joho/godotenv"
"gopkg.in/src-d/go-billy.v4"
"gopkg.in/src-d/go-billy.v4/memfs"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/storage/memory"
)
const (
HOST = "http://127.0.0.1"
APINAME = "Github Driver"
)
func CreateVms(c *fiber.Ctx) error {
log.Printf("%v Started CreateVMs Request", APINAME)
data, err := CreateVMArray(c)
if err != nil {
log.Printf("%v Cannot create vm array. Error: %v", APINAME, err.Error())
return c.Status(http.StatusBadRequest).JSON(&fiber.Map{"data": err})
} else {
log.Printf("%v Successfullu finished operation 'CreateVms' ", APINAME)
return c.Status(http.StatusOK).JSON(&fiber.Map{"status": http.StatusOK, "data": data, "message": "success"})
}
}
func CreateVMArray(c *fiber.Ctx) ([]model.VMs, error) {
log.Printf("%v Started to create vm array ", APINAME)
//first input is the URL and the second is the filename
jsonBody := TakeDataFromgithub("https://"+GetEnv("UserName")+":"+GetEnv("GithubToken")+GetEnv("Path"), GetEnv("FileName2"))
log.Printf("%v Json body : %v", APINAME, jsonBody)
//VM Array
var vms []model.VMs
err := json.Unmarshal([]byte(jsonBody), &vms)
if err != nil {
log.Printf("%v Cannot unmrshall json boy to vm. Error: %v", APINAME, err.Error())
}
log.Printf("%v All vms are created", APINAME)
return vms, nil
}
func CreateVm(c *fiber.Ctx) error {
log.Printf("%v Started to create single Vm", APINAME)
data, err := Create(c)
if err != nil {
log.Printf("%v Cannot create vm. Error: %v", APINAME, err.Error())
return c.Status(http.StatusBadRequest).JSON(&fiber.Map{"data": err})
} else {
log.Printf("%v Successfully created vm : %v", APINAME, data)
return c.Status(http.StatusOK).JSON(&fiber.Map{"status": http.StatusOK, "data": data, "message": "success"})
}
}
var client = http.Client{
Timeout: 60 * time.Second,
}
func Create(c *fiber.Ctx) (model.VMs, error) {
log.Printf("%v 'Create Request' has been started", APINAME)
//first input is the URL and the second is the filename
strJsonBody := TakeDataFromgithub("https://"+GetEnv("UserName")+":"+GetEnv("GithubToken")+GetEnv("Path"), GetEnv("FileName"))
jsonBody := []byte(strJsonBody)
var vm model.VMs
err := json.Unmarshal(jsonBody, &vm)
if err != nil {
log.Printf("%v Cannot unmarshall jsonbody. Error: %v", APINAME, err.Error())
}
log.Printf("%v 'Body: %v", APINAME, strJsonBody)
return vm, nil
}
// Reads the env values
func GetEnv(variable string) string {
log.Printf("%v Loading data from environemnt file", APINAME)
err := godotenv.Load("local.env")
if err != nil {
log.Printf("%v Cannot load data from env file. Error: %v", APINAME, err.Error())
}
return os.Getenv(variable)
}
// For reading files that are bytes
func ParseData(file billy.File) string {
log.Printf("%v Started to parse file data to string", APINAME)
buf := make([]byte, 1)
var data string
for {
n, err := file.Read(buf)
if err == io.EOF {
break
}
if err != nil {
log.Printf("%v Cannot read the file. Error: %v", APINAME, err.Error())
continue
}
if n > 0 {
data += string(buf[:n])
}
}
return data
}
// Takes data from github and returns the value as string
func TakeDataFromgithub(URL string, FileName string) string {
log.Printf("%v Started to fetch data from Github", APINAME)
fs := memfs.New()
//Authenticate and clone the repository
repo, err := git.Clone(memory.NewStorage(), fs, &git.CloneOptions{
URL: URL,
})
if err != nil {
log.Printf("%v Repo could not find. Error: %v", APINAME, err.Error())
} else {
log.Printf("%v Fetched Repository : %v", APINAME, repo)
}
//Reads the repository as byte
file, err := fs.Open(FileName)
if err != nil {
log.Printf("%v File could not find. Error: %v", APINAME, err.Error())
} else {
log.Printf("%v Fetched Repositories File : %v", APINAME, file.Name())
}
defer func(file billy.File) {
err := file.Close()
if err != nil {
log.Printf("%v Cannot close file. Error: %v", APINAME, err.Error())
}
}(file)
//turns into a string
return ParseData(file)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment