package main

import (
	"log"
	"net"
	"os"

	"github.com/gin-gonic/gin"
)

var (
	GIN_SOCK_PATH = "/tmp/sshd-tool.sock"
)

func InitGin() net.Listener {
	os.RemoveAll(GIN_SOCK_PATH)
	sock, err := net.Listen("unix", GIN_SOCK_PATH)
	if err != nil {
		log.Fatalf("error listen unix socket %s: %v", GIN_SOCK_PATH, err)
	}
	err = os.Chmod(GIN_SOCK_PATH, 0666)
	if err != nil {
		sock.Close()
		log.Fatalf("error chmod of %s: %v", GIN_SOCK_PATH, err)
	}
	g := gin.Default()
	g.GET("/user", getLoginedUserInfo)
	go g.RunListener(sock)
	return sock
}
