aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2013-04-17 14:51:51 +0200
committerPacien TRAN-GIRARD2013-04-17 14:51:51 +0200
commit8465bf703c826f778f915e80522ee7f1a07750b8 (patch)
treeeeae80bd67ebfe86516fefc013742113097cba06 /main.go
parentbe3d1e7f5bc99968d3386625b80f3fbf680db5a7 (diff)
downloadstaticweb-8465bf703c826f778f915e80522ee7f1a07750b8.tar.gz
First version.
Diffstat (limited to 'main.go')
-rwxr-xr-xmain.go91
1 files changed, 91 insertions, 0 deletions
diff --git a/main.go b/main.go
new file mode 100755
index 0000000..29eed83
--- /dev/null
+++ b/main.go
@@ -0,0 +1,91 @@
1/*
2
3 This file is part of StaticWeb (https://github.com/Pacien/StaticWeb).
4
5 StaticWeb is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Affero General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 StaticWeb is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with StaticWeb. If not, see <http://www.gnu.org/licenses/>.
17
18*/
19
20package main
21
22import (
23 "flag"
24 "log"
25 "net/http"
26 "os"
27 "strings"
28)
29
30var params struct {
31 addr, port, dir, log string
32}
33
34func defaultHandler(w http.ResponseWriter, r *http.Request) {
35 host := strings.Split(*&r.Host, ":")
36 if host[0] == "" {
37 log.Println("No host")
38 http.Error(w, "404 page not found", http.StatusNotFound)
39 return
40 }
41 request := r.URL.Path[1:]
42 requestedFile := *&params.dir + "/" + *&host[0] + "/" + *&request
43 log.Println(requestedFile)
44 file, err := os.Stat(requestedFile)
45 if err != nil {
46 log.Println(err)
47 http.Error(w, "404 page not found", http.StatusNotFound)
48 return
49 }
50 if file.IsDir() && !strings.HasSuffix(requestedFile, "/") {
51 http.Redirect(w, r, r.URL.Path+"/", http.StatusFound)
52 return
53 }
54 http.ServeFile(w, r, *&requestedFile)
55}
56
57func init() {
58 flag.StringVar(&params.addr, "addr", "127.0.0.1", "Address to listen.")
59 flag.StringVar(&params.port, "port", "8080", "Port to listen.")
60 flag.StringVar(&params.dir, "dir", ".", "Absolute or relative path to the root directory to serve.")
61 flag.StringVar(&params.log, "log", "", "Absolute or relative path to the log file. Leave empty for stdout.")
62 flag.Parse()
63}
64
65func main() {
66 if params.log != "" {
67 logFile, err := os.OpenFile(*&params.log, os.O_WRONLY, 0666)
68 if os.IsNotExist(*&err) {
69 log.Println("Log file not found, creating a new log file:", *&err)
70 logFile, err = os.Create(*&params.log)
71 if err != nil {
72 log.Println("Cannot create log file:", *&err)
73 return
74 }
75 } else if *&err != nil {
76 log.Println("Cannot open log file:", *&err)
77 return
78 }
79 defer logFile.Close()
80 log.SetOutput(*&logFile)
81 }
82
83 log.Println("Starting StaticWeb on " + *&params.addr + ":" + *&params.port)
84
85 http.HandleFunc("/", defaultHandler)
86 err := http.ListenAndServe(*&params.addr+":"+*&params.port, nil)
87 if err != nil {
88 log.Println(*&err)
89 return
90 }
91}