From 8465bf703c826f778f915e80522ee7f1a07750b8 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Wed, 17 Apr 2013 14:51:51 +0200 Subject: First version. --- README.md | 13 ++++++++- main.go | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 1 deletion(-) create mode 100755 main.go diff --git a/README.md b/README.md index 2fb8ca5..f76488e 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,15 @@ StaticWeb ========= -StaticWeb \ No newline at end of file +StaticWeb is a basic static files web server written in Go. + +It automatically serves the content of the folder corresponding to the domain of the website. + +Usage +----- + +``./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]"`` + +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. + +To serve multiple sites with the same content, you can use symbolic links. \ No newline at end of file diff --git a/main.go b/main.go new file mode 100755 index 0000000..29eed83 --- /dev/null +++ b/main.go @@ -0,0 +1,91 @@ +/* + + This file is part of StaticWeb (https://github.com/Pacien/StaticWeb). + + StaticWeb is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + StaticWeb is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with StaticWeb. If not, see . + +*/ + +package main + +import ( + "flag" + "log" + "net/http" + "os" + "strings" +) + +var params struct { + addr, port, dir, log string +} + +func defaultHandler(w http.ResponseWriter, r *http.Request) { + host := strings.Split(*&r.Host, ":") + if host[0] == "" { + log.Println("No host") + http.Error(w, "404 page not found", http.StatusNotFound) + return + } + request := r.URL.Path[1:] + requestedFile := *¶ms.dir + "/" + *&host[0] + "/" + *&request + log.Println(requestedFile) + file, err := os.Stat(requestedFile) + if err != nil { + log.Println(err) + http.Error(w, "404 page not found", http.StatusNotFound) + return + } + if file.IsDir() && !strings.HasSuffix(requestedFile, "/") { + http.Redirect(w, r, r.URL.Path+"/", http.StatusFound) + return + } + http.ServeFile(w, r, *&requestedFile) +} + +func init() { + flag.StringVar(¶ms.addr, "addr", "127.0.0.1", "Address to listen.") + flag.StringVar(¶ms.port, "port", "8080", "Port to listen.") + flag.StringVar(¶ms.dir, "dir", ".", "Absolute or relative path to the root directory to serve.") + flag.StringVar(¶ms.log, "log", "", "Absolute or relative path to the log file. Leave empty for stdout.") + flag.Parse() +} + +func main() { + if params.log != "" { + logFile, err := os.OpenFile(*¶ms.log, os.O_WRONLY, 0666) + if os.IsNotExist(*&err) { + log.Println("Log file not found, creating a new log file:", *&err) + logFile, err = os.Create(*¶ms.log) + if err != nil { + log.Println("Cannot create log file:", *&err) + return + } + } else if *&err != nil { + log.Println("Cannot open log file:", *&err) + return + } + defer logFile.Close() + log.SetOutput(*&logFile) + } + + log.Println("Starting StaticWeb on " + *¶ms.addr + ":" + *¶ms.port) + + http.HandleFunc("/", defaultHandler) + err := http.ListenAndServe(*¶ms.addr+":"+*¶ms.port, nil) + if err != nil { + log.Println(*&err) + return + } +} -- cgit v1.2.3