|
@@ -1,40 +1,26 @@
|
|
|
package main
|
|
|
|
|
|
import (
|
|
|
+ "errors"
|
|
|
"flag"
|
|
|
"fmt"
|
|
|
"net/http"
|
|
|
+ "os"
|
|
|
|
|
|
"golang-fave/assets"
|
|
|
"golang-fave/consts"
|
|
|
+ "golang-fave/static"
|
|
|
"golang-fave/utils"
|
|
|
|
|
|
"github.com/vladimirok5959/golang-server-bootstrap/bootstrap"
|
|
|
"github.com/vladimirok5959/golang-server-resources/resource"
|
|
|
- /*
|
|
|
- "context"
|
|
|
- "errors"
|
|
|
- "flag"
|
|
|
- "fmt"
|
|
|
- "log"
|
|
|
- "net/http"
|
|
|
- "os"
|
|
|
- "os/signal"
|
|
|
- "strconv"
|
|
|
- "strings"
|
|
|
- "time"
|
|
|
-
|
|
|
- "golang-fave/constants"
|
|
|
- "golang-fave/engine/actions"
|
|
|
- "golang-fave/engine/wrapper"
|
|
|
- */)
|
|
|
+ "github.com/vladimirok5959/golang-server-sessions/session"
|
|
|
+)
|
|
|
|
|
|
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")
|
|
@@ -44,25 +30,29 @@ func init() {
|
|
|
|
|
|
func main() {
|
|
|
ParamWwwDir = utils.FixPath(ParamWwwDir)
|
|
|
- if !(utils.IsFileExists(ParamWwwDir) && utils.IsDir(ParamWwwDir)) {
|
|
|
+ if !utils.IsHostDirExists(ParamWwwDir) {
|
|
|
fmt.Println("Virtual hosts directory is not exists")
|
|
|
fmt.Println("Example: ./fave -host 127.0.0.1 -port 80 -dir ./hosts")
|
|
|
return
|
|
|
}
|
|
|
|
|
|
res := resource.New()
|
|
|
- res.Add("assets/cp/scripts.js", "application/javascript; charset=utf-8", assets.CpScriptsJs)
|
|
|
- res.Add("assets/cp/styles.css", "text/css", assets.CpStylesCss)
|
|
|
- res.Add("assets/sys/bg.png", "image/png", assets.SysBgPng)
|
|
|
- res.Add("assets/sys/fave.ico", "image/x-icon", assets.SysFaveIco)
|
|
|
- res.Add("assets/sys/logo.png", "image/png", assets.SysLogoPng)
|
|
|
- res.Add("assets/sys/logo.svg", "image/svg+xml", assets.SysLogoSvg)
|
|
|
- res.Add("assets/sys/styles.css", "text/css", assets.SysStylesCss)
|
|
|
-
|
|
|
- bootstrap.Start(fmt.Sprintf("%s:%d", ParamHost, ParamPort), 30, "assets", func(w http.ResponseWriter, r *http.Request) {
|
|
|
+ res.Add(consts.AssetsCpScriptsJs, "application/javascript; charset=utf-8", assets.CpScriptsJs)
|
|
|
+ res.Add(consts.AssetsCpStylesCss, "text/css", assets.CpStylesCss)
|
|
|
+ res.Add(consts.AssetsSysBgPng, "image/png", assets.SysBgPng)
|
|
|
+ res.Add(consts.AssetsSysFaveIco, "image/x-icon", assets.SysFaveIco)
|
|
|
+ res.Add(consts.AssetsSysLogoPng, "image/png", assets.SysLogoPng)
|
|
|
+ res.Add(consts.AssetsSysLogoSvg, "image/svg+xml", assets.SysLogoSvg)
|
|
|
+ res.Add(consts.AssetsSysStylesCss, "text/css", assets.SysStylesCss)
|
|
|
+
|
|
|
+ // TODO: Move this to `"github.com/vladimirok5959/golang-server-static/static"`
|
|
|
+ stat := static.New(consts.DirIndexFile)
|
|
|
+
|
|
|
+ // TODO: Logic as object here
|
|
|
+
|
|
|
+ bootstrap.Start(fmt.Sprintf("%s:%d", ParamHost, ParamPort), 30, consts.AssetsPath, func(w http.ResponseWriter, r *http.Request) {
|
|
|
w.Header().Set("Server", "fave.pro/"+consts.ServerVersion)
|
|
|
}, func(w http.ResponseWriter, r *http.Request) {
|
|
|
-
|
|
|
// Mounted assets
|
|
|
if res.Response(w, r, func(w http.ResponseWriter, r *http.Request, i *resource.Resource) {
|
|
|
w.Header().Set("Cache-Control", "public, max-age=31536000")
|
|
@@ -70,127 +60,87 @@ func main() {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- /*
|
|
|
- // 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>
|
|
|
- `))
|
|
|
- */
|
|
|
+ // Host and port
|
|
|
+ host, port := utils.ExtractHostPort(r.Host, false)
|
|
|
+ vhost_dir := ParamWwwDir + string(os.PathSeparator) + host
|
|
|
+ if !utils.IsHostDirExists(vhost_dir) {
|
|
|
+ vhost_dir = ParamWwwDir + string(os.PathSeparator) + "localhost"
|
|
|
+ }
|
|
|
|
|
|
- })
|
|
|
+ // Check for minimal dir structure
|
|
|
+ vhost_dir_config := vhost_dir + string(os.PathSeparator) + "config"
|
|
|
+ vhost_dir_htdocs := vhost_dir + string(os.PathSeparator) + "htdocs"
|
|
|
+ vhost_dir_logs := vhost_dir + string(os.PathSeparator) + "logs"
|
|
|
+ vhost_dir_template := vhost_dir + string(os.PathSeparator) + "template"
|
|
|
+ vhost_dir_tmp := vhost_dir + string(os.PathSeparator) + "tmp"
|
|
|
|
|
|
- /*
|
|
|
- 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")
|
|
|
+ if !utils.IsHostDirExists(vhost_dir_config) {
|
|
|
+ utils.SystemErrorPage(w, errors.New("Folder "+vhost_dir_config+" is not found"))
|
|
|
return
|
|
|
}
|
|
|
- if ParamWwwDir[len(ParamWwwDir)-1] != '/' {
|
|
|
- ParamWwwDir = ParamWwwDir + "/"
|
|
|
+ if !utils.IsHostDirExists(vhost_dir_htdocs) {
|
|
|
+ utils.SystemErrorPage(w, errors.New("Folder "+vhost_dir_htdocs+" is not found"))
|
|
|
+ return
|
|
|
}
|
|
|
-
|
|
|
- // Handle
|
|
|
- mux := http.NewServeMux()
|
|
|
- mux.HandleFunc("/", handler)
|
|
|
-
|
|
|
- srv := &http.Server{
|
|
|
- Addr: fmt.Sprintf("%s:%d", ParamHost, ParamPort),
|
|
|
- Handler: mux,
|
|
|
+ if !utils.IsHostDirExists(vhost_dir_logs) {
|
|
|
+ utils.SystemErrorPage(w, errors.New("Folder "+vhost_dir_logs+" is not found"))
|
|
|
+ return
|
|
|
}
|
|
|
-
|
|
|
- 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!")
|
|
|
+ if !utils.IsHostDirExists(vhost_dir_template) {
|
|
|
+ utils.SystemErrorPage(w, errors.New("Folder "+vhost_dir_template+" is not found"))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if !utils.IsHostDirExists(vhost_dir_tmp) {
|
|
|
+ utils.SystemErrorPage(w, errors.New("Folder "+vhost_dir_tmp+" is not found"))
|
|
|
+ return
|
|
|
}
|
|
|
- */
|
|
|
-}
|
|
|
|
|
|
-/*
|
|
|
-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
|
|
|
- }
|
|
|
+ // Static files
|
|
|
+ if stat.Response(vhost_dir_htdocs, w, r, nil, nil) {
|
|
|
+ return
|
|
|
}
|
|
|
- }
|
|
|
- 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]
|
|
|
- }
|
|
|
+ // Session
|
|
|
+ //sess := session.New(w, r, vhost_dir_tmp)
|
|
|
+ //defer sess.Close()
|
|
|
|
|
|
- // Cut "www" if exists
|
|
|
- if strings.HasPrefix(host, "www.") {
|
|
|
- host = host[4:]
|
|
|
- }
|
|
|
- VhostHomeDir = ParamWwwDir + host
|
|
|
+ // Session struct need to make public!
|
|
|
+ sess := session.New(w, r, vhost_dir_tmp)
|
|
|
+ defer sess.Close()
|
|
|
|
|
|
- // Check if virtual host exists
|
|
|
- if !vhExists(VhostHomeDir) {
|
|
|
- host = "localhost"
|
|
|
- VhostHomeDir = ParamWwwDir + host
|
|
|
- }
|
|
|
+ // Logic
|
|
|
+ // TODO: call from `logic.Response()`
|
|
|
+ // TODO: create logic object here???
|
|
|
+ if logic(host, port, vhost_dir_config, vhost_dir_htdocs, vhost_dir_logs, vhost_dir_template, vhost_dir_tmp, w, r) {
|
|
|
+ return
|
|
|
+ }
|
|
|
|
|
|
- // Set protocol
|
|
|
- r.URL.Scheme = "http"
|
|
|
+ // Error 404
|
|
|
+ // TODO: display default template
|
|
|
+ w.WriteHeader(http.StatusNotFound)
|
|
|
+ w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
|
|
|
+ w.Header().Set("Content-Type", "text/html")
|
|
|
+ w.Write([]byte(`404`))
|
|
|
+ })
|
|
|
|
|
|
- // Set server name
|
|
|
- w.Header().Set("Server", "fave.pro/"+constants.ServerVersion)
|
|
|
+ // Delete expired session files
|
|
|
+ // session.Clean("./tmp")
|
|
|
+}
|
|
|
|
|
|
- wrap := wrapper.New(&w, r, host, port, ParamWwwDir, VhostHomeDir)
|
|
|
+func logic(host, port, dir_config, dir_htdocs, dir_logs, dir_template, dir_tmp string, w http.ResponseWriter, r *http.Request) bool {
|
|
|
+ if r.URL.Path == "/" {
|
|
|
+ w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
|
|
|
+ w.Header().Set("Content-Type", "text/html")
|
|
|
|
|
|
- // Check if localhost exists
|
|
|
- if !vhExists(VhostHomeDir) && !strings.HasPrefix(r.URL.Path, "/assets/") {
|
|
|
- wrap.PrintEnginePageError(errors.New("Folder " + VhostHomeDir + " is not found"))
|
|
|
- return
|
|
|
- }
|
|
|
+ //counter := sess.GetInt("counter", 0)
|
|
|
+ //w.Write([]byte(`Logic -> (` + fmt.Sprintf("%d", counter) + `)`))
|
|
|
|
|
|
- // Create and start engine
|
|
|
- wrap.Run(func(wrapper *wrapper.Wrapper) bool {
|
|
|
- // Actions
|
|
|
- action := actions.New(wrapper)
|
|
|
- if action.Run() {
|
|
|
- wrapper.Session.Save()
|
|
|
- return true
|
|
|
- }
|
|
|
+ w.Write([]byte(`Logic`))
|
|
|
|
|
|
- // Pages
|
|
|
- return handlerPage(wrapper)
|
|
|
- })
|
|
|
+ //counter++
|
|
|
+ //sess.SetInt("counter", counter)
|
|
|
+
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ return false
|
|
|
}
|
|
|
-*/
|