-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
142 lines (130 loc) · 3.55 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"embed"
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"strings"
"github.com/joho/godotenv"
)
//go:embed _init
var initFiles embed.FS
type Layout struct {
Name string
Path string
}
type Snippet struct {
Name string
Path string
}
type FrontMatter struct {
Layout string `yaml:"layout"`
}
var PORT = "8080"
const DIST = "dist"
const SRC = "./src"
const DEFAULT_LAYOUT = "src/layouts/Default.html"
// src
// └── assets
// │ ├── css
// │ │ ├── pico.colors.min.css
// │ │ ├── pico.min.css
// │ │ └── styles.css
// │ ├── images
// │ │ └── logo.png
// │ └── js
// │ └── app.js
// ├── snippets
// │ └── Test.html
// ├── layouts
// │ ├── alpinejs.html
// │ ├── blog.html
// │ ├── default.html
// │ └── vanjs.html
// └── pages
// ├── about.html
// ├── alpinejs.html
// ├── index.html
// ├── markdown.md
// └── vanjs.html
var clients = make(map[chan string]bool)
var layouts []Layout
var snippets []Snippet
var dependencies = make(map[string][]string)
func main() {
var doBuild bool
flag.BoolVar(&doBuild, "build", false, "build the site")
var doDev bool
flag.BoolVar(&doDev, "dev", false, "build the site and run the dev server")
var doInit bool
flag.BoolVar(&doInit, "init", false, "scaffold a site in the current directory")
var jsFramework string
flag.StringVar(&jsFramework, "js", "none", "on init, which javascript framework do you want? none, vanjs (default), or alpinejs")
var doDeploy bool
flag.BoolVar(&doDeploy, "deploy", false, "deploy built site via scp")
var domain string
flag.StringVar(&domain, "domain", "", "optional, if you don't provide one we'll create one for you")
var env string
envOptions := []string{"production", "staging"}
flag.StringVar(&env, "env", envOptions[1], fmt.Sprintf("one of %v, defaults to %v", envOptions, envOptions[1]))
flag.Parse()
err := godotenv.Load(".env")
if err != nil {
_, err := os.Create(".env")
if err != nil {
log.Fatalf("Could not load or create .env file: %s", err)
}
}
if doBuild {
err := build(false)
if err != nil {
log.Fatalf("Build failed: %s", err)
}
} else if doDeploy {
err := build(false)
if err != nil {
log.Fatalf("Deploy failed: %s", err)
}
deploy(domain, env)
} else if doInit {
err := initializeNewProject(jsFramework)
if err != nil {
log.Fatalf("Init failed: %s", err)
}
} else if doDev {
err := build(false)
if err != nil {
log.Fatalf("Could not build: %s", err)
}
go fileWatcher()
http.HandleFunc("/", requestHandler)
http.HandleFunc("/sssg-hot-reload", hotReloadHandler)
ln, err := net.Listen("tcp", ":"+PORT)
if err != nil {
if strings.Contains(err.Error(), "address already in use") {
for port := 8080; port < 65535; port++ {
ln, err = net.Listen("tcp", ":"+fmt.Sprint(port))
if err == nil {
PORT = fmt.Sprint(port)
break
}
}
} else {
log.Fatal(err)
}
}
fmt.Printf("Server started on port %v\n", PORT)
log.Fatal(http.Serve(ln, nil))
} else {
flag.Usage()
fmt.Println("\nUsage: sssgo option")
fmt.Println("\n where option is one of the following:")
fmt.Println("\n build Build the site")
fmt.Println(" deploy Build and then deploy the site")
fmt.Println(" dev Build the site and start the dev server")
fmt.Println(" init Scaffold out a project folder structure and files if they don't already exist")
}
}