Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
FROM golang:alpine AS builder
FROM golang:1.16-alpine AS builder

LABEL org.label-schema.vcs-url="https://github.com/daBONDi/go-rest-wol" \
org.label-schema.url="https://github.com/daBONDi/go-rest-wol/blob/master/README.md"

RUN mkdir /app
ADD . /app/
WORKDIR /app

ADD . .

# Install Dependecies
RUN apk update && apk upgrade && \
apk add --no-cache git && \
go get -d github.com/gorilla/handlers && \
go get -d github.com/gorilla/mux && \
go get -d github.com/gocarina/gocsv
apk add --no-cache git

# Build Source Files
RUN go build -o main .
RUN go build -o go-rest-wol .

# Create 2nd Stage final image
FROM alpine

WORKDIR /app
COPY --from=builder /app/pages/index.html ./pages/index.html
COPY --from=builder /app/computer.csv .
COPY --from=builder /app/main .

ARG WOLHTTPPORT=8080
ARG WOLFILE=computer.csv
COPY --from=builder /app/pages/index.html ./pages/index.html
COPY --from=builder /app/computer.csv ./computer.csv
COPY --from=builder /app/go-rest-wol .

CMD ["/app/main"]
ENV WOLHTTPPORT=8080
ENV WOLFILE=computer.csv

EXPOSE ${WOLHTTPPORT}

ENTRYPOINT ["/app/go-rest-wol"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Computer3,FF-B3-95-62-1C-DD,192.168.10.254:9

```
docker build -t go-rest-wol .
docker run go-rest-wol
docker run --rm -p 8080:8080 go-rest-wol
```
If you want to run it on a different port (i.e.: 6969) and also want to provide the CSV file on your host:

Expand Down
6 changes: 3 additions & 3 deletions computer.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name,mac,ip
Computer1,64-07-2D-BB-BB-BF,192.168.10.254:9
Computer2,2D-F2-3D-06-17-00,192.168.10.254:9
Computer3,FF-B3-95-62-1C-DD,192.168.10.254:9
ExampleComputer1,64-07-2D-BB-BB-BF,192.168.10.254:9
ExampleComputer2,2D-F2-3D-06-17-00,192.168.10.254:9
ExampleComputer3,FF-B3-95-62-1C-DD,192.168.10.254:9
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/daBONDi/go-rest-wol

go 1.16

require (
github.com/gocarina/gocsv v0.0.0-20201208093247-67c824bc04d4
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ=
github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/gocarina/gocsv v0.0.0-20201208093247-67c824bc04d4 h1:Q7s2AN3DhFJKOnzO0uTKLhJTfXTEcXcvw5ylf2BHJw4=
github.com/gocarina/gocsv v0.0.0-20201208093247-67c824bc04d4/go.mod h1:5YoVOkjYAQumqlV356Hj3xeYh4BdZuLE0/nRkf2NKkI=
github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4=
github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
14 changes: 2 additions & 12 deletions data.go → internal/repository/data.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package repository

import (
"log"
Expand All @@ -8,7 +8,7 @@ import (
)

// LoadComputerList loads the Computer and return the readed list or an error
func loadComputerList(computerCsvFilePath string) ([]Computer, error) {
func LoadComputerList(computerCsvFilePath string) ([]Computer, error) {

var computers []Computer

Expand All @@ -26,13 +26,3 @@ func loadComputerList(computerCsvFilePath string) ([]Computer, error) {
computerCsvFilePointer.Close()
return computers, nil
}

// Test if Path is a File and it exist
func FileExists(name string) bool {
if fi, err := os.Stat(name); err == nil {
if fi.Mode().IsRegular() {
return true
}
}
return false
}
11 changes: 11 additions & 0 deletions internal/repository/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package repository

// Computer represents a Computer Object
type Computer struct {
Name string `csv:"name"`
Mac string `csv:"mac"`
BroadcastIPAddress string `csv:"ip"`
}

// ComputerList contains all Computers who we can use to work with
var ComputerList []Computer
2 changes: 1 addition & 1 deletion wol.go → internal/wol/wol.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package wol

// Stolen from https://github.com/sabhiram/go-wol

Expand Down
22 changes: 11 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,39 @@ import (
"log"
"net/http"

"github.com/daBONDi/go-rest-wol/internal/repository"
"github.com/daBONDi/go-rest-wol/pkg/cmd"
"github.com/daBONDi/go-rest-wol/pkg/http/handler"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)

// ComputerList contains all Computers who we can use to work with
var ComputerList []Computer

func main() {

httpPort := DefaultHTTPPort
computerFilePath := DefaultComputerFilePath
httpPort := cmd.DefaultHTTPPort
computerFilePath := cmd.DefaultComputerFilePath

// Start Processing Shell Arguments or use Default Values defined i const.go
httpPort, computerFilePath = processShellArgs()
httpPort, computerFilePath = cmd.ProcessShellArgs()

// Process Environment Variables
httpPort, computerFilePath = processEnvVars()
httpPort, computerFilePath = cmd.ProcessEnvVars()

// Loading Computer CSV File to Memory File in Memory
var loadComputerCSVFileError error
if ComputerList, loadComputerCSVFileError = loadComputerList(computerFilePath); loadComputerCSVFileError != nil {
if repository.ComputerList, loadComputerCSVFileError = repository.LoadComputerList(computerFilePath); loadComputerCSVFileError != nil {
log.Fatalf("Error on loading Computerlist File \"%s\" check File access and formating", computerFilePath)
}

// Init HTTP Router - mux
router := mux.NewRouter()

// Define Home Route
router.HandleFunc("/", renderHomePage).Methods("GET")
router.HandleFunc("/", handler.RenderHomePage).Methods("GET")

// Define Wakeup Api functions with a Computer Name
router.HandleFunc("/api/wakeup/computer/{computerName}", restWakeUpWithComputerName).Methods("GET")
router.HandleFunc("/api/wakeup/computer/{computerName}/", restWakeUpWithComputerName).Methods("GET")
router.HandleFunc("/api/wakeup/computer/{computerName}", handler.RestWakeUpWithComputerName).Methods("GET")
router.HandleFunc("/api/wakeup/computer/{computerName}/", handler.RestWakeUpWithComputerName).Methods("GET")

// Setup Webserver
httpListen := fmt.Sprint(":", httpPort)
Expand Down
13 changes: 0 additions & 13 deletions pages.go

This file was deleted.

6 changes: 5 additions & 1 deletion pages/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ <h3>REST API Usage</h3>
<span class="fa fa-github"></span> Project Page: <a href="https://github.com/dabondi/go-rest-wol">https://github.com/dabondi/go-rest-wol</a>
</p>
<p>
<small>Build with <span class="fa fa-heart text-danger" aria-hidden="true"></span> by David Baumann, <span class="fa fa-github"></span> <a href="https://github.com/dabondi">https://github.com/dabondi</a></small>
<small>Build with <span class="fa fa-heart text-danger" aria-hidden="true"></span> by:</small>
<ul>
<li>David Baumann, <span class="fa fa-github"></span> <a target="_blank" href="https://github.com/dabondi">https://github.com/dabondi</a></li>
<li>Contributors, <span class="fa fa-github"></span> <a target="_blank" href="https://github.com/daBONDi/go-rest-wol/graphs/contributors">https://github.com/daBONDi/go-rest-wol/graphs/contributors</a></li>
</ul>
</p>
</div>
</div>
Expand Down
27 changes: 13 additions & 14 deletions const.go → pkg/cmd/const.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
//All Global Constants and Program Default Values
package main

//DefaultHTTPPort defines the Default HTTP Port to listen
const DefaultHTTPPort int = 8080

//DefaultComputerFilePath define the Default File Path to search for the Computer List
const DefaultComputerFilePath string = "computer.csv"

//DefaultComputerFilePathEnvironmentName define the Name of the Enviroment Variable where we should look for a Path
const DefaultComputerFilePathEnvironmentName string = "WOLFILE"

//DefaultHTTPPortEnvironmentVariableName define the Name of the Environment Variable what TCP Port we should use for the Webserver
const DefaultHTTPPortEnvironmentVariableName string = "WOLHTTPPORT"
package cmd

//DefaultHTTPPort defines the Default HTTP Port to listen
const DefaultHTTPPort int = 8080

//DefaultComputerFilePath define the Default File Path to search for the Computer List
const DefaultComputerFilePath string = "computer.csv"

//DefaultComputerFilePathEnvironmentName define the Name of the Enviroment Variable where we should look for a Path
const DefaultComputerFilePathEnvironmentName string = "WOLFILE"

//DefaultHTTPPortEnvironmentVariableName define the Name of the Environment Variable what TCP Port we should use for the Webserver
const DefaultHTTPPortEnvironmentVariableName string = "WOLHTTPPORT"
16 changes: 13 additions & 3 deletions envVars.go → pkg/cmd/envVars.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package main
package cmd

import (
"log"
"os"
"strconv"
)

// Processing Shell Arguments
func processEnvVars() (int, string) {
// Test if Path is a File and it exist
func FileExists(name string) bool {
if fi, err := os.Stat(name); err == nil {
if fi.Mode().IsRegular() {
return true
}
}
return false
}

// ProcessEnvVars Processing Shell Arguments
func ProcessEnvVars() (int, string) {

computerFile := DefaultComputerFilePath
port := DefaultHTTPPort
Expand Down
8 changes: 3 additions & 5 deletions shellArgs.go → pkg/cmd/shellArgs.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
/* Processing and handling Shell Arguments */

package main
package cmd

import (
"log"
"os"
"strconv"
)

// Processing Shell Arguments
func processShellArgs() (int, string) {
// ProcessShellArgs Processing Shell Arguments
func ProcessShellArgs() (int, string) {
// Reading Shell Args
shellArgs := os.Args[1:]

Expand Down
16 changes: 16 additions & 0 deletions pkg/http/handler/pages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package handler

import (
"html/template"
"net/http"

"github.com/daBONDi/go-rest-wol/internal/repository"
)

// RenderHomePage The index page
func RenderHomePage(w http.ResponseWriter, r *http.Request) {

tmpl, _ := template.ParseFiles("pages/index.html")
tmpl.Execute(w, repository.ComputerList)

}
14 changes: 7 additions & 7 deletions rest.go → pkg/http/handler/rest.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// Rest API Implementations

package main
package handler

import (
"encoding/json"
"fmt"
"net/http"

"github.com/daBONDi/go-rest-wol/internal/repository"
"github.com/daBONDi/go-rest-wol/internal/wol"
"github.com/gorilla/mux"
)

//restWakeUpWithComputerName - REST Handler for Processing URLS /api/computer/<computerName>
func restWakeUpWithComputerName(w http.ResponseWriter, r *http.Request) {
// RestWakeUpWithComputerName - REST Handler for Processing URLS /api/computer/<computerName>
func RestWakeUpWithComputerName(w http.ResponseWriter, r *http.Request) {

w.Header().Set("Content-Type", "application/json")

Expand All @@ -30,11 +30,11 @@ func restWakeUpWithComputerName(w http.ResponseWriter, r *http.Request) {
} else {

// Get Computer from List
for _, c := range ComputerList {
for _, c := range repository.ComputerList {
if c.Name == computerName {

// We found the Computername
if err := SendMagicPacket(c.Mac, c.BroadcastIPAddress, ""); err != nil {
if err := wol.SendMagicPacket(c.Mac, c.BroadcastIPAddress, ""); err != nil {
// We got an internal Error on SendMagicPacket
w.WriteHeader(http.StatusInternalServerError)
result.Success = false
Expand Down
8 changes: 8 additions & 0 deletions pkg/http/handler/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package handler

// WakeUpResponseObject Datastructure for holding information for the WakeUpResult
type WakeUpResponseObject struct {
Success bool `json:"success"`
Message string `json:"message"`
ErrorObject error `json:"error"`
}
15 changes: 0 additions & 15 deletions types.go

This file was deleted.