Browse Source

Assemble base skeleton

Vova Tkach 6 years ago
parent
commit
7550b663aa

+ 17 - 0
Gopkg.lock

@@ -0,0 +1,17 @@
+# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
+
+
+[[projects]]
+  digest = "1:32c86e5ce15a42e48631f1384891c840f6067333f4735b3e904a462d14344114"
+  name = "github.com/vladimirok5959/golang-server-bootstrap"
+  packages = ["bootstrap"]
+  pruneopts = "UT"
+  revision = "1afc55739623b0d65c665abaa71624b4b48fa0df"
+  version = "v1.0.0"
+
+[solve-meta]
+  analyzer-name = "dep"
+  analyzer-version = 1
+  input-imports = ["github.com/vladimirok5959/golang-server-bootstrap/bootstrap"]
+  solver-name = "gps-cdcl"
+  solver-version = 1

+ 30 - 0
Gopkg.toml

@@ -0,0 +1,30 @@
+# Gopkg.toml example
+#
+# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
+# for detailed Gopkg.toml documentation.
+#
+# required = ["github.com/user/thing/cmd/thing"]
+# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
+#
+# [[constraint]]
+#   name = "github.com/user/project"
+#   version = "1.0.0"
+#
+# [[constraint]]
+#   name = "github.com/user/project2"
+#   branch = "dev"
+#   source = "github.com/myfork/project2"
+#
+# [[override]]
+#   name = "github.com/x/y"
+#   version = "2.4.0"
+#
+# [prune]
+#   non-go = false
+#   go-tests = true
+#   unused-packages = true
+
+
+[prune]
+  go-tests = true
+  unused-packages = true

+ 5 - 0
consts/consts.go

@@ -0,0 +1,5 @@
+package consts
+
+const Debug = !false
+const ServerVersion = "1.0.1"
+const AssetsVersion = "1"

+ 178 - 0
main.go

@@ -0,0 +1,178 @@
+package main
+
+import (
+	"flag"
+	"fmt"
+	"net/http"
+
+	"golang-fave/consts"
+	"golang-fave/utils"
+
+	"github.com/vladimirok5959/golang-server-bootstrap/bootstrap"
+	/*
+		"context"
+		"errors"
+		"flag"
+		"fmt"
+		"log"
+		"net/http"
+		"os"
+		"os/signal"
+		"strconv"
+		"strings"
+		"time"
+
+		"golang-fave/constants"
+		"golang-fave/engine/actions"
+		"golang-fave/engine/wrapper"
+	*/)
+
+var ParamHost string
+var ParamPort int
+var ParamWwwDir string
+
+//var VhostHomeDir string
+
+func init() {
+	flag.StringVar(&ParamHost, "host", "0.0.0.0", "server host")
+	flag.IntVar(&ParamPort, "port", 8080, "server port")
+	flag.StringVar(&ParamWwwDir, "dir", "", "virtual hosts directory")
+	flag.Parse()
+}
+
+func main() {
+	ParamWwwDir = utils.FixPath(ParamWwwDir)
+	if !(utils.IsFileExists(ParamWwwDir) && utils.IsDir(ParamWwwDir)) {
+		fmt.Println("Virtual hosts directory is not exists")
+		fmt.Println("Example: ./fave -host 127.0.0.1 -port 80 -dir ./hosts")
+		return
+	}
+
+	bootstrap.Start("127.0.0.1:8080", 30, "assets", func(w http.ResponseWriter, r *http.Request) {
+		w.Header().Set("Server", "fave.pro/"+consts.ServerVersion)
+	}, func(w http.ResponseWriter, r *http.Request) {
+
+		/*
+			// After callback
+			w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
+			w.Header().Set("Content-Type", "text/html")
+			w.Write([]byte(`
+				<div>Hello World!</div>
+				<div><a href="/assets/bootstrap.css">/assets/bootstrap.css</a></div>
+				<div><a href="/assets/bootstrap.js">/assets/bootstrap.js</a></div>
+				<div><a href="/assets/jquery.js">/assets/jquery.js</a></div>
+				<div><a href="/assets/popper.js">/assets/popper.js</a></div>
+			`))
+		*/
+
+	})
+
+	/*
+		if _, err := os.Stat(ParamWwwDir); os.IsNotExist(err) {
+			fmt.Println("Virtual hosts directory is not exists")
+			fmt.Println("Example: ./fave -host 127.0.0.1 -port 80 -dir ./hosts")
+			return
+		}
+		if ParamWwwDir[len(ParamWwwDir)-1] != '/' {
+			ParamWwwDir = ParamWwwDir + "/"
+		}
+
+		// Handle
+		mux := http.NewServeMux()
+		mux.HandleFunc("/", handler)
+
+		srv := &http.Server{
+			Addr:    fmt.Sprintf("%s:%d", ParamHost, ParamPort),
+			Handler: mux,
+		}
+
+		stop := make(chan os.Signal)
+		signal.Notify(stop, os.Interrupt)
+
+		go func() {
+			log.Printf("Starting server at %s:%d", ParamHost, ParamPort)
+			if err := srv.ListenAndServe(); err != nil {
+				if err != http.ErrServerClosed {
+					log.Fatal(err)
+				}
+			}
+		}()
+
+		// Wait for signal
+		<-stop
+
+		log.Printf("Shutting down server...\n")
+		ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
+		defer cancel()
+
+		if err := srv.Shutdown(ctx); err != nil {
+			log.Fatal(err)
+		} else {
+			log.Printf("Server is off!")
+		}
+	*/
+}
+
+/*
+func vhExists(vhosthome string) bool {
+	if st, err := os.Stat(vhosthome); !os.IsNotExist(err) {
+		if err == nil {
+			fmode := st.Mode()
+			if fmode.IsDir() {
+				return true
+			}
+		}
+	}
+	return false
+}
+
+func handler(w http.ResponseWriter, r *http.Request) {
+	// Build vhost home dir
+	host := r.Host
+	port := strconv.Itoa(ParamPort)
+	index := strings.Index(host, ":")
+	if index > -1 {
+		port = host[index+1:]
+		host = host[0:index]
+	}
+
+	// Cut "www" if exists
+	if strings.HasPrefix(host, "www.") {
+		host = host[4:]
+	}
+	VhostHomeDir = ParamWwwDir + host
+
+	// Check if virtual host exists
+	if !vhExists(VhostHomeDir) {
+		host = "localhost"
+		VhostHomeDir = ParamWwwDir + host
+	}
+
+	// Set protocol
+	r.URL.Scheme = "http"
+
+	// Set server name
+	w.Header().Set("Server", "fave.pro/"+constants.ServerVersion)
+
+	wrap := wrapper.New(&w, r, host, port, ParamWwwDir, VhostHomeDir)
+
+	// Check if localhost exists
+	if !vhExists(VhostHomeDir) && !strings.HasPrefix(r.URL.Path, "/assets/") {
+		wrap.PrintEnginePageError(errors.New("Folder " + VhostHomeDir + " is not found"))
+		return
+	}
+
+	// Create and start engine
+	wrap.Run(func(wrapper *wrapper.Wrapper) bool {
+		// Actions
+		action := actions.New(wrapper)
+		if action.Run() {
+			wrapper.Session.Save()
+			return true
+		}
+
+		// Pages
+		return handlerPage(wrapper)
+	})
+}
+*/

