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 ( import (
"database/sql" "database/sql"
"github.com/Redume/EveryNasa/functions"
"net/http" "net/http"
"github.com/Redume/EveryNasa/api/utils" "github.com/Redume/EveryNasa/api/utils"
"github.com/Redume/EveryNasa/functions"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
) )
var SettingsGet = func(w http.ResponseWriter, r *http.Request) { var SettingsGet = func(w http.ResponseWriter, r *http.Request) {
db, errOpen := sql.Open("sqlite3", "EveryNasa.db") db, err := sql.Open("sqlite3", "EveryNasa.db")
if errOpen != nil { if err != nil {
panic(errOpen) functions.Logger(err.Error())
} }
query, errQuery := db.Query("SELECT * FROM settings") query, err := db.Query("SELECT * FROM settings")
if errQuery != nil { if err != nil {
panic(errQuery) functions.Logger(err.Error())
} }
defer func(query *sql.Rows) { defer func(query *sql.Rows) {
err := query.Close() err := query.Close()
if err != nil { if err != nil {
panic(err) functions.Logger(err.Error())
} }
}(query) }(query)
var autostart, autochangewallpaper int var startup, autochangewallpaper int
for query.Next() { for query.Next() {
err := query.Scan(&autostart, &autochangewallpaper) err := query.Scan(&startup, &autochangewallpaper)
if err != nil { 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) utils.Respond(w, data)
} }
} }
var SettingsUpdate = func(w http.ResponseWriter, r *http.Request) { var SettingsUpdate = func(w http.ResponseWriter, r *http.Request) {
db, errOpen := sql.Open("sqlite3", "EveryNasa.db") db, err := sql.Open("sqlite3", "EveryNasa.db")
if errOpen != nil { if err != nil {
panic(errOpen) functions.Logger(err.Error())
} }
autostart := r.FormValue("autostart")
autochangewallpaper := r.FormValue("autochangewallpaper") autochangewallpaper := r.FormValue("autochangewallpaper")
startup := r.FormValue("startup")
if autostart == "" && autochangewallpaper == "" { if startup == "" && autochangewallpaper == "" {
utils.Respond(w, utils.Message(false, "All fields are required")) utils.Respond(w, utils.Message(false, "All fields are required."))
return return
} }
if autostart != "" {
_, err := db.Exec("UPDATE settings SET autostart = ?", autostart)
if err != nil {
panic(err)
}
}
if autochangewallpaper != "" { if autochangewallpaper != "" {
_, err := db.Exec("UPDATE settings SET autochangewallpaper = ?", autochangewallpaper) _, err := db.Exec("UPDATE settings SET autochangewallpaper = ?", autochangewallpaper)
if err != nil { if err != nil {
panic(err) functions.Logger(err.Error())
} }
if autochangewallpaper == "1" { 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 package controllers
import ( import (
"gopkg.in/yaml.v2"
"net/http" "net/http"
"os" "os"
"github.com/Redume/EveryNasa/api/utils" "github.com/Redume/EveryNasa/api/utils"
"github.com/Redume/EveryNasa/functions"
"gopkg.in/yaml.v2"
) )
var Version = func(w http.ResponseWriter, r *http.Request) { var Version = func(w http.ResponseWriter, r *http.Request) {
file, readErr := os.ReadFile("config.yaml") file, err := os.ReadFile("config.yaml")
if readErr != nil { if err != nil {
panic(readErr) functions.Logger(err.Error())
} }
data := make(map[interface{}]interface{}) data := make(map[interface{}]interface{})
marshalErr := yaml.Unmarshal(file, &data) err = yaml.Unmarshal(file, &data)
if marshalErr != nil { if err != nil {
panic(marshalErr) functions.Logger(err.Error())
} }
utils.Respond(w, utils.Message(true, data["version"].(string))) utils.Respond(w, utils.Message(true, data["version"].(string)))

View file

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

View file

@ -2,40 +2,42 @@ package functions
import ( import (
"database/sql" "database/sql"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
) )
func Database() { func Database() {
db, openErr := sql.Open("sqlite3", "EveryNasa.db") db, err := sql.Open("sqlite3", "EveryNasa.db")
if openErr != nil { if err != nil {
panic(openErr) Logger(err.Error())
} }
var exists bool var exists bool
QueryErr := db.QueryRow("SELECT EXISTS(SELECT name FROM sqlite_master WHERE type='table' AND name='settings')").Scan(&exists) err = db.QueryRow("SELECT EXISTS(SELECT name FROM sqlite_master WHERE type='table' AND name='settings')").Scan(&exists)
if QueryErr != nil { if err != nil {
panic(QueryErr) Logger(err.Error())
} }
if exists == false { if exists == false {
sqlTable := ` sqlTable := `
CREATE TABLE IF NOT EXISTS settings ( CREATE TABLE IF NOT EXISTS settings (
autostart INTEGER DEFAULT 0, startup INTEGER DEFAULT 0,
autochangewallpaper INTEGER DEFAULT 0 autochangewallpaper INTEGER DEFAULT 0
);` );`
_, CreateTableErr := db.Exec(sqlTable)
if CreateTableErr != nil { _, err = db.Exec(sqlTable)
panic(CreateTableErr) if err != nil {
Logger(err.Error())
} }
stmt, InsertErr := db.Prepare("INSERT INTO settings(autostart, autochangewallpaper) values(?,?)") stmt, err := db.Prepare("INSERT INTO settings(startup, autochangewallpaper) values(?, ?)")
if InsertErr != nil { if err != nil {
panic(InsertErr) Logger(err.Error())
} }
_, ExecErr := stmt.Exec(0, 0) _, err = stmt.Exec(0, 0)
if ExecErr != nil { if err != nil {
panic(ExecErr) 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 ( import (
"fmt" "fmt"
"os"
"github.com/getlantern/systray" "github.com/getlantern/systray"
"github.com/shirou/gopsutil/v3/process" "github.com/shirou/gopsutil/v3/process"
"github.com/skratchdot/open-golang/open" "github.com/skratchdot/open-golang/open"
"os"
) )
func Tray() { func Tray() {
@ -20,7 +21,7 @@ func Tray() {
case <-ui.ClickedCh: case <-ui.ClickedCh:
err := open.Run("http://localhost:4662") err := open.Run("http://localhost:4662")
if err != nil { if err != nil {
panic(err) Logger(err.Error())
} }
case <-exit.ClickedCh: case <-exit.ClickedCh:
Quit() Quit()
@ -31,7 +32,7 @@ func Tray() {
func Quit() { func Quit() {
err := KillProcess("EveryNasa.exe") err := KillProcess("EveryNasa.exe")
if err != nil { if err != nil {
panic(err) Logger(err.Error())
} }
systray.Quit() systray.Quit()
@ -40,13 +41,13 @@ func Quit() {
func KillProcess(name string) error { func KillProcess(name string) error {
processes, err := process.Processes() processes, err := process.Processes()
if err != nil { if err != nil {
panic(err) Logger(err.Error())
} }
for _, p := range processes { for _, p := range processes {
n, ProccErr := p.Name() n, err := p.Name()
if ProccErr != nil { if err != nil {
panic(ProccErr) Logger(err.Error())
} }
if n == name { if n == name {
@ -59,7 +60,7 @@ func KillProcess(name string) error {
func GetIcon(s string) []byte { func GetIcon(s string) []byte {
b, err := os.ReadFile(s) b, err := os.ReadFile(s)
if err != nil { if err != nil {
fmt.Print(err) Logger(err.Error())
} }
return b return b
} }