Files
openlan/pkg/config/common.go
2024-01-06 18:19:10 +08:00

62 lines
1.1 KiB
Go
Executable File

package config
import (
"fmt"
"os"
"runtime"
"strings"
"github.com/luscis/openlan/pkg/libol"
)
var index = 1024
func GenName(prefix string) string {
index += 1
return fmt.Sprintf("%s%d", prefix, index)
}
func VarDir(name ...string) string {
return "/var/openlan/" + strings.Join(name, "/")
}
type Log struct {
File string `json:"file,omitempty"`
Verbose int `json:"level,omitempty"`
}
func LogFile(file string) string {
if runtime.GOOS == "linux" {
return "/var/log/" + file
}
return file
}
type Http struct {
Listen string `json:"listen,omitempty"`
Public string `json:"public,omitempty"`
}
func CorrectAddr(listen *string, port int) {
values := strings.SplitN(*listen, ":", 2)
if len(values) == 1 {
*listen = fmt.Sprintf("%s:%d", values[0], port)
}
}
func (h *Http) GetUrl() string {
port := "10000"
values := strings.SplitN(h.Listen, ":", 2)
if len(values) == 2 {
port = values[1]
}
return "https://127.0.0.1:" + port
}
func GetAlias() string {
if hostname, err := os.Hostname(); err == nil {
return strings.ToLower(hostname)
}
return libol.GenString(13)
}