diff options
-rw-r--r-- | README.md | 13 | ||||
-rwxr-xr-x | main.go | 91 |
2 files changed, 103 insertions, 1 deletions
@@ -1,4 +1,15 @@ | |||
1 | StaticWeb | 1 | StaticWeb |
2 | ========= | 2 | ========= |
3 | 3 | ||
4 | StaticWeb \ No newline at end of file | 4 | StaticWeb is a basic static files web server written in Go. |
5 | |||
6 | It automatically serves the content of the folder corresponding to the domain of the website. | ||
7 | |||
8 | Usage | ||
9 | ----- | ||
10 | |||
11 | ``./StaticWeb -addr="[Address to listen: 127.0.0.1]" -port="[Port to listen: 80]" -dir="[Absolute or relative path to the root directory to serve: .]" -log="[Absolute or relative path to the log file. Leave empty for stdout]"`` | ||
12 | |||
13 | The program will serve the files in the folder (inside the given root directory) with the same name as the domain from which the request originated. | ||
14 | |||
15 | To serve multiple sites with the same content, you can use symbolic links. \ No newline at end of file | ||
@@ -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 | } | ||