+ 37 - 0
utils/utils.go

@@ -0,0 +1,37 @@
+package utils
+
+import (
+	"os"
+	"strings"
+)
+
+func IsFileExists(filename string) bool {
+	if _, err := os.Stat(filename); !os.IsNotExist(err) {
+		if err == nil {
+			return true
+		}
+	}
+	return false
+}
+
+func IsDir(filename string) bool {
+	if st, err := os.Stat(filename); !os.IsNotExist(err) {
+		if err == nil {
+			if st.Mode().IsDir() {
+				return true
+			}
+		}
+	}
+	return false
+}
+
+func FixPath(path string) string {
+	newPath := strings.TrimSpace(path)
+	if len(newPath) <= 0 {
+		return newPath
+	}
+	if newPath[len(newPath)-1] == '/' || newPath[len(newPath)-1] == '\\' {
+		newPath = newPath[0 : len(newPath)-2]
+	}
+	return newPath
+}

+ 21 - 0
vendor/github.com/vladimirok5959/golang-server-bootstrap/LICENSE

@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2019 Vova Tkach
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.

File diff suppressed because it is too large
+ 2 - 0
vendor/github.com/vladimirok5959/golang-server-bootstrap/bootstrap/bootstrap.css.go


+ 74 - 0
vendor/github.com/vladimirok5959/golang-server-bootstrap/bootstrap/bootstrap.go

@@ -0,0 +1,74 @@
+package bootstrap
+
+import (
+	"context"
+	"fmt"
+	"net/http"
+	"os"
+	"os/signal"
+	"time"
+)
+
+type callback func(w http.ResponseWriter, r *http.Request)
+
+type bootstrap struct {
+	path   string
+	before callback
+	after  callback
+}
+
+func new(path string, before callback, after callback) *bootstrap {
+	return &bootstrap{path, before, after}
+}
+
+func (this *bootstrap) handler(w http.ResponseWriter, r *http.Request) {
+	if this.before != nil {
+		this.before(w, r)
+	}
+	if r.URL.Path == "/"+this.path+"/bootstrap.css" {
+		w.Header().Set("Content-Type", "text/css")
+		w.Write(resource_bootstrap_css)
+		return
+	} else if r.URL.Path == "/"+this.path+"/bootstrap.js" {
+		w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
+		w.Write(resource_bootstrap_js)
+		return
+	} else if r.URL.Path == "/"+this.path+"/jquery.js" {
+		w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
+		w.Write(resource_jquery_js)
+		return
+	} else if r.URL.Path == "/"+this.path+"/popper.js" {
+		w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
+		w.Write(resource_popper_js)
+		return
+	}
+	if this.after != nil {
+		this.after(w, r)
+	}
+}
+
+func Start(host string, timeout time.Duration, path string, before callback, after callback) {
+	mux := http.NewServeMux()
+	mux.HandleFunc("/", new(path, before, after).handler)
+	srv := &http.Server{
+		Addr:    host,
+		Handler: mux,
+	}
+	stop := make(chan os.Signal)
+	signal.Notify(stop, os.Interrupt)
+	go func() {
+		fmt.Printf("Starting server at http://%s/\n", host)
+		if err := srv.ListenAndServe(); err != nil {
+			if err != http.ErrServerClosed {
+				fmt.Println(err)
+			}
+		}
+	}()
+	<-stop
+	fmt.Println("Shutting down server...")
+	ctx, cancel := context.WithTimeout(context.Background(), timeout*time.Second)
+	defer cancel()
+	if err := srv.Shutdown(ctx); err != nil {
+		fmt.Println(err)
+	}
+}

File diff suppressed because it is too large
+ 2 - 0
vendor/github.com/vladimirok5959/golang-server-bootstrap/bootstrap/bootstrap.js.go


File diff suppressed because it is too large
+ 2 - 0
vendor/github.com/vladimirok5959/golang-server-bootstrap/bootstrap/jquery.js.go


File diff suppressed because it is too large
+ 2 - 0
vendor/github.com/vladimirok5959/golang-server-bootstrap/bootstrap/popper.js.go


Some files were not shown because too many files changed in this diff