Browse Source

use httprouter for golink

Breandan Dezendorf 2 years ago
parent
commit
9a462de1b2

+ 3 - 1
dezendorf/applications/golink/cmd/main/BUILD.bazel

@@ -6,7 +6,9 @@ go_library(
     importpath = "gogs.dezendorf.net/breandan/monorepo/dezendorf/applications/golink/cmd/main",
     importpath = "gogs.dezendorf.net/breandan/monorepo/dezendorf/applications/golink/cmd/main",
     visibility = ["//visibility:private"],
     visibility = ["//visibility:private"],
     deps = [
     deps = [
-        "//dezendorf/applications/golink/internal/core"
+        "//dezendorf/applications/golink/internal/core",
+        "//dezendorf/applications/golink/internal/routes",
+
     ]
     ]
 )
 )
 
 

+ 5 - 1
dezendorf/applications/golink/cmd/main/main.go

@@ -5,6 +5,7 @@ import (
   "flag"
   "flag"
   "log"
   "log"
   "net/http"
   "net/http"
+  "github.com/julienschmidt/httprouter"
 )
 )
 
 
 import core "gogs.dezendorf.net/breandan/monorepo/dezendorf/applications/golink/internal/core"
 import core "gogs.dezendorf.net/breandan/monorepo/dezendorf/applications/golink/internal/core"
@@ -19,8 +20,11 @@ func main() {
   flag.Parse()
   flag.Parse()
   core.DepImport()
   core.DepImport()
   if *server {
   if *server {
-
     fmt.Printf("Starting linkserver on port :%d", *serverPort)
     fmt.Printf("Starting linkserver on port :%d", *serverPort)
+    router := httprouter.New()
+    router.GET("/", core.List)
+    router.GET("/search/:target", core.Search)
+
     if err := http.ListenAndServe(":80", nil); err != nil {
     if err := http.ListenAndServe(":80", nil); err != nil {
       log.Fatal(err)
       log.Fatal(err)
     }
     }

+ 2 - 0
dezendorf/applications/golink/go.mod

@@ -1,3 +1,5 @@
 module main
 module main
 
 
 go 1.19
 go 1.19
+
+require github.com/julienschmidt/httprouter v1.3.0

+ 2 - 0
dezendorf/applications/golink/go.sum

@@ -0,0 +1,2 @@
+github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
+github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=

+ 9 - 2
dezendorf/applications/golink/internal/core/core.go

@@ -1,7 +1,14 @@
 package core
 package core
 
 
-import "fmt"
+import (
+	"fmt"
+	"net/http"
+)
 
 
 func DepImport() {
 func DepImport() {
 	fmt.Println("this is a print")
 	fmt.Println("this is a print")
-}
+}
+
+func HandleList(w http.ResponseWriter, r *http.Request) {
+
+}

+ 15 - 1
dezendorf/applications/golink/internal/routes/routes.go

@@ -1 +1,15 @@
-package core
+package core
+
+import (
+	"fmt"
+	"net/http"
+	"github.com/julienschmidt/httprouter"
+)
+
+func List(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
+	fmt.Fprint(w, "Welcome!\n")
+}
+
+func Search(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
+	fmt.Fprintf(w, "Search! %s\n", ps.ByName("target"))
+}