package main
import (
"archive/zip"
"database/sql"
"fmt"
guuid "github.com/google/uuid"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"text/template"
_ "github.com/go-sql-driver/mysql"
)
type Script struct {
Id int
Name string
ScriptText string
Flag int
}
func dbConn() (db *sql.DB) {
dbDriver := "mysql"
dbUser := "root"
dbPass := "root"
dbName := "scripts"
db, err := sql.Open(dbDriver, dbUser+":"+dbPass+"@/"+dbName)
if err != nil {
panic(err.Error())
}
return db
}
var tmpl = template.Must(template.ParseGlob("form/*"))
func Index(w http.ResponseWriter, r *http.Request) {
db := dbConn()
selDB, err := db.Query("SELECT * FROM script ORDER BY id DESC")
if err != nil {
panic(err.Error())
}
script := Script{}
res := []Script{}
for selDB.Next() {
var id int
var name string
buf := make([]byte, 65535)
var flag int
err = selDB.Scan(&id, &name, &buf, &flag)
if err != nil {
panic(err.Error())
}
script.Id = id
script.Name = name
script.ScriptText = string(buf)
script.Flag = flag
log.Println(script)
res = append(res, script)
}
tmpl.ExecuteTemplate(w, "Index", res)
defer db.Close()
}
func Show(w http.ResponseWriter, r *http.Request) {
db := dbConn()
nId := r.URL.Query().Get("id")
selDB, err := db.Query("SELECT * FROM script WHERE id=?", nId)
if err != nil {
panic(err.Error())
}
script := Script{}
for selDB.Next() {
var id int
var name string
buf := make([]byte, 65535)
var flag int
err = selDB.Scan(&id, &name, &buf, &flag)
if err != nil {
panic(err.Error())
}
script.Id = id
script.Name = name
script.ScriptText = string(buf)
script.Flag = flag
}
tmpl.ExecuteTemplate(w, "Show", script)
defer db.Close()
}
func New(w http.ResponseWriter, r *http.Request) {
tmpl.ExecuteTemplate(w, "New", nil)
}
func Edit(w http.ResponseWriter, r *http.Request) {
db := dbConn()
nId := r.URL.Query().Get("id")
selDB, err := db.Query("SELECT * FROM script WHERE id=?", nId)
if err != nil {
panic(err.Error())
}
script := Script{}
for selDB.Next() {
var id int
var name string
buf := make([]byte, 65535)
var flag int
err = selDB.Scan(&id, &name, &buf, &flag)
if err != nil {
panic(err.Error())
}
script.Id = id
script.Name = name
script.ScriptText = string(buf)
script.Flag = flag
}
tmpl.ExecuteTemplate(w, "Edit", script)
defer db.Close()
}
func Insert(w http.ResponseWriter, r *http.Request) {
db := dbConn()
if r.Method == "POST" {
id := 0
name := r.FormValue("name")
script := r.FormValue("script")
flag := r.FormValue("flag")
insForm, err := db.Prepare("INSERT INTO script(id, name, script, flag) VALUES(?,?,?,?)")
if err != nil {
panic(err.Error())
}
insForm.Exec(id, name, script, flag)
log.Println("INSERT: Name: " + name + " | script: " + script)
}
defer db.Close()
http.Redirect(w, r, "/", 301)
}
func Update(w http.ResponseWriter, r *http.Request) {
db := dbConn()
if r.Method == "POST" {
name := r.FormValue("name")
script := r.FormValue("script")
id := r.FormValue("uid")
flag := r.FormValue("flag")
insForm, err := db.Prepare("UPDATE script SET name=?, script=? , flag=? WHERE id=?")
if err != nil {
panic(err.Error())
}
insForm.Exec(name, script, id, flag)
log.Println("UPDATE: Name: " + name + " | script: " + script)
}
defer db.Close()
http.Redirect(w, r, "/", 301)
}
func Delete(w http.ResponseWriter, r *http.Request) {
db := dbConn()
script := r.URL.Query().Get("id")
delForm, err := db.Prepare("DELETE FROM script WHERE id=?")
if err != nil {
panic(err.Error())
}
delForm.Exec(script)
log.Println("DELETE")
defer db.Close()
http.Redirect(w, r, "/", 301)
}
func Export(w http.ResponseWriter, r *http.Request) {
db := dbConn()
selDB, err := db.Query("SELECT * FROM script ORDER BY id DESC")
if err != nil {
panic(err.Error())
}
script := Script{}
res := []Script{}
files := []string{}
guid := guuid.NewString()
for selDB.Next() {
var id int
var name string
buf := make([]byte, 65535)
var flag int
err = selDB.Scan(&id, &name, &buf, &flag)
if err != nil {
panic(err.Error())
}
script.Id = id
script.Name = name
script.ScriptText = string(buf)
script.Flag = flag
writeFile(script.ScriptText, guid+"/"+name+".txt")
files = append(files, guid+"/"+name+".txt")
log.Println(script)
res = append(res, script)
}
defer db.Close()
output := guid + ".zip"
if err := ZipFiles(output, files); err != nil {
panic(err)
}
fmt.Println("Zipped File:", output)
http.ServeFile(w, r, output)
}
// writes message into a file
func writeFile(msg string, filename string) {
bytes := []byte(msg)
ioutil.WriteFile(filename, bytes, 0644)
}
func createFolder(location string) {
err := os.Mkdir("/Users/temp", 0755)
if err != nil {
panic(err.Error())
}
}
func ZipFiles(filename string, files []string) error {
newZipFile, err := os.Create(filename)
if err != nil {
return err
}
defer newZipFile.Close()
zipWriter := zip.NewWriter(newZipFile)
defer zipWriter.Close()
for _, file := range files {
if err = AddFileToZip(zipWriter, file); err != nil {
return err
}
}
return nil
}
func AddFileToZip(zipWriter *zip.Writer, filename string) error {
fileToZip, err := os.Open(filename)
if err != nil {
return err
}
defer fileToZip.Close()
info, err := fileToZip.Stat()
if err != nil {
return err
}
header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
header.Name = filename
header.Method = zip.Deflate
writer, err := zipWriter.CreateHeader(header)
if err != nil {
return err
}
_, err = io.Copy(writer, fileToZip)
return err
}
func main() {
log.Println("Server started on: http://localhost:8080")
http.HandleFunc("/", Index)
http.HandleFunc("/show", Show)
http.HandleFunc("/new", New)
http.HandleFunc("/edit", Edit)
http.HandleFunc("/insert", Insert)
http.HandleFunc("/update", Update)
http.HandleFunc("/delete", Delete)
http.HandleFunc("/export", Export)
http.ListenAndServe(":8444", nil)
log.Println("END")
}
No comments:
Post a Comment