GithubHelp home page GithubHelp logo

bitz0x75 / godoc-example Goto Github PK

View Code? Open in Web Editor NEW

This project forked from marvin5064/godoc-example

0.0 1.0 0.0 15 KB

An example on how to generate godoc in local machine for private repo

Dockerfile 13.76% Makefile 20.78% Go 47.53% Shell 17.93%

godoc-example's Introduction

How to Generate Customized Go Documents for Private Repository

Get Started in Local Machine

Godoc

the simple tool, provided by golang community, will help you get local customized go documents in a nutshell

godoc -http=:6060

after that, you will be able to review all go document under

open http://localhost:6060/pkg

Of course, the detail of documentation has to be done by you inline with code. This repository has some implementation in place, also here is more godoc styling tricks

However, the documents generated will include all directories and subdirectories under $GOPATH/src, most of them may not be necessary

Docker

Dockerfile

# Use an official Golang runtime as a parent image
FROM golang:1.10.3

# env setup for reusability
ENV APP_NAME=godoc-example
ENV WORKING_DIR=$GOPATH/src/github.com/marvin5064/$APP_NAME

# Set the working directory to golang working space
WORKDIR $WORKING_DIR

# Copy the current directory contents into the container at current directory
ADD . .

CMD godoc -http=:6060

Build & Run

docker build . \
    -t godoc-example/v1 # giving a tag for reference
docker run \
    -p 6060:6060 \ # (local):(docker) as we expose 6060 port in docker
    -it godoc-example/v1 # run for a built image by tag

for simplicity, the cmd has been stored in Makefile, you can simply trigger make docker-run to execute it

Check it out

after above steps, we can see all documentations about golang native library and current repository

open http://localhost:6060/pkg
open http://localhost:6060/pkg/github.com/marvin5064/godoc-example/

Extensive

Let s further exclude the golang native library from the documentation

After some research, godoc no yet support specific file documentation, there are some tricks for it

  1. Generate static web files for the repository you are working with, here we using docker with a simple script to generate it
#!/bin/bash
CURRENT_PKG=`go list -e`

# run a godoc server
godoc -http=:6060 & DOC_PID=$!

# Wait for the server to start
until curl -sSf "http://localhost:6060/pkg/$CURRENT_PKG/" > /dev/null
do
    sleep 1
done
sleep 1

# recursive fetch entire web including CSS & JS
# turn off robots check, otherwise might get blocked with details in `robots.txt` file
# only get the directories we are looking for
wget -r -p \
    -e robots=off \
    --include-directories="/lib/godoc,/pkg/$CURRENT_PKG,/src/$CURRENT_PKG" \
    --exclude-directories="/pkg/$CURRENT_PKG/vendor,/src/$CURRENT_PKG/vendor" \
    "http://localhost:6060/pkg/$CURRENT_PKG/"

# Stop the godoc server
kill -9 $DOC_PID

# all file will be generated into `localhost:6060` folder, hence we move them out from docker to local machine
mv localhost:6060 /tmp

you can simply achieve this by Makefile

make docker-gen-docs
  1. Serve it out by nodejs http server package as a static web page
make docker-gen-docs-server
  1. Check it out!
open http://localhost:8080/pkg/github.com/marvin5064/godoc-example/

Reference

PS - Improvements

  • use NGINX as a replacement of normal node server
    • NGINX is native for static website serving
    • Leverage http rules out of application/server side
  • use docker-compose
    • hence we can run on docs generation & http server on same container
    • if you run on a CD/CI process, different container do different job will be better
  • Extensive version has too many drawbacks
    • wait for golang community to make improvement on the godoc
    • documents are generated by crawler, it can by bugger at certain point
    • lack of their party documentation
      • if the repo itself has third party dependencies (vendor). For the approach with docker, I would suggest move all files in vendor to $GOPATH/src, and generate the docs right after.

Chinese Version

godoc-example's People

Contributors

marvin5064 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.