This commit is contained in:
Zheng Kai
2023-03-29 17:42:41 +08:00
parent 94b04a181a
commit 1b107ed035
36 changed files with 617 additions and 0 deletions

29
server/src/build/build.go Normal file
View File

@@ -0,0 +1,29 @@
package build
import "fmt"
// BuildGoVersion ...
var BuildGoVersion string
// BuildTime ...
var BuildTime string
// BuildType ...
var BuildType string
// BuildHost ...
var BuildHost string
// BuildGit ...
var BuildGit string
// DumpBuildInfo ...
func DumpBuildInfo() {
fmt.Println()
fmt.Println(BuildGoVersion)
fmt.Println(BuildTime)
fmt.Println(BuildType)
fmt.Println(BuildHost)
fmt.Println(BuildGit)
fmt.Println()
}

View File

@@ -0,0 +1,10 @@
package config
// config
var (
Prod bool
Dir string
MySQL = `user:pass@/dbname`
StaticDir = `/tmp`
)

22
server/src/config/init.go Normal file
View File

@@ -0,0 +1,22 @@
package config
import (
"os"
"path/filepath"
)
func init() {
Dir, _ = filepath.Abs(filepath.Dir(os.Args[0]))
list := map[string]*string{
`ORCA_MYSQL`: &MySQL,
`STATIC_DIR`: &StaticDir,
}
for k, v := range list {
s := os.Getenv(k)
if len(s) > 1 {
*v = s
}
}
}

9
server/src/go.mod Normal file
View File

@@ -0,0 +1,9 @@
module project
go 1.19
require github.com/zhengkai/zog v1.0.3
require github.com/zhengkai/life-go v1.0.3
require github.com/prometheus/client_golang v1.14.0 // indirect

6
server/src/go.sum Normal file
View File

@@ -0,0 +1,6 @@
github.com/prometheus/client_golang v1.14.0 h1:nJdhIvne2eSX/XRAFV9PcvFFRbrjbcTUj0VP62TMhnw=
github.com/prometheus/client_golang v1.14.0/go.mod h1:8vpkKitgIVNcqrRBWh1C4TIUQgYNtG/XQE4E/Zae36Y=
github.com/zhengkai/life-go v1.0.3 h1:rzm+Hb8H4He5trWx3lthFEQPf3sHpns0bDZ7vubT6sI=
github.com/zhengkai/life-go v1.0.3/go.mod h1:e2RGLfk+uRzjhRrMQash9X4iY3jAuGj99r0qj5JS7m4=
github.com/zhengkai/zog v1.0.3 h1:dkJdXJKRjbqqlseFycA1d80AUU6HAZrPe4WplpmwTo4=
github.com/zhengkai/zog v1.0.3/go.mod h1:dXbJ0XDMRXQX+XeNuIM9hJy/6OLRNtXHPq/86ll8u6I=

27
server/src/project.go Normal file
View File

@@ -0,0 +1,27 @@
package project
import (
"project/build"
"project/config"
"project/zj"
"github.com/zhengkai/life-go"
)
// Start ...
func Start() {
build.DumpBuildInfo()
zj.Init()
life.Wait()
}
// Prod ...
func Prod() {
config.Prod = true
Start()
}

33
server/src/web/server.go Normal file
View File

@@ -0,0 +1,33 @@
package web
import (
"fmt"
"net/http"
"project/zj"
"time"
)
// Server ...
func Server(port int) {
addr := fmt.Sprintf(`localhost:%d`, port)
mux := http.NewServeMux()
mux.HandleFunc(`/`, failbackHandle)
s := &http.Server{
Addr: addr,
Handler: mux,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 30 * time.Second,
}
zj.J(`start web server`, addr)
s.ListenAndServe()
}
func failbackHandle(w http.ResponseWriter, r *http.Request) {
zj.J(`failback handle`, r.URL.String())
}

48
server/src/zj/init.go Normal file
View File

@@ -0,0 +1,48 @@
package zj
import (
"path/filepath"
"project/config"
"github.com/zhengkai/zog"
)
// Init ...
func Init() {
mainCfg := zog.NewConfig()
mainCfg.Caller = zog.CallerLong
infoCfg := mainCfg.Clone()
infoCfg.Color = zog.ColorInfo
infoCfg.LinePrefix = `[IO] `
debugCfg := mainCfg.Clone()
debugCfg.Color = zog.ColorLight
debugCfg.LinePrefix = `[Debug] `
errCfg := zog.NewErrConfig()
errCfg.Color = zog.ColorWarn
errCfg.LinePrefix = `[Error] `
baseLog.CDefault = mainCfg
baseLog.CDebug = debugCfg
baseLog.CInfo = infoCfg
baseLog.CError = errCfg
baseLog.CWarn = errCfg
baseLog.CFatal = errCfg
baseLog.SetDirPrefix(filepath.Dir(zog.GetSourceFileDir()))
// 生产环境走 docker不写本地文件
if !config.Prod {
mainFile, _ := zog.NewFile(config.Dir+`/log/default.txt`, false)
infoFile, _ := zog.NewFile(config.Dir+`/log/io.txt`, false)
errFile, _ := zog.NewFile(config.Dir+`/log/err.txt`, true)
mainCfg.Output = append(mainCfg.Output, mainFile)
infoCfg.Output = append(infoCfg.Output, infoFile)
errCfg.Output = append(errCfg.Output, mainFile, errFile)
}
}

36
server/src/zj/log.go Normal file
View File

@@ -0,0 +1,36 @@
package zj
import "github.com/zhengkai/zog"
var baseLog = &zog.Logger{}
// J log
var J = baseLog.Println
// F log printf
var F = baseLog.Printf
// D debug log
var D = baseLog.Debugln
// DF debug printf
var DF = baseLog.Debugf
// W warn log
var W = baseLog.Warningln
// WF warn log
var WF = baseLog.Warningf
// IO ...
var IO = baseLog.Infoln
// IOF ...
var IOF = baseLog.Infof
// Watch ...
var Watch = baseLog.WatchStack
// N log nothing
func N(x ...interface{}) {
}