├── .gitignore ├── .goxc.json ├── LICENSE ├── README.md ├── Rakefile └── main.go /.gitignore: -------------------------------------------------------------------------------- 1 | gost 2 | build 3 | -------------------------------------------------------------------------------- /.goxc.json: -------------------------------------------------------------------------------- 1 | { 2 | "FormatVersion": "0.8", 3 | "ArtifactsDest": "build", 4 | "Arch": "386,amd64", 5 | "Os": "darwin,linux,windows", 6 | "PackageVersion": "0.1.1", 7 | "PrereleaseInfo": "snapshot", 8 | "Verbosity": "v", 9 | "Resources": { 10 | "Include": "README*,LICENSE*", 11 | "Exclude": "*.go" 12 | }, 13 | "TaskSettings": { 14 | "archive": { 15 | "os": { 16 | "linux": "TarGz", 17 | "darwin": "TarGz" 18 | } 19 | }, 20 | "downloads-page": { 21 | "fileheader": "", 22 | "filename": "" 23 | }, 24 | "pkg-build": { 25 | "metadata": { 26 | "description": "Simple static file server", 27 | "maintainer": "Akeda Bagus (http://gedex.web.id)" 28 | }, 29 | "metadata-deb": { 30 | "Depends": "", 31 | "Homepage": "https://github.com/golang-id/gost" 32 | } 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 gost authors. All rights reserved. 2 | 3 | Redistribution and use in source and binary forms, with or without 4 | modification, are permitted provided that the following conditions are 5 | met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above 10 | copyright notice, this list of conditions and the following disclaimer 11 | in the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of Google Inc. nor the names of its 14 | contributors may be used to endorse or promote products derived from 15 | this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | gost 2 | ==== 3 | 4 | Simple static file server. Current version is 0.1.1. 5 | 6 | ## Install 7 | 8 | ~~~text 9 | $ go install github.com/golang-id/gost 10 | ~~~ 11 | 12 | ## Usage 13 | 14 | Running static file server on `http://localhost:8080` with current 15 | directory as document root: 16 | 17 | ~~~text 18 | $ gost 19 | ~~~ 20 | 21 | You can specify optional `listen` address and/or `path` (to be served 22 | as the document root) flags. 23 | 24 | ~~~text 25 | $ gost -listen=:12345 -path="/home/gedex/my_static_web" 26 | ~~~ 27 | 28 | You can also specify an IP address if needed. In this example the server will 29 | bind to IP address 10.0.1.5 on port 9000. 30 | 31 | ~~~text 32 | $ gost -listen 10.0.1.5:9000 33 | ~~~ 34 | 35 | See the help: 36 | 37 | ~~~text 38 | $ gost --help 39 | Usage of gost: 40 | -listen=":8080": Address to bind to 41 | -path="./": Path served as document root 42 | ~~~ 43 | 44 | ## Roadmap 45 | 46 | Provides binaries for major platforms using [goxc](https://github.com/laher/goxc). 47 | 48 | Good reference: https://github.com/jingweno/gh 49 | 50 | ## Credits 51 | 52 | * [Serving Static Files with HTTP](https://code.google.com/p/go-wiki/wiki/HttpStaticFiles) 53 | * [Node.JS static file web server](https://gist.github.com/rpflorence/701407) 54 | 55 | ## License 56 | 57 | See [LICENSE](./LICENSE) file. 58 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "fileutils" 2 | 3 | class VersionedFile 4 | def initialize(file, regex) 5 | @file = file 6 | @regex = regex 7 | end 8 | 9 | def current_version! 10 | @current_version ||= matched_data![1] 11 | end 12 | 13 | def bump_version!(type) 14 | position = case type 15 | when :major 16 | 0 17 | when :minor 18 | 1 19 | when :patch 20 | 2 21 | end 22 | @current_version = current_version!.split(".").tap do |v| 23 | v[position] = v[position].to_i + 1 24 | # Reset consequent numbers 25 | ((position + 1)..2).each { |p| v[p] = 0 } 26 | end.join(".") 27 | end 28 | 29 | def save! 30 | text = File.read(@file) 31 | new_line = matched_data![0].gsub(matched_data![1], @current_version) 32 | text.gsub!(matched_data![0], new_line) 33 | 34 | File.open(@file, "w") { |f| f.puts text } 35 | end 36 | 37 | private 38 | 39 | def matched_data! 40 | @matched_data ||= begin 41 | m = @regex.match File.read(@file) 42 | raise "No version #{@regex} matched in #{@file}" unless m 43 | m 44 | end 45 | end 46 | end 47 | 48 | def fullpath(file) 49 | File.expand_path(file, File.dirname(__FILE__)) 50 | end 51 | 52 | VERSION_FILES = { 53 | fullpath("main.go") => /^const Version = "(\d+.\d+.\d+)"$/, 54 | fullpath("README.md") => /Current version is (\d+.\d+.\d+)/, 55 | fullpath(".goxc.json") => /"PackageVersion": "(\d+.\d+.\d+)"/, 56 | } 57 | 58 | class Git 59 | class << self 60 | def dirty? 61 | !`git status -s`.empty? 62 | end 63 | 64 | def checkout 65 | `git checkout .` 66 | end 67 | 68 | def commit_all(msg) 69 | `git commit -am "#{msg}"` 70 | end 71 | 72 | def create_tag(tag, msg) 73 | `git tag -a #{tag} -m "#{msg}"` 74 | end 75 | end 76 | end 77 | 78 | namespace :release do 79 | desc "Current released version" 80 | task :current do 81 | vf = VersionedFile.new(*VERSION_FILES.first) 82 | puts vf.current_version! 83 | end 84 | 85 | [:major, :minor, :patch].each do |type| 86 | desc "Release #{type} version" 87 | task type do 88 | if Git.dirty? 89 | puts "Please commit all changes first" 90 | exit 1 91 | end 92 | 93 | new_versions = VERSION_FILES.map do |file, regex| 94 | begin 95 | vf = VersionedFile.new(file, regex) 96 | current_version = vf.current_version! 97 | vf.bump_version!(type) 98 | vf.save! 99 | puts "Successfully bump #{file} from #{current_version} to #{vf.current_version!}" 100 | vf.current_version! 101 | rescue => e 102 | Git.checkout 103 | raise e 104 | end 105 | end 106 | 107 | require "set" 108 | new_versions = new_versions.to_set 109 | if new_versions.size != 1 110 | raise "More than one version among #{VERSION_FILES}" 111 | end 112 | 113 | new_version = "v#{new_versions.first}" 114 | msg = "Bump version to #{new_version}" 115 | Git.commit_all(msg) 116 | Git.create_tag(new_version, msg) 117 | end 118 | end 119 | end 120 | 121 | module OS 122 | class << self 123 | def type 124 | if darwin? 125 | "darwin" 126 | elsif linux? 127 | "linux" 128 | elsif windows? 129 | "windows" 130 | else 131 | raise "Unknown OS type #{RUBY_PLATFORM}" 132 | end 133 | end 134 | 135 | def windows? 136 | (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil 137 | end 138 | 139 | def darwin? 140 | (/darwin/ =~ RUBY_PLATFORM) != nil 141 | end 142 | 143 | def linux? 144 | (/linux/ =~ RUBY_PLATFORM) != nil 145 | end 146 | end 147 | end 148 | 149 | namespace :build do 150 | desc "Build for current operating system" 151 | task :current => [:update_goxc, :remove_build_target, :build_gost] 152 | 153 | task :update_goxc do 154 | puts "Updating goxc..." 155 | result = system "go get -u github.com/laher/goxc" 156 | raise "Fail to update goxc" unless result 157 | end 158 | 159 | task :remove_build_target do 160 | FileUtils.rm_rf fullpath("build") 161 | end 162 | 163 | task :build_gost do 164 | puts "Building for #{OS.type}..." 165 | puts `goxc -wd=. -os=#{OS.type} -c=#{OS.type}` 166 | end 167 | end 168 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "net/http" 7 | "path/filepath" 8 | ) 9 | 10 | var ( 11 | listen = flag.String("listen", ":8080", "Address to bind to") 12 | path = flag.String("path", "./", "Path served as document root.") 13 | ) 14 | 15 | const Version = "0.1.1" 16 | 17 | func main() { 18 | flag.Parse() 19 | 20 | docroot, err := filepath.Abs(*path) 21 | if err != nil { 22 | fmt.Printf("Error: %v", err) 23 | } 24 | 25 | fmt.Printf("Static file server running at %v. CTRL + C to shutdown\n", 26 | *listen) 27 | 28 | handler := http.FileServer(http.Dir(docroot)) 29 | 30 | if err = http.ListenAndServe(*listen, handler); err != nil { 31 | fmt.Printf("Error: %v\n", err) 32 | } 33 | } 34 | --------------------------------------------------------------------------------