Skip to content

Commit

Permalink
DOCKER_SOCKET_PATH usage,
Browse files Browse the repository at this point in the history
better env vars defaults/errors handling
  • Loading branch information
LbP22 committed Nov 1, 2023
1 parent 1babd94 commit efa5fb9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
9 changes: 6 additions & 3 deletions application/backend/app/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func validateMessage(message string) (string, bool) {
}

func createConnection(containerName string) net.Conn {
connection, _ := net.Dial("unix", "/var/run/docker.sock")
connection, _ := net.Dial("unix", os.Getenv("DOCKER_SOCKET_PATH"))
fmt.Fprintf(
connection,
"GET /containers/"+containerName+"/logs?stdout=true&stderr=true&timestamps=true&follow=true&since="+strconv.FormatInt(time.Now().Add(-5*time.Second).Unix(), 10)+" HTTP/1.0\r\n\r\n",
Expand Down Expand Up @@ -146,15 +146,18 @@ func CreateDaemonToDBStream(containerName string) {
}

if time.Now().Unix()-lastSleep > 1 {
time.Sleep(5 * time.Millisecond)
time.Sleep(10 * time.Millisecond)
lastSleep = time.Now().Unix()
}
}
}

// make request to docker socket
func makeSocketRequest(path string) []byte {
connection, _ := net.Dial("unix", "/var/run/docker.sock")
connection, err := net.Dial("unix", os.Getenv("DOCKER_SOCKET_PATH"))
if err != nil {
panic(err)
}
fmt.Fprintf(connection, "GET /"+path+" HTTP/1.0\r\n\r\n")

body, _ := ioutil.ReadAll(connection)
Expand Down
1 change: 0 additions & 1 deletion application/backend/app/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ func ReplacePrefixVariableForFrontend() {
fmt.Println("INFO: unable to find 'dist' folder")
return
}
fmt.Println("INFO: base onlogs prefix is: ", "\""+os.Getenv("ONLOGS_PATH_PREFIX")+"\"")
for _, file := range files {
if file.IsDir() {
dir_files, _ := os.ReadDir("dist/" + file.Name())
Expand Down
31 changes: 22 additions & 9 deletions application/backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,29 @@ import (
"github.com/joho/godotenv"
)

func init_config() {
if os.Getenv("PORT") == "" {
os.Setenv("PORT", "2874")
}

if os.Getenv("JWT_SECRET") == "" {
token, err := os.ReadFile("leveldb/JWT_secret")
if err != nil {
os.WriteFile("leveldb/JWT_secret", []byte(os.Getenv("JWT_SECRET")), 0700)
token, _ = os.ReadFile("leveldb/JWT_secret")
}
os.Setenv("JWT_SECRET", string(token))
}

if os.Getenv("DOCKER_SOCKET_PATH") == "" {
os.Setenv("DOCKER_SOCKET_PATH", "/var/run/docker.sock")
}
fmt.Println("INFO: OnLogs configs done!")
}

func main() {
godotenv.Load(".env")
init_config()
if os.Getenv("AGENT") != "" {
streamer.StreamLogs()
}
Expand All @@ -24,15 +45,6 @@ func main() {
util.ReplacePrefixVariableForFrontend()
util.CreateInitUser()

if os.Getenv("JWT_SECRET") == "" {
token, err := os.ReadFile("leveldb/JWT_secret")
if err != nil {
os.WriteFile("leveldb/JWT_secret", []byte(os.Getenv("JWT_SECRET")), 0700)
token, _ = os.ReadFile("leveldb/JWT_secret")
}
os.Setenv("JWT_SECRET", string(token))
}

pathPrefix := os.Getenv("ONLOGS_PATH_PREFIX")
http.HandleFunc(pathPrefix+"/", routes.Frontend)
http.HandleFunc(pathPrefix+"/api/v1/addHost", routes.AddHost)
Expand Down Expand Up @@ -65,5 +77,6 @@ func main() {
http.HandleFunc(pathPrefix+"/api/v1/logout", routes.Logout)
http.HandleFunc(pathPrefix+"/api/v1/updateUserSettings", routes.UpdateUserSettings)

fmt.Println("Listening on port:", string(os.Getenv("PORT"))+"...")
fmt.Println("ONLOGS: ", http.ListenAndServe(":"+string(os.Getenv("PORT")), nil))
}

0 comments on commit efa5fb9

Please sign in to comment.