Logs added

This commit is contained in:
Данил 2022-09-25 11:04:32 +03:00
parent d84a153f7d
commit 35adbe07ff
6 changed files with 126 additions and 59 deletions

View file

@ -2,68 +2,61 @@ package controllers
import (
"database/sql"
"github.com/Redume/EveryNasa/functions"
"net/http"
"github.com/Redume/EveryNasa/api/utils"
"github.com/Redume/EveryNasa/functions"
_ "github.com/mattn/go-sqlite3"
)
var SettingsGet = func(w http.ResponseWriter, r *http.Request) {
db, errOpen := sql.Open("sqlite3", "EveryNasa.db")
if errOpen != nil {
panic(errOpen)
db, err := sql.Open("sqlite3", "EveryNasa.db")
if err != nil {
functions.Logger(err.Error())
}
query, errQuery := db.Query("SELECT * FROM settings")
if errQuery != nil {
panic(errQuery)
query, err := db.Query("SELECT * FROM settings")
if err != nil {
functions.Logger(err.Error())
}
defer func(query *sql.Rows) {
err := query.Close()
if err != nil {
panic(err)
functions.Logger(err.Error())
}
}(query)
var autostart, autochangewallpaper int
var startup, autochangewallpaper int
for query.Next() {
err := query.Scan(&autostart, &autochangewallpaper)
err := query.Scan(&startup, &autochangewallpaper)
if err != nil {
panic(err)
functions.Logger(err.Error())
}
var data = map[string]interface{}{"autostart": autostart, "autochangewallpaper": autochangewallpaper}
var data = map[string]interface{}{"startup": startup, "autochangewallpaper": autochangewallpaper}
utils.Respond(w, data)
}
}
var SettingsUpdate = func(w http.ResponseWriter, r *http.Request) {
db, errOpen := sql.Open("sqlite3", "EveryNasa.db")
if errOpen != nil {
panic(errOpen)
}
autostart := r.FormValue("autostart")
autochangewallpaper := r.FormValue("autochangewallpaper")
if autostart == "" && autochangewallpaper == "" {
utils.Respond(w, utils.Message(false, "All fields are required"))
return
}
if autostart != "" {
_, err := db.Exec("UPDATE settings SET autostart = ?", autostart)
db, err := sql.Open("sqlite3", "EveryNasa.db")
if err != nil {
panic(err)
functions.Logger(err.Error())
}
autochangewallpaper := r.FormValue("autochangewallpaper")
startup := r.FormValue("startup")
if startup == "" && autochangewallpaper == "" {
utils.Respond(w, utils.Message(false, "All fields are required."))
return
}
if autochangewallpaper != "" {
_, err := db.Exec("UPDATE settings SET autochangewallpaper = ?", autochangewallpaper)
if err != nil {
panic(err)
functions.Logger(err.Error())
}
if autochangewallpaper == "1" {
@ -71,5 +64,12 @@ var SettingsUpdate = func(w http.ResponseWriter, r *http.Request) {
}
}
utils.Respond(w, utils.Message(true, "Settings updated"))
if startup != "" {
_, err := db.Exec("UPDATE settings SET startup = ?", startup)
if err != nil {
functions.Logger(err.Error())
}
}
utils.Respond(w, utils.Message(true, "The settings have been applied successfully."))
}

View file

@ -1,24 +1,25 @@
package controllers
import (
"gopkg.in/yaml.v2"
"net/http"
"os"
"github.com/Redume/EveryNasa/api/utils"
"github.com/Redume/EveryNasa/functions"
"gopkg.in/yaml.v2"
)
var Version = func(w http.ResponseWriter, r *http.Request) {
file, readErr := os.ReadFile("config.yaml")
if readErr != nil {
panic(readErr)
file, err := os.ReadFile("config.yaml")
if err != nil {
functions.Logger(err.Error())
}
data := make(map[interface{}]interface{})
marshalErr := yaml.Unmarshal(file, &data)
if marshalErr != nil {
panic(marshalErr)
err = yaml.Unmarshal(file, &data)
if err != nil {
functions.Logger(err.Error())
}
utils.Respond(w, utils.Message(true, data["version"].(string)))

View file

@ -2,6 +2,7 @@ package utils
import (
"encoding/json"
"github.com/Redume/EveryNasa/functions"
"net/http"
)
@ -13,6 +14,6 @@ func Respond(w http.ResponseWriter, data map[string]interface{}) {
w.Header().Add("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(data)
if err != nil {
panic(err)
functions.Logger(err.Error())
}
}

View file

@ -2,40 +2,42 @@ package functions
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
func Database() {
db, openErr := sql.Open("sqlite3", "EveryNasa.db")
if openErr != nil {
panic(openErr)
db, err := sql.Open("sqlite3", "EveryNasa.db")
if err != nil {
Logger(err.Error())
}
var exists bool
QueryErr := db.QueryRow("SELECT EXISTS(SELECT name FROM sqlite_master WHERE type='table' AND name='settings')").Scan(&exists)
if QueryErr != nil {
panic(QueryErr)
err = db.QueryRow("SELECT EXISTS(SELECT name FROM sqlite_master WHERE type='table' AND name='settings')").Scan(&exists)
if err != nil {
Logger(err.Error())
}
if exists == false {
sqlTable := `
CREATE TABLE IF NOT EXISTS settings (
autostart INTEGER DEFAULT 0,
startup INTEGER DEFAULT 0,
autochangewallpaper INTEGER DEFAULT 0
);`
_, CreateTableErr := db.Exec(sqlTable)
if CreateTableErr != nil {
panic(CreateTableErr)
_, err = db.Exec(sqlTable)
if err != nil {
Logger(err.Error())
}
stmt, InsertErr := db.Prepare("INSERT INTO settings(autostart, autochangewallpaper) values(?,?)")
if InsertErr != nil {
panic(InsertErr)
stmt, err := db.Prepare("INSERT INTO settings(startup, autochangewallpaper) values(?, ?)")
if err != nil {
Logger(err.Error())
}
_, ExecErr := stmt.Exec(0, 0)
if ExecErr != nil {
panic(ExecErr)
_, err = stmt.Exec(0, 0)
if err != nil {
Logger(err.Error())
}
}
}

62
functions/logger.go Normal file
View file

@ -0,0 +1,62 @@
package functions
import (
"os"
"runtime"
"time"
)
func Logger(text string) {
if !FileExists("logger.log") {
err := CreateFile("logger.log")
if err != nil {
panic(err)
}
}
f, err := os.OpenFile("logger.log", os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
panic(err)
}
now := time.Now()
//what file name is it
_, file, _, ok := runtime.Caller(1)
if !ok {
file = "???"
}
dir, err := os.Getwd()
if err != nil {
Logger(err.Error())
}
file = file[len(dir)+1:]
_, err = f.Write([]byte(now.Format("Mon Jan 2 15:04:05 2006") + " | " + text + " [" + file + "] " + "\n"))
if err != nil {
panic(err)
}
}
func FileExists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
func CreateFile(name string) error {
fo, err := os.Create(name)
if err != nil {
return err
}
defer func() {
err := fo.Close()
if err != nil {
return
}
}()
return nil
}

View file

@ -2,10 +2,11 @@ package functions
import (
"fmt"
"os"
"github.com/getlantern/systray"
"github.com/shirou/gopsutil/v3/process"
"github.com/skratchdot/open-golang/open"
"os"
)
func Tray() {
@ -20,7 +21,7 @@ func Tray() {
case <-ui.ClickedCh:
err := open.Run("http://localhost:4662")
if err != nil {
panic(err)
Logger(err.Error())
}
case <-exit.ClickedCh:
Quit()
@ -31,7 +32,7 @@ func Tray() {
func Quit() {
err := KillProcess("EveryNasa.exe")
if err != nil {
panic(err)
Logger(err.Error())
}
systray.Quit()
@ -40,13 +41,13 @@ func Quit() {
func KillProcess(name string) error {
processes, err := process.Processes()
if err != nil {
panic(err)
Logger(err.Error())
}
for _, p := range processes {
n, ProccErr := p.Name()
if ProccErr != nil {
panic(ProccErr)
n, err := p.Name()
if err != nil {
Logger(err.Error())
}
if n == name {
@ -59,7 +60,7 @@ func KillProcess(name string) error {
func GetIcon(s string) []byte {
b, err := os.ReadFile(s)
if err != nil {
fmt.Print(err)
Logger(err.Error())
}
return b
}