Files
openlan/pkg/config/common.go
T
2026-01-22 15:35:11 +08:00

96 lines
1.7 KiB
Go
Executable File

package config
import (
"fmt"
"os"
"runtime"
"strings"
"github.com/luscis/openlan/pkg/libol"
)
var genindexs map[string]int
func init() {
genindexs = make(map[string]int, 32)
}
func GenName(name string) string {
index, ok := genindexs[name]
if !ok {
index = 1000
}
index += 1
genindexs[name] = index
return fmt.Sprintf("%s%d", name, index)
}
func VarDir(name ...string) string {
return "/var/openlan/" + strings.Join(name, "/")
}
type Log struct {
File string `json:"file,omitempty" yaml:"file,omitempty"`
Verbose int `json:"level,omitempty" yaml:"level,omitempty"`
}
func (l *Log) Correct() {
if l.Verbose == 0 {
l.Verbose = libol.INFO
}
}
func LogFile(file string) string {
if runtime.GOOS == "linux" {
return "/var/log/" + file
}
return file
}
type Http struct {
Listen string `json:"listen,omitempty" yaml:"listen,omitempty"`
Public string `json:"-" yaml:"-"`
}
func (h *Http) Correct() {
SetListen(&h.Listen, 10000)
if h.Public == "" {
h.Public = VarDir("public")
}
}
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 SetListen(listen *string, port int) {
if *listen == "" {
*listen = fmt.Sprintf("0.0.0.0:%d", port)
return
}
values := strings.SplitN(*listen, ":", 2)
if len(values) == 1 {
*listen = fmt.Sprintf("%s:%d", values[0], port)
}
}
func GetAlias() string {
if hostname, err := os.Hostname(); err == nil {
return strings.ToLower(hostname)
}
return libol.GenString(13)
}
func ListenAll(listen string) string {
values := strings.SplitN(listen, ":", 2)
if len(values) == 1 {
return fmt.Sprintf("0.0.0.0:%s", values[0])
}
return listen
}