diff options
author | Pacien TRAN-GIRARD | 2013-04-17 14:51:51 +0200 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2013-04-17 14:51:51 +0200 |
commit | 8465bf703c826f778f915e80522ee7f1a07750b8 (patch) | |
tree | eeae80bd67ebfe86516fefc013742113097cba06 /main.go | |
parent | be3d1e7f5bc99968d3386625b80f3fbf680db5a7 (diff) | |
download | staticweb-8465bf703c826f778f915e80522ee7f1a07750b8.tar.gz |
First version.
Diffstat (limited to 'main.go')
-rwxr-xr-x | main.go | 91 |
1 files changed, 91 insertions, 0 deletions
@@ -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 | |||
20 | package main | ||
21 | |||
22 | import ( | ||
23 | "flag" | ||
24 | "log" | ||
25 | "net/http" | ||
26 | "os" | ||
27 | "strings" | ||
28 | ) | ||
29 | |||
30 | var params struct { | ||
31 | addr, port, dir, log string | ||
32 | } | ||
33 | |||
34 | func 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 := *¶ms.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 | |||
57 | func init() { | ||
58 | flag.StringVar(¶ms.addr, "addr", "127.0.0.1", "Address to listen.") | ||
59 | flag.StringVar(¶ms.port, "port", "8080", "Port to listen.") | ||
60 | flag.StringVar(¶ms.dir, "dir", ".", "Absolute or relative path to the root directory to serve.") | ||
61 | flag.StringVar(¶ms.log, "log", "", "Absolute or relative path to the log file. Leave empty for stdout.") | ||
62 | flag.Parse() | ||
63 | } | ||
64 | |||
65 | func main() { | ||
66 | if params.log != "" { | ||
67 | logFile, err := os.OpenFile(*¶ms.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(*¶ms.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 " + *¶ms.addr + ":" + *¶ms.port) | ||
84 | |||
85 | http.HandleFunc("/", defaultHandler) | ||
86 | err := http.ListenAndServe(*¶ms.addr+":"+*¶ms.port, nil) | ||
87 | if err != nil { | ||
88 | log.Println(*&err) | ||
89 | return | ||
90 | } | ||
91 | } | ||