diff --git a/.gitignore b/.gitignore index 4850c8df..a01c9816 100644 --- a/.gitignore +++ b/.gitignore @@ -24,4 +24,6 @@ docker/.node_modules/* /data appveyor.yml .travis.yml -/public \ No newline at end of file +/.public +/vendor +/bin diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..87e0d271 --- /dev/null +++ b/Makefile @@ -0,0 +1,199 @@ +SHELL=/bin/bash + +# APP info +APP_NAME := logging-center +APP_VERSION := 1.0.0_SNAPSHOT +APP_CONFIG := $(APP_NAME).yml +APP_STATIC_FOLDER := .public +APP_UI_FOLDER := ui +APP_PLUGIN_FOLDER := plugin + +# Get release version from environment +ifneq "$(VERSION)" "" + APP_VERSION := $(VERSION) +endif + +# Ensure GOPATH is set before running build process. +ifeq "$(GOPATH)" "" + GOPATH := ~/go + #$(error Please set the environment variable GOPATH before running `make`) +endif + + +PATH := $(PATH):$(GOPATH)/bin + +# Go environment +CURDIR := $(shell pwd) +OLDGOPATH:= $(GOPATH) +NEWGOPATH:= $(CURDIR):$(CURDIR)/vendor:$(GOPATH) + +GO := GO15VENDOREXPERIMENT="1" GO111MODULE=off go +GOBUILD := GOPATH=$(NEWGOPATH) CGO_ENABLED=1 $(GO) build -ldflags -s -gcflags "-m -m" +GOBUILDNCGO := GOPATH=$(NEWGOPATH) CGO_ENABLED=0 $(GO) build -ldflags -s +GOTEST := GOPATH=$(NEWGOPATH) CGO_ENABLED=1 $(GO) test -ldflags -s + +ARCH := "`uname -s`" +LINUX := "Linux" +MAC := "Darwin" +GO_FILES=$(find . -iname '*.go' | grep -v /vendor/) +PKGS=$(go list ./... | grep -v /vendor/) + +INFINI_BASE_FOLDER := $(GOPATH)/src/infini.sh/ +FRAMEWORK_FOLDER := $(INFINI_BASE_FOLDER)/framework/ +FRAMEWORK_BRANCH := master +FRAMEWORK_VENDOR_FOLDER := $(CURDIR)/vendor/ +FRAMEWORK_VENDOR_BRANCH := master + +FRAMEWORK_OFFLINE_BUILD := "" +ifneq "$(OFFLINE_BUILD)" "" + FRAMEWORK_OFFLINE_BUILD := $(OFFLINE_BUILD) +endif + +.PHONY: all build update test clean + +default: build + +build: config + @#echo $(GOPATH) + @echo $(NEWGOPATH) + $(GOBUILD) -o bin/$(APP_NAME) + @$(MAKE) restore-generated-file + +build-cmd: config + @$(MAKE) restore-generated-file + +# used to build the binary for gdb debugging +build-race: clean config update-vfs + $(GOBUILD) -gcflags "-m -N -l" -race -o bin/$(APP_NAME) + @$(MAKE) restore-generated-file + +tar: build + cd bin && tar cfz ../bin/$(APP_NAME).tar.gz $(APP_NAME) $(APP_CONFIG) + +cross-build: clean config update-vfs + $(GO) test + GOOS=windows GOARCH=amd64 $(GOBUILD) -o bin/$(APP_NAME)-windows64.exe + GOOS=darwin GOARCH=amd64 $(GOBUILD) -o bin/$(APP_NAME)-darwin64 + GOOS=linux GOARCH=amd64 $(GOBUILD) -o bin/$(APP_NAME)-linux64 + @$(MAKE) restore-generated-file + + +build-win: + CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ GOOS=windows GOARCH=amd64 $(GOBUILD) -o bin/$(APP_NAME)-windows64.exe + CC=i686-w64-mingw32-gcc CXX=i686-w64-mingw32-g++ GOOS=windows GOARCH=386 $(GOBUILD) -o bin/$(APP_NAME)-windows32.exe + +build-linux: + GOOS=linux GOARCH=amd64 $(GOBUILD) -o bin/$(APP_NAME)-linux64 + GOOS=linux GOARCH=386 $(GOBUILD) -o bin/$(APP_NAME)-linux32 + GOOS=linux GOARCH=arm GOARM=5 $(GOBUILD) -o bin/$(APP_NAME)-armv5 + +build-darwin: + GOOS=darwin GOARCH=amd64 $(GOBUILD) -o bin/$(APP_NAME)-darwin64 + GOOS=darwin GOARCH=386 $(GOBUILD) -o bin/$(APP_NAME)-darwin32 + +build-bsd: + GOOS=freebsd GOARCH=amd64 $(GOBUILD) -o bin/$(APP_NAME)-freebsd64 + GOOS=freebsd GOARCH=386 $(GOBUILD) -o bin/$(APP_NAME)-freebsd32 + GOOS=netbsd GOARCH=amd64 $(GOBUILD) -o bin/$(APP_NAME)-netbsd64 + GOOS=netbsd GOARCH=386 $(GOBUILD) -o bin/$(APP_NAME)-netbsd32 + GOOS=openbsd GOARCH=amd64 $(GOBUILD) -o bin/$(APP_NAME)-openbsd64 + GOOS=openbsd GOARCH=386 $(GOBUILD) -o bin/$(APP_NAME)-openbsd32 + +all: clean config update-vfs cross-build restore-generated-file + +all-platform: clean config update-vfs cross-build-all-platform restore-generated-file + +cross-build-all-platform: clean config build-bsd build-linux build-darwin build-win restore-generated-file + +format: + go fmt $$(go list ./... | grep -v /vendor/) + +clean_data: + rm -rif dist + rm -rif data + rm -rif log + +clean: clean_data + rm -rif bin + mkdir bin + +init: + @echo building $(APP_NAME) $(APP_VERSION) + @echo $(CURDIR) + @mkdir -p $(INFINI_BASE_FOLDER) + @if [ ! -d $(FRAMEWORK_FOLDER) ]; then echo "framework does not exist";(cd $(INFINI_BASE_FOLDER)&&git clone -b $(FRAMEWORK_BRANCH) ssh://git@git.infini.ltd:64221/infini/framework.git) fi + @if [ ! -d $(FRAMEWORK_VENDOR_FOLDER) ]; then echo "framework vendor does not exist";(git clone -b $(FRAMEWORK_VENDOR_BRANCH) ssh://git@git.infini.ltd:64221/infini/framework-vendor.git vendor) fi + +update-generated-file: + @echo "update generated info" + @echo -e "package config\n\nconst LastCommitLog = \""`git log -1 --pretty=format:"%h, %ad, %an, %s"` "\"\nconst BuildDate = \"`date`\"" > config/generated.go + @echo -e "\nconst Version = \"$(APP_VERSION)\"" >> config/generated.go + + +restore-generated-file: + @echo "restore generated info" + @echo -e "package config\n\nconst LastCommitLog = \"N/A\"\nconst BuildDate = \"N/A\"" > config/generated.go + @echo -e "\nconst Version = \"0.0.1-SNAPSHOT\"" >> config/generated.go + + +update-vfs: + cd $(FRAMEWORK_FOLDER) && cd cmd/vfs && go build && cp vfs ~/ + @if [ -d $(APP_STATIC_FOLDER) ]; then echo "generate static files";(cd $(APP_STATIC_FOLDER) && ~/vfs -ignore="static.go|.DS_Store" -o static.go -pkg public . ) fi + +update-template-ui: + @if [ -d $(APP_UI_FOLDER) ]; then (echo "generate main UI pages";$(GO) get infini.sh/ego/cmd/ego;cd $(APP_UI_FOLDER)/ && ego) fi + @if [ -d $(APP_PLUGIN_FOLDER) ]; then (echo "generate plugin UI pages";$(GO) get infini.sh/ego/cmd/ego;cd $(APP_PLUGIN_FOLDER)/ && ego) fi + +#config: init update-vfs update-template-ui +config: init update-vfs update-template-ui update-generated-file + @echo "update configs" + @# $(GO) env + @mkdir -p bin + @cp $(APP_CONFIG) bin/$(APP_CONFIG) + + +dist: cross-build package + +dist-major-platform: all package + +dist-all-platform: all-platform package-all-platform + +package: + @echo "Packaging" + cd bin && tar cfz ../bin/darwin64.tar.gz darwin64 $(APP_CONFIG) + cd bin && tar cfz ../bin/linux64.tar.gz linux64 $(APP_CONFIG) + cd bin && tar cfz ../bin/windows64.tar.gz windows64 $(APP_CONFIG) + +package-all-platform: package-darwin-platform package-linux-platform package-windows-platform + @echo "Packaging all" + cd bin && tar cfz ../bin/freebsd64.tar.gz $(APP_NAME)-freebsd64 $(APP_CONFIG) + cd bin && tar cfz ../bin/freebsd32.tar.gz $(APP_NAME)-freebsd32 $(APP_CONFIG) + cd bin && tar cfz ../bin/netbsd64.tar.gz $(APP_NAME)-netbsd64 $(APP_CONFIG) + cd bin && tar cfz ../bin/netbsd32.tar.gz $(APP_NAME)-netbsd32 $(APP_CONFIG) + cd bin && tar cfz ../bin/openbsd64.tar.gz $(APP_NAME)-openbsd64 $(APP_CONFIG) + cd bin && tar cfz ../bin/openbsd32.tar.gz $(APP_NAME)-openbsd32 $(APP_CONFIG) + + +package-darwin-platform: + @echo "Packaging Darwin" + cd bin && tar cfz ../bin/darwin64.tar.gz $(APP_NAME)-darwin64 $(APP_CONFIG) + cd bin && tar cfz ../bin/darwin32.tar.gz $(APP_NAME)-darwin32 $(APP_CONFIG) + +package-linux-platform: + @echo "Packaging Linux" + cd bin && tar cfz ../bin/linux64.tar.gz $(APP_NAME)-linux64 $(APP_CONFIG) + cd bin && tar cfz ../bin/linux32.tar.gz $(APP_NAME)-linux32 $(APP_CONFIG) + cd bin && tar cfz ../bin/armv5.tar.gz $(APP_NAME)-armv5 $(APP_CONFIG) + +package-windows-platform: + @echo "Packaging Windows" + cd bin && tar cfz ../bin/windows64.tar.gz $(APP_NAME)-windows64.exe $(APP_CONFIG) + cd bin && tar cfz ../bin/windows32.tar.gz $(APP_NAME)-windows32.exe $(APP_CONFIG) + +test: + go get -u github.com/kardianos/govendor + go get github.com/stretchr/testify/assert + govendor test +local + go test -timeout 60s ./... + #GORACE="halt_on_error=1" go test ./... -race -timeout 120s --ignore ./vendor + #go test -bench=. -benchmem diff --git a/build.bat b/build.bat new file mode 100644 index 00000000..d1571dfa --- /dev/null +++ b/build.bat @@ -0,0 +1,2 @@ +set GOPATH=%CD%;%CD%/vendor;%GOPATH% +go build diff --git a/config/config.go b/config/config.go new file mode 100644 index 00000000..cbf93610 --- /dev/null +++ b/config/config.go @@ -0,0 +1,9 @@ +package config + +type AppConfig struct { + IndexName string `config:"index_name"` + ElasticConfig string `config:"elastic_config"` + UILocalPath string `config:"ui_path"` + UILocalEnabled bool `config:"ui_local"` + UIVFSEnabled bool `config:"ui_vfs"` +} diff --git a/config/generated.go b/config/generated.go new file mode 100644 index 00000000..b08b2d9c --- /dev/null +++ b/config/generated.go @@ -0,0 +1,6 @@ +package config + +const LastCommitLog = "N/A" +const BuildDate = "N/A" + +const Version = "0.0.1-SNAPSHOT" diff --git a/logging-center.yml b/logging-center.yml new file mode 100644 index 00000000..559cac98 --- /dev/null +++ b/logging-center.yml @@ -0,0 +1,20 @@ +elasticsearch: + - name: default + enabled: true + endpoint: http://localhost:9200 + index_prefix: infini- + basic_auth: + username: elastic + password: 13A1EcwWs1He6w3BXHuJ + +modules: + - name: web + enabled: true + network: + binding: 127.0.0.1:9000 + +logging-center: + index_name: reports + ui_path: .public + ui_vfs: true + ui_local: true diff --git a/main.go b/main.go new file mode 100644 index 00000000..4ee39016 --- /dev/null +++ b/main.go @@ -0,0 +1,75 @@ +package main + +import ( + "errors" + _ "expvar" + "infini.sh/framework" + "infini.sh/framework/core/env" + "infini.sh/framework/core/module" + "infini.sh/framework/modules" + "infini.sh/logging-center/config" +) + +var appConfig *config.AppConfig +var appUI *UI + +func main() { + + terminalHeader := (" __ _ __ ____ __ _ __ __ \n") + terminalHeader += (" / // |/ // __// // |/ // / \n") + terminalHeader += (" / // || // _/ / // || // / \n") + terminalHeader += ("/_//_/|_//_/ /_//_/|_//_/ \n") + terminalHeader += (" __ _ __ __ __ _ __ __\n") + terminalHeader += (" / / ,' \\ ,'_/ ,'_/ / // |/ /,'_/\n") + terminalHeader += (" / /_/ o |/ /_n/ /_n / // || // /_n\n") + terminalHeader += ("/___/|_,' |__,'|__,'/_//_/|_/ |__,'\n") + terminalHeader += (" __ ___ _ __ _____ ___ ___ \n") + terminalHeader += (" ,'_/ / _/ / |/ //_ _// _/ / o | \n") + terminalHeader += ("/ /_ / _/ / || / / / / _/ / ,' \n") + terminalHeader += ("|__//___//_/|_/ /_/ /___//_/`_\\ \n") + terminalHeader += (" \n") + + terminalFooter := (" __ _ __ ____ __ _ __ __ __ _____ __ \n") + terminalFooter += (" / // |/ // __// // |/ // / / //_ _// \\ \n") + terminalFooter += (" / // || // _/ / // || // / / /_ / / / o | \n") + terminalFooter += ("/_//_/|_//_/ /_//_/|_//_/() /___//_/ /__,' \n\n") + terminalFooter += ("©2020 INFINI.LTD, All Rights Reserved.\n") + + app := framework.NewApp("logging-center", "the easiest way to operate your own logging center.", + config.Version, config.LastCommitLog, config.BuildDate, terminalHeader, terminalFooter) + + app.Init(nil) + defer app.Shutdown() + + app.Start(func() { + + //load core modules first + modules.Register() + + appConfig = &config.AppConfig{ + ElasticConfig: "default", + UILocalPath: ".public", + UIVFSEnabled: true, + UILocalEnabled: true, + } + + ok, err := env.ParseConfig("logging-center", appConfig) + if err != nil { + panic(err) + } + if !ok { + panic(errors.New("config not exists")) + } + + //load web UI files + appUI = &UI{config: appConfig} + appUI.InitUI() + + //start each module, with enabled provider + module.Start() + + }, func() { + + }) + +} diff --git a/ui.go b/ui.go new file mode 100644 index 00000000..78b67684 --- /dev/null +++ b/ui.go @@ -0,0 +1,40 @@ +package main + +import ( + "fmt" + log "github.com/cihub/seelog" + "infini.sh/framework/core/api" + "infini.sh/framework/core/ui" + "infini.sh/framework/core/util" + "infini.sh/framework/core/vfs" + "infini.sh/logging-center/.public" + "infini.sh/logging-center/config" + "net/http" +) + +type UI struct { + api.Handler + config *config.AppConfig +} + +func (h UI) InitUI() { + + vfs.RegisterFS(public.StaticFS{StaticFolder: h.config.UILocalPath, TrimLeftPath: h.config.UILocalPath , CheckLocalFirst: h.config.UILocalEnabled, SkipVFS: !h.config.UIVFSEnabled}) + + ui.HandleUI("/", vfs.FileServer(vfs.VFS())) + + + ui.HandleUIFunc("/api/", func(w http.ResponseWriter, req *http.Request) { + log.Warn("api: ",req.URL," not implemented") + request, err := h.GetRawBody(req) + if err != nil { + fmt.Println(err) + return + } + + response:=map[string]interface{}{} + response["request"]=string(request) + + w.Write(util.ToJSONBytes(request)) + }) +}