Skip to content

Commit

Permalink
update readme, added http port flag, add buildroot package files
Browse files Browse the repository at this point in the history
  • Loading branch information
kgolding committed Feb 6, 2020
1 parent d9b8a89 commit 36c2305
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 22 deletions.
19 changes: 0 additions & 19 deletions Makefile

This file was deleted.

22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
# go-simple-http-server
Super simple golang HTTP

Super simple golang HTTP server with example systemd service file and buildroot package.

## Buildroot

Copy the files buildroot/* using the same directory structure into your buildroot directory.

Add the following line to the buildroot/package/Config.in file typically under the 'menu "Miscellaneous"' line.

`source "package/go-simple-http-server/Config.in"`

Run `make menuconfig` and enable your new 'go-simple-http-server' under Packages -> Miscellaneous.

## Usage
```
Usage of go-simple-http-server:
-port int
http port (default 80)
-www string
directory with files to be served (default "www")
```
4 changes: 4 additions & 0 deletions buildroot/package/go-simple-http-server/Config.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
config BR2_PACKAGE_GO_SIMPLE_HTTP_SERVER
bool "go-simple-http-server"
help
Super simple go http server to prove golang apps can be built with buildroot
37 changes: 37 additions & 0 deletions buildroot/package/go-simple-http-server/go-simple-http-server.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
################################################################################
#
# Go Simple Http Server
#
################################################################################

GO_SIMPLE_HTTP_SERVER_VERSION = 0.4
GO_SIMPLE_HTTP_SERVER_SITE = $(call github,kgolding,go-simple-http-server,$(GO_SIMPLE_HTTP_SERVER_VERSION))
GO_SIMPLE_HTTP_SERVER_LICENSE = MIT
GO_SIMPLE_HTTP_SERVER_LICENSE_FILES = LICENSE

GO_SIMPLE_HTTP_SERVER_DESTDIR = /usr/share/go-simple-http-server

# Remove go DWARF dubgging table to shrink the binary a bit
GO_SIMPLE_HTTP_SERVER_LDFLAGS = -s -w

define GO_SIMPLE_HTTP_SERVER_PRE_INSTALL_TARGET_HOOKS
# Create directory
$(INSTALL) -d $(TARGET_DIR)$(GO_SIMPLE_HTTP_SERVER_DESTDIR)/www/
# Install www assets
find $(@D)/www/ -type f -exec $(INSTALL) -m 0644 -D "{}" $(TARGET_DIR)$(GO_SIMPLE_HTTP_SERVER_DESTDIR)/www/ \;
endef

# Add Systemd service
define GO_SIMPLE_HTTP_SERVER_INSTALL_INIT_SYSTEMD
# Install systemd service file
$(INSTALL) -D -m 0644 $(@D)/systemd.service \
$(TARGET_DIR)/usr/lib/systemd/system/go-simple-http-server.service

# Enable the service to auto start by symlinking the file
mkdir -p $(TARGET_DIR)/etc/systemd/system/multi-user.target.wants
ln -sf /usr/lib/systemd/system/go-simple-http-server.service \
$(TARGET_DIR)//etc/systemd/system/multi-user.target.wants/go-simple-http-server.service
endef

# The binary will be installed as /usr/bin/go-simple-http-server
$(eval $(golang-package))
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@ import (
"fmt"
"log"
"net/http"
"strconv"
)

func main() {
wwwDir := flag.String("www", "www", "directory with files to be served")
wwwPort := flag.Int("port", 80, "http port")

flag.Parse()

log.Printf("Serving files from '%s'", *wwwDir)

http.HandleFunc("/hello", handler)
http.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir(*wwwDir))))

err := http.ListenAndServe(":8080", nil)
log.Printf("Starting HTTP server on port %d", *wwwPort)
err := http.ListenAndServe(":"+strconv.Itoa(*wwwPort), nil)
if err != nil {
log.Fatal(err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion systemd.service
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Type=simple
Restart=always
RestartSec=1
User=root
ExecStart=/usr/bin/go-simple-http-server -www /usr/share/go-simple-http-server/www
ExecStart=/usr/bin/go-simple-http-server -port 80 -www /usr/share/go-simple-http-server/www

[Install]
WantedBy=multi-user.target

0 comments on commit 36c2305

Please sign in to comment.