Skip to content
This repository was archived by the owner on Aug 16, 2024. It is now read-only.

Commit dde5c05

Browse files
author
Stephen Gutekanst
committed
add -cloud mode for custom messaging on doctree.org
Signed-off-by: Stephen Gutekanst <stephen@sourcegraph.com>
1 parent 6c99be1 commit dde5c05

File tree

5 files changed

+90
-11
lines changed

5 files changed

+90
-11
lines changed

Taskfile.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ tasks:
66
cmds:
77
- task --parallel fmt-lint backend frontend --watch
88

9+
cloud:
10+
desc: Run development server, watching code files for changes.
11+
cmds:
12+
- task --parallel fmt-lint backend-cloud frontend --watch
13+
914
fmt-lint:
1015
desc: "Run formatters and linters"
1116
cmds:
@@ -21,6 +26,15 @@ tasks:
2126
deps:
2227
- build-go-debug
2328

29+
backend-cloud:
30+
desc: Run "doctree serve -cloud" backend
31+
cmds:
32+
- .bin/doctree serve -cloud
33+
env:
34+
ELM_DEBUG_SERVER: "http://localhost:1234/"
35+
deps:
36+
- build-go-debug
37+
2438
frontend:
2539
desc: Run Elm frontend dev server
2640
dir: frontend

cmd/doctree/serve.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ Examples:
3636
flagSet := flag.NewFlagSet("serve", flag.ExitOnError)
3737
dataDirFlag := flagSet.String("data-dir", defaultDataDir(), "where doctree stores its data")
3838
httpFlag := flagSet.String("http", ":3333", "address to bind for the HTTP server")
39+
cloudModeFlag := flagSet.Bool("cloud", false, "run in cloud mode (i.e. doctree.org)")
3940

4041
// Handles calls to our subcommand.
4142
handler := func(args []string) error {
4243
_ = flagSet.Parse(args)
4344
indexDataDir := filepath.Join(*dataDirFlag, "index")
44-
return Serve(*httpFlag, indexDataDir)
45+
return Serve(*cloudModeFlag, *httpFlag, indexDataDir)
4546
}
4647

4748
// Register the command.
@@ -58,10 +59,24 @@ Examples:
5859
}
5960

6061
// Serve an HTTP server on the given addr.
61-
func Serve(addr, indexDataDir string) error {
62+
func Serve(cloudMode bool, addr, indexDataDir string) error {
6263
log.Printf("Listening on %s", addr)
6364
mux := http.NewServeMux()
6465
mux.Handle("/", frontendHandler())
66+
mux.Handle("/main.js", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
67+
flags := struct {
68+
CloudMode bool `json:"cloudMode"`
69+
}{CloudMode: cloudMode}
70+
71+
flagsJson, err := json.Marshal(flags)
72+
if err != nil {
73+
http.Error(w, err.Error(), http.StatusInternalServerError)
74+
return
75+
}
76+
77+
w.Header().Set("Content-Type", "application/javascript")
78+
fmt.Fprintf(w, `Elm.Main.init({flags: %s})`, flagsJson)
79+
}))
6580
mux.Handle("/api/list", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
6681
// SECURITY: This endpoint isn't mutable and doesn't serve privileged information, and
6782
// therefor safe to use from any origin.
@@ -159,7 +174,7 @@ func frontendHandler() http.Handler {
159174
return proxy
160175
}
161176

162-
// Server assets that are embedded into Go binary.
177+
// Serve assets that are embedded into Go binary.
163178
fs := http.FS(frontend.EmbeddedFS())
164179
fileServer := http.FileServer(fs)
165180
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {

frontend/public/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@
3131
</script>
3232

3333
<script src="/dist/elm.js"></script>
34-
<script> Elm.Main.init() </script>
34+
<script src="/main.js"></script>
3535
</body>
3636
</html>

frontend/src/Pages/Home_.elm

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ page shared req =
3232
Page.element
3333
{ init = init
3434
, update = update
35-
, view = view
35+
, view = view shared.flags.cloudMode
3636
, subscriptions = subscriptions
3737
}
3838

@@ -139,8 +139,8 @@ subscriptions model =
139139
-- VIEW
140140

141141

142-
view : Model -> View Msg
143-
view model =
142+
view : Bool -> Model -> View Msg
143+
view cloudMode model =
144144
{ title = "doctree"
145145
, body =
146146
[ E.layout (List.concat [ Style.layout, [ E.width E.fill ] ])
@@ -154,6 +154,40 @@ view model =
154154
, if model.query /= "" then
155155
Element.Lazy.lazy searchResults model.results
156156

157+
else if cloudMode then
158+
E.column [ E.centerX ]
159+
[ Style.h2 [ E.paddingXY 0 32 ] (E.text "# Try doctree (sample projects)")
160+
, E.column [] (projectsList list)
161+
, Style.h2 [ E.paddingEach { top = 32, right = 0, bottom = 0, left = 0 } ]
162+
(E.text "# Add your repository to doctree.org")
163+
, E.paragraph [ E.paddingEach { top = 32, right = 0, bottom = 0, left = 0 } ]
164+
[ E.text "(coming soon)"
165+
]
166+
, Style.h2 [ E.paddingEach { top = 32, right = 0, bottom = 0, left = 0 } ]
167+
(E.text "# Experimental! Early stages!")
168+
, E.paragraph [ E.paddingEach { top = 16, right = 0, bottom = 0, left = 0 } ]
169+
[ E.text "We're working on adding more languages, polishing the experience, and adding usage examples. It's all very early stages and experimental - please bear with us!"
170+
]
171+
, Style.h3 [ E.paddingEach { top = 32, right = 0, bottom = 0, left = 0 } ]
172+
(E.text "# 100% open-source library docs tool for every language")
173+
, E.paragraph [ E.paddingEach { top = 16, right = 0, bottom = 0, left = 0 } ]
174+
[ E.text "Available "
175+
, E.link [ Font.underline ] { url = "https://github.com/sourcegraph/doctree", label = E.text "on GitHub" }
176+
, E.text ", doctree provides first-class library documentation for every language (based on tree-sitter), with symbol search & more. Using Sourcegraph, it can automatically find real-world usage examples."
177+
]
178+
, Style.h3 [ E.paddingEach { top = 32, right = 0, bottom = 0, left = 0 } ] (E.text "# Run locally, self-host, or use doctree.org")
179+
, E.paragraph [ E.paddingEach { top = 16, right = 0, bottom = 0, left = 0 } ]
180+
[ E.text "doctree is a single binary, and is designed to be lightweight for use on your personal machine. It's easy to self-host, or you can use via doctree.org with any GitHub repository."
181+
, E.link [ Font.underline ] { url = "https://github.com/sourcegraph/doctree#installation", label = E.text "installation instructions" }
182+
, E.text ")"
183+
]
184+
, E.paragraph [ E.paddingEach { top = 16, right = 0, bottom = 0, left = 0 } ]
185+
[ E.text "Extremely early stages (pre v0.1), please see "
186+
, E.link [ Font.underline ] { url = "https://github.com/sourcegraph/doctree/issues/27", label = E.text "our v1.0 roadmap" }
187+
, E.text " for more details on where we're going! Ideas/feedback welcome!"
188+
]
189+
]
190+
157191
else
158192
E.column [ E.centerX ]
159193
[ Style.h2 [ E.paddingXY 0 32 ] (E.text "# Your projects")

frontend/src/Shared.elm

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@ module Shared exposing
99

1010
import APISchema
1111
import Http
12-
import Json.Decode as Json
12+
import Json.Decode as Decode exposing (Decoder)
13+
import Json.Decode.Pipeline as Pipeline
1314
import Request exposing (Request)
1415
import Url.Builder
1516

1617

1718
type alias Flags =
18-
Json.Value
19+
Decode.Value
1920

2021

2122
type alias Model =
22-
{ currentProjectName : Maybe String
23+
{ flags : DecodedFlags
24+
, currentProjectName : Maybe String
2325
, projectIndexes : Maybe (Result Http.Error APISchema.ProjectIndexes)
2426
}
2527

@@ -29,10 +31,24 @@ type Msg
2931
| GotProject (Result Http.Error APISchema.ProjectIndexes)
3032

3133

34+
flagsDecoder : Decoder DecodedFlags
35+
flagsDecoder =
36+
Decode.succeed DecodedFlags
37+
|> Pipeline.required "cloudMode" Decode.bool
38+
39+
40+
type alias DecodedFlags =
41+
{ cloudMode : Bool
42+
}
43+
44+
3245
init : Request -> Flags -> ( Model, Cmd Msg )
33-
init _ _ =
46+
init _ flags =
3447
( { currentProjectName = Nothing
3548
, projectIndexes = Nothing
49+
, flags =
50+
Decode.decodeValue flagsDecoder flags
51+
|> Result.withDefault { cloudMode = False }
3652
}
3753
, Cmd.none
3854
)

0 commit comments

Comments
 (0)