forked from txn2/txwifi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
847 additions
and
459 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,12 @@ | ||
IMAGE ?= cjimti/iotwifi | ||
NAME ?= txwifi | ||
VERSION ?= 1.0.4 | ||
|
||
all: build push | ||
|
||
dev: dev_build dev_run | ||
|
||
build: | ||
docker build -t $(IMAGE):latest . | ||
docker build -t $(IMAGE):arm32v6-$(VERSION) . | ||
|
||
push: | ||
docker build -t $(IMAGE):latest . | ||
docker build -t $(IMAGE):arm32v6-$(VERSION) . | ||
|
||
dev_build: | ||
docker build -t $(IMAGE) ./dev/ | ||
|
||
dev_run: | ||
sudo docker run --rm -it --privileged --network=host \ | ||
-v $(CURDIR):/go/src/github.com/txn2/txwifi \ | ||
-w /go/src/github.com/txn2/txwifi \ | ||
--name=$(NAME) $(IMAGE):latest | ||
build_arm: | ||
GOARCH=arm GOARM=5 GOOS=linux go build | ||
|
||
build_x86: clean | ||
go build -o server_gorilla examples/server_gorilla.go | ||
go build -o server_gin examples/server_gin.go | ||
|
||
clean: | ||
@rm -f server_gorilla | ||
@rm -f server_gin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// IoT Wifi Management | ||
|
||
// todo: update documentation!!!! | ||
// todo: update Dockerfile | ||
// todo: listen for shutdown signal, remove uap0, kill wpa,apd,dnsmasq | ||
|
||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/gin-gonic/gin" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/txn2/txwifi/iotwifi" | ||
) | ||
|
||
func init() { | ||
log.SetOutput(os.Stdout) | ||
log.SetLevel(log.DebugLevel) | ||
} | ||
|
||
func NewSetupCfg() *iotwifi.SetupCfg { | ||
cfg := iotwifi.SetupCfg{ | ||
iotwifi.DnsmasqCfg{ | ||
"/#/10.10.10.1", "10.10.10.10,10.10.10.20,1h", "set:device,IoT", | ||
}, | ||
iotwifi.HostApdCfg{ | ||
"Test AP", "", "6", "10.10.10.1", | ||
}, | ||
iotwifi.WpaSupplicantCfg{ | ||
"/etc/wpa_supplicant/wpa_supplicant.conf", | ||
}, | ||
true, | ||
true, | ||
} | ||
return &cfg | ||
} | ||
|
||
func main() { | ||
log.Info("Starting IoT Wifi...") | ||
port := "8080" | ||
|
||
setupCfg := NewSetupCfg() | ||
h := iotwifi.NewHttpHandler(setupCfg, true) | ||
|
||
r := gin.Default() | ||
|
||
// set app routes | ||
r.GET("/status", gin.WrapF(h.StatusHandler)) | ||
r.POST("/connect", gin.WrapF(h.ConnectHandler)) | ||
r.GET("/scan", gin.WrapF(h.ScanHandler)) | ||
|
||
// --- | ||
if setupCfg.DontFallBackToApMode { | ||
r.GET("/reset", gin.WrapF(h.ResetHandler)) | ||
} | ||
|
||
if setupCfg.AllowStartStop { | ||
r.GET("/start", gin.WrapF(h.StartHandler)) | ||
r.GET("/stop", gin.WrapF(h.StopHandler)) | ||
} | ||
|
||
// CORS | ||
// headersOk := handlers.AllowedHeaders([]string{"Content-Type", "Authorization", "Content-Length", "X-Requested-With", "Accept", "Origin"}) | ||
// originsOk := handlers.AllowedOrigins([]string{"*"}) | ||
// methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS", "DELETE"}) | ||
|
||
// serve http | ||
log.Info("HTTP Listening on " + port) | ||
// http.ListenAndServe(":"+port, handlers.CORS(originsOk, headersOk, methodsOk)(r)) | ||
r.Run(":" + port) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// IoT Wifi Management | ||
|
||
// todo: update documentation!!!! | ||
// todo: update Dockerfile | ||
// todo: listen for shutdown signal, remove uap0, kill wpa,apd,dnsmasq | ||
|
||
package main | ||
|
||
import ( | ||
"net/http" | ||
"os" | ||
|
||
"github.com/gorilla/handlers" | ||
"github.com/gorilla/mux" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/txn2/txwifi/iotwifi" | ||
) | ||
|
||
func init() { | ||
log.SetOutput(os.Stdout) | ||
log.SetLevel(log.DebugLevel) | ||
} | ||
|
||
func main() { | ||
log.Info("Starting IoT Wifi...") | ||
|
||
cfgUrl := setEnvIfEmpty("IOTWIFI_CFG", "cfg/wificfg.json") | ||
port := setEnvIfEmpty("IOTWIFI_PORT", "8080") | ||
static := setEnvIfEmpty("IOTWIFI_STATIC", "/static/") | ||
|
||
setupCfg, err := iotwifi.LoadCfg(cfgUrl) | ||
if err != nil { | ||
log.Error("Could not load config: %s", err.Error()) | ||
panic(err) | ||
} | ||
|
||
h := iotwifi.NewHttpHandler(setupCfg, true) | ||
|
||
// setup router and middleware | ||
r := mux.NewRouter() | ||
//r.Use(h.LogHandler) | ||
|
||
// set app routes | ||
r.HandleFunc("/status", h.StatusHandler) | ||
r.HandleFunc("/connect", h.ConnectHandler).Methods("POST") | ||
r.HandleFunc("/scan", h.ScanHandler) | ||
|
||
// --- | ||
if setupCfg.DontFallBackToApMode { | ||
r.HandleFunc("/reset", h.ResetHandler) | ||
} | ||
|
||
if setupCfg.AllowStartStop { | ||
r.HandleFunc("/start", h.StartHandler) | ||
r.HandleFunc("/stop", h.StopHandler) | ||
} | ||
|
||
r.PathPrefix("/").Handler(http.FileServer(http.Dir(static))) | ||
http.Handle("/", r) | ||
|
||
// CORS | ||
headersOk := handlers.AllowedHeaders([]string{"Content-Type", "Authorization", "Content-Length", "X-Requested-With", "Accept", "Origin"}) | ||
originsOk := handlers.AllowedOrigins([]string{"*"}) | ||
methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS", "DELETE"}) | ||
|
||
// serve http | ||
log.Info("HTTP Listening on " + port) | ||
http.ListenAndServe(":"+port, handlers.CORS(originsOk, headersOk, methodsOk)(r)) | ||
|
||
} | ||
|
||
// getEnv gets an environment variable or sets a default if | ||
// one does not exist. | ||
func getEnv(key, fallback string) string { | ||
value := os.Getenv(key) | ||
if len(value) == 0 { | ||
return fallback | ||
} | ||
|
||
return value | ||
} | ||
|
||
// setEnvIfEmp<ty sets an environment variable to itself or | ||
// fallback if empty. | ||
func setEnvIfEmpty(env string, fallback string) (envVal string) { | ||
envVal = getEnv(env, fallback) | ||
os.Setenv(env, envVal) | ||
|
||
return envVal | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.