diff --git a/Dockerfile b/Dockerfile index ccc9093..5e11bbe 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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"] \ No newline at end of file diff --git a/README.md b/README.md index 58853c8..102e606 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/computer.csv b/computer.csv index 8b6f8b0..fce9757 100644 --- a/computer.csv +++ b/computer.csv @@ -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 \ No newline at end of file +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 \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d4f325f --- /dev/null +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..67878d2 --- /dev/null +++ b/go.sum @@ -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= diff --git a/data.go b/internal/repository/data.go similarity index 68% rename from data.go rename to internal/repository/data.go index e199f83..fea7eb3 100644 --- a/data.go +++ b/internal/repository/data.go @@ -1,4 +1,4 @@ -package main +package repository import ( "log" @@ -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 @@ -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 -} diff --git a/internal/repository/types.go b/internal/repository/types.go new file mode 100644 index 0000000..a39d5be --- /dev/null +++ b/internal/repository/types.go @@ -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 diff --git a/wol.go b/internal/wol/wol.go similarity index 96% rename from wol.go rename to internal/wol/wol.go index cc37738..041cec8 100644 --- a/wol.go +++ b/internal/wol/wol.go @@ -1,4 +1,4 @@ -package main +package wol // Stolen from https://github.com/sabhiram/go-wol diff --git a/main.go b/main.go index 0b2f56c..b1a4c03 100644 --- a/main.go +++ b/main.go @@ -5,27 +5,27 @@ 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) } @@ -33,11 +33,11 @@ func main() { 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) diff --git a/pages.go b/pages.go deleted file mode 100644 index ed9b4b1..0000000 --- a/pages.go +++ /dev/null @@ -1,13 +0,0 @@ -package main - -import ( - "html/template" - "net/http" -) - -func renderHomePage(w http.ResponseWriter, r *http.Request) { - - tmpl, _ := template.ParseFiles("pages/index.html") - tmpl.Execute(w, ComputerList) - -} diff --git a/pages/index.html b/pages/index.html index 7dec997..f937d5c 100644 --- a/pages/index.html +++ b/pages/index.html @@ -72,7 +72,11 @@

REST API Usage

Project Page: https://github.com/dabondi/go-rest-wol

- Build with by David Baumann, https://github.com/dabondi + Build with by: +

diff --git a/const.go b/pkg/cmd/const.go similarity index 88% rename from const.go rename to pkg/cmd/const.go index 08dc323..9e3b152 100644 --- a/const.go +++ b/pkg/cmd/const.go @@ -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" diff --git a/envVars.go b/pkg/cmd/envVars.go similarity index 69% rename from envVars.go rename to pkg/cmd/envVars.go index 23275c1..b22847b 100644 --- a/envVars.go +++ b/pkg/cmd/envVars.go @@ -1,4 +1,4 @@ -package main +package cmd import ( "log" @@ -6,8 +6,18 @@ import ( "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 diff --git a/shellArgs.go b/pkg/cmd/shellArgs.go similarity index 84% rename from shellArgs.go rename to pkg/cmd/shellArgs.go index ed3e68e..341dc40 100644 --- a/shellArgs.go +++ b/pkg/cmd/shellArgs.go @@ -1,6 +1,4 @@ -/* Processing and handling Shell Arguments */ - -package main +package cmd import ( "log" @@ -8,8 +6,8 @@ import ( "strconv" ) -// Processing Shell Arguments -func processShellArgs() (int, string) { +// ProcessShellArgs Processing Shell Arguments +func ProcessShellArgs() (int, string) { // Reading Shell Args shellArgs := os.Args[1:] diff --git a/pkg/http/handler/pages.go b/pkg/http/handler/pages.go new file mode 100644 index 0000000..5ae138c --- /dev/null +++ b/pkg/http/handler/pages.go @@ -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) + +} diff --git a/rest.go b/pkg/http/handler/rest.go similarity index 76% rename from rest.go rename to pkg/http/handler/rest.go index a3b5e44..d730d66 100644 --- a/rest.go +++ b/pkg/http/handler/rest.go @@ -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/ -func restWakeUpWithComputerName(w http.ResponseWriter, r *http.Request) { +// RestWakeUpWithComputerName - REST Handler for Processing URLS /api/computer/ +func RestWakeUpWithComputerName(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") @@ -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 diff --git a/pkg/http/handler/types.go b/pkg/http/handler/types.go new file mode 100644 index 0000000..9cfb771 --- /dev/null +++ b/pkg/http/handler/types.go @@ -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"` +} diff --git a/types.go b/types.go deleted file mode 100644 index 0e88377..0000000 --- a/types.go +++ /dev/null @@ -1,15 +0,0 @@ -package main - -//WakeUpResponseObject Datastructure for holding information for the WakeUpResult -type WakeUpResponseObject struct { - Success bool `json:"success"` - Message string `json:"message"` - ErrorObject error `json:"error"` -} - -// Computer represents a Computer Object -type Computer struct { - Name string `csv:"name"` - Mac string `csv:"mac"` - BroadcastIPAddress string `csv:"ip"` -